♲ Refactor configuration to use viper with context propagation
- Replace global ConfigPath and Overrides with viper-based configuration - Add viper.New() to create configurable viper instances - Store viper and unmarshaled Config struct in context for testability - Add RetrieveFromContext and AddToContext helper functions - Update files.Append to accept context and retrieve config from it - Update formatters.Preferred and formatters.New to accept context - Add PersistentPreRunE in CLI to create and configure viper instance - Support -c flag for custom config file path - Support -v flag for config value overrides - Update all test files to create viper and add to context - Remove unused config types and load functions - Add viper as dependency with automatic env var support (MYLOG_*)
This commit is contained in:
parent
d34363b8c0
commit
9f05f933dd
21 changed files with 338 additions and 360 deletions
|
|
@ -32,23 +32,24 @@ var ConfigCmd = &cobra.Command{
|
|||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
print, _ := cmd.Flags().GetBool("print")
|
||||
if print {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), config.ConfigPath)
|
||||
fmt.Fprintln(cmd.OutOrStdout(), config.DefaultPath())
|
||||
return nil
|
||||
}
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
configPath := config.DefaultPath()
|
||||
if !force {
|
||||
_, err = os.Stat(config.ConfigPath)
|
||||
_, err = os.Stat(configPath)
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", configPath)
|
||||
}
|
||||
}
|
||||
dir := fp.Dir(config.ConfigPath)
|
||||
dir := fp.Dir(configPath)
|
||||
err = os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(config.ConfigPath)
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue