git-webhook-ci/cfg/config.go

46 lines
1003 B
Go
Raw Normal View History

2023-02-25 22:32:10 +01:00
package cfg
import (
"log"
"os/exec"
"strings"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
type Config struct {
2023-02-26 10:59:22 +01:00
ScriptBinaryPath string `required:"true" split_words:"true"`
WebhookScriptPath string `required:"true" split_words:"true"`
FileToWatchPath string `required:"true" split_words:"true"`
2023-02-26 22:10:07 +01:00
TestFileToWatchPath string `required:"false" split_words:"true"`
2023-02-25 22:32:10 +01:00
}
2023-02-26 22:10:07 +01:00
func RootDir() string {
2023-02-25 22:32:10 +01:00
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 {
2023-02-26 22:10:07 +01:00
err := godotenv.Load(RootDir() + "/.env")
2023-02-25 22:32:10 +01:00
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
}