From edf10de87946d0fc87fdc922ad472f5494255ed9 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 9 Mar 2026 16:48:58 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20MYLOG=5FCONFIG=5FPATH=20to?= =?UTF-8?q?=20override=20the=20config=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 2 +- cli/root.go | 17 +++++++---------- config/load.go | 10 +++++++--- config/load_test.go | 9 +++------ 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 74f23bc..8ea2d6e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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` diff --git a/cli/root.go b/cli/root.go index ddaca3b..9b32959 100644 --- a/cli/root.go +++ b/cli/root.go @@ -28,19 +28,12 @@ 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()) + + ctx, v, err := config.New(cmd.Context(), configPath) if err != nil { return err } - if configPath != "" { - v.SetConfigFile(configPath) - err := v.ReadInConfig() - 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") } diff --git a/config/load.go b/config/load.go index fd20cae..09726d2 100644 --- a/config/load.go +++ b/config/load.go @@ -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 } diff --git a/config/load_test.go b/config/load_test.go index 71dbb0c..af885b0 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -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")