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

@ -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")
}