✨ Allow MYLOG_CONFIG_PATH to override the config path
This commit is contained in:
parent
b7eaf941fb
commit
edf10de879
4 changed files with 18 additions and 20 deletions
17
cli/root.go
17
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")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue