Allow MYLOG_CONFIG_PATH to override the config path

This commit is contained in:
Dan Jones 2026-03-09 16:48:58 -05:00
commit edf10de879
4 changed files with 18 additions and 20 deletions

View file

@ -22,7 +22,7 @@
## Config System
- Viper instance is stored in context using a custom key type (`confKeyType`)
- Use `config.New(path, overrides)` to create a new viper instance
- Use `config.New(ctx, path)` to create a new viper instance
- Use `config.RetrieveFromContext(ctx)` to get both viper and the unmarshaled Config struct
- Formatters can use `v.Sub("formatters." + kind)` to get their own sub-config and unmarshal into their specific config struct
- Test files must create viper instances and add them to context using `config.AddToContext`

View file

@ -28,18 +28,11 @@ var RootCmd = &cobra.Command{
Use: "my-log",
Short: "A brief description of your application",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ctx, v, err := config.New(cmd.Context())
if err != nil {
return err
}
if configPath != "" {
v.SetConfigFile(configPath)
err := v.ReadInConfig()
ctx, v, err := config.New(cmd.Context(), configPath)
if err != nil {
return err
}
}
for k, val := range configValues {
v.Set(k, val)
@ -62,6 +55,10 @@ var configPath string
var configValues map[string]string
func init() {
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file")
path := os.Getenv("MYLOG_CONFIG_PATH")
if path == "" {
path = config.DefaultPath()
}
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", path, "config file")
RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v formatters.preferred=json")
}

View file

@ -32,7 +32,7 @@ func AddToContext(ctx context.Context, v *viper.Viper) context.Context {
return context.WithValue(ctx, viperKey, v)
}
func New(ctx context.Context) (context.Context, *viper.Viper, error) {
func New(ctx context.Context, path string) (context.Context, *viper.Viper, error) {
v := viper.New()
v.SetConfigType("toml")
@ -40,12 +40,16 @@ func New(ctx context.Context) (context.Context, *viper.Viper, error) {
return ctx, nil, err
}
v.SetConfigFile(DefaultPath())
if path == "" {
path = DefaultPath()
}
v.SetConfigFile(path)
v.SetEnvPrefix("MYLOG")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
if err := v.ReadInConfig(); err != nil {
if err := v.MergeInConfig(); err != nil {
return ctx, nil, err
}

View file

@ -9,7 +9,7 @@ import (
)
func TestNew(t *testing.T) {
_, v, err := New(t.Context())
_, v, err := New(t.Context(), "")
be.Err(t, err, nil)
be.True(t, v != nil)
}
@ -18,7 +18,7 @@ func TestNewWithEnvOverrides(t *testing.T) {
os.Setenv("MYLOG_INPUT_PATH", "/test/path")
defer os.Unsetenv("MYLOG_INPUT_PATH")
_, v, err := New(t.Context())
_, v, err := New(t.Context(), "")
be.Err(t, err, nil)
be.Equal(t, v.GetString("input.path"), "/test/path")
}
@ -32,12 +32,9 @@ path = "/file/path"
ext = "log"`)
f.Close()
_, v, err := New(t.Context())
_, v, err := New(t.Context(), f.Name())
be.Err(t, err, nil)
v.SetConfigFile(f.Name())
v.SetConfigType("toml")
err = v.ReadInConfig()
be.Err(t, err, nil)
be.Equal(t, v.GetString("input.path"), "/file/path")
be.Equal(t, v.GetString("input.ext"), "log")