This commit is contained in:
2023-09-03 12:11:35 +10:00
parent 91147f073f
commit badd2e3543
10 changed files with 343 additions and 49 deletions

View File

@@ -9,15 +9,16 @@ import (
)
const (
envPrefix = "wingmate"
configPathKey = "config_path"
configSearchPathKey = "config_search_path"
EnvPrefix = "wingmate"
PathKey = "config_path"
SearchPathKey = "config_search_path"
defaultPath = "/etc/wingmate/"
defaultName = "config"
ServiceKey = "service"
CronKey = "cron"
CommandKey = "command"
)
var (
@@ -27,9 +28,9 @@ var (
)
func init() {
viper.SetEnvPrefix(envPrefix)
_ = viper.BindEnv(configPathKey)
_ = viper.BindEnv(configSearchPathKey)
viper.SetEnvPrefix(EnvPrefix)
_ = viper.BindEnv(PathKey)
_ = viper.BindEnv(SearchPathKey)
}
func BindFlags(command *cobra.Command) {
@@ -44,13 +45,13 @@ func Read(cmd *cobra.Command, args []string) error {
_, _ = cmd, args // prevent warning for unused arguments
if viper.IsSet(configPathKey) {
configPath = viper.GetString(configPathKey)
if viper.IsSet(PathKey) {
configPath = viper.GetString(PathKey)
}
if viper.IsSet(configSearchPathKey) {
if err = viper.UnmarshalKey(configSearchPathKey, &configSearchPath); err != nil {
return fmt.Errorf("reading %s: %w %w", configSearchPathKey, err, debugframes.GetTraces())
if viper.IsSet(SearchPathKey) {
if err = viper.UnmarshalKey(SearchPathKey, &configSearchPath); err != nil {
return fmt.Errorf("reading %s: %w %w", SearchPathKey, err, debugframes.GetTraces())
}
}

View File

@@ -32,8 +32,8 @@ func TestRead(t *testing.T) {
{
name: "env",
env: map[string]string{
strings.ToUpper(envPrefix + "_" + configPathKey): "/path/to/config",
strings.ToUpper(envPrefix + "_" + configSearchPathKey): "/path/one,/path/two",
strings.ToUpper(EnvPrefix + "_" + PathKey): "/path/to/config",
strings.ToUpper(EnvPrefix + "_" + SearchPathKey): "/path/one,/path/two",
},
args: testReadArgs{
nil,
@@ -66,7 +66,7 @@ func TestRead(t *testing.T) {
}
tc.env = map[string]string{
strings.ToUpper(envPrefix + "_" + configPathKey): fname,
strings.ToUpper(EnvPrefix + "_" + PathKey): fname,
}
tc.post = func(t *testing.T, tc *testRead) {
@@ -142,7 +142,7 @@ func TestGet(t *testing.T) {
}
tc.env = map[string]string{
strings.ToUpper(envPrefix + "_" + configPathKey): fname,
strings.ToUpper(EnvPrefix + "_" + PathKey): fname,
}
tc.post = func(t *testing.T, tc *testRead) {