45 lines
931 B
Go
45 lines
931 B
Go
|
package cfg
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/joho/godotenv"
|
||
|
"github.com/kelseyhightower/envconfig"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
ScriptBinaryPath string `required:"true" split_words:"true"`
|
||
|
WebhookScriptPath string `required:"true" split_words:"true"`
|
||
|
FileToWatchPath string `required:"true" split_words:"true"`
|
||
|
}
|
||
|
|
||
|
func rootDir() string {
|
||
|
cmdOut, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
|
||
|
if err != nil {
|
||
|
log.Fatalf("exec.Command: %s", err)
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
rootDir := strings.TrimSpace(string(cmdOut))
|
||
|
return rootDir
|
||
|
}
|
||
|
|
||
|
func NewConfig(isProd bool) *Config {
|
||
|
if !isProd {
|
||
|
err := godotenv.Load(rootDir() + "/.env")
|
||
|
if err != nil {
|
||
|
log.Fatalf("environment variable ENV is empty and an error occurred while loading the .env file\n")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cfg := &Config{}
|
||
|
err := envconfig.Process("", cfg)
|
||
|
if err != nil {
|
||
|
log.Fatalf("envconfig.Process: %s\n", err)
|
||
|
}
|
||
|
|
||
|
return cfg
|
||
|
}
|