-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: option to load arguments from config file (#107)
- bind flags from viper to cobra command - add tests - test for Execute - support multiple config formats - update readme with help message - document configuration env and file Closes #21 **Proposed Changes** - feat: read arguments from Environment Variables - feat: read arguments from JSON/YAML file - docs: auto-update the README when the `help` message was changed - docs: how to use arguments from Environment Variables or config file **Limitations** - You still can run only one plugin at a time. - You have to add the plugin command to the CLI command. - Positional arguments are not yet supported. --------- Co-authored-by: Jossef Harush Kadouri <jossef12@gmail.com>
- Loading branch information
Showing
6 changed files
with
938 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func LoadConfig(v *viper.Viper, configFilePath string) error { | ||
if configFilePath == "" { | ||
return nil | ||
} | ||
|
||
configType := strings.TrimPrefix(filepath.Ext(configFilePath), ".") | ||
|
||
v.SetConfigType(configType) | ||
v.SetConfigFile(configFilePath) | ||
return v.ReadInConfig() | ||
} | ||
|
||
// TODO: can be a package | ||
|
||
// BindFlags fill flags values with config file or environment variables data | ||
func BindFlags(cmd *cobra.Command, v *viper.Viper, envPrefix string) error { | ||
commandHierarchy := getCommandHierarchy(cmd) | ||
|
||
bindFlag := func(f *pflag.Flag) { | ||
fullFlagName := fmt.Sprintf("%s%s", commandHierarchy, f.Name) | ||
bindEnvVarIntoViper(v, fullFlagName, envPrefix) | ||
|
||
if f.Changed { | ||
return | ||
} | ||
|
||
if v.IsSet(fullFlagName) { | ||
val := v.Get(fullFlagName) | ||
applyViperFlagToCommand(f, val, cmd) | ||
} | ||
} | ||
cmd.PersistentFlags().VisitAll(bindFlag) | ||
cmd.Flags().VisitAll(bindFlag) | ||
|
||
for _, subCmd := range cmd.Commands() { | ||
if err := BindFlags(subCmd, v, envPrefix); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func bindEnvVarIntoViper(v *viper.Viper, fullFlagName, envPrefix string) { | ||
envVarSuffix := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(fullFlagName, "-", "_"), ".", "_")) | ||
envVarName := fmt.Sprintf("%s_%s", envPrefix, envVarSuffix) | ||
|
||
if err := v.BindEnv(fullFlagName, envVarName, strings.ToLower(envVarName)); err != nil { | ||
log.Err(err).Msg("Failed to bind Viper flags") | ||
} | ||
} | ||
|
||
func applyViperFlagToCommand(flag *pflag.Flag, val interface{}, cmd *cobra.Command) { | ||
switch t := val.(type) { | ||
case []interface{}: | ||
var paramSlice []string | ||
for _, param := range t { | ||
paramSlice = append(paramSlice, param.(string)) | ||
} | ||
valStr := strings.Join(paramSlice, ",") | ||
if err := flag.Value.Set(valStr); err != nil { | ||
log.Err(err).Msg("Failed to set Viper flags") | ||
} | ||
default: | ||
newVal := fmt.Sprintf("%v", val) | ||
if err := flag.Value.Set(newVal); err != nil { | ||
log.Err(err).Msg("Failed to set Viper flags") | ||
} | ||
} | ||
flag.Changed = true | ||
} | ||
|
||
func getCommandHierarchy(cmd *cobra.Command) string { | ||
names := []string{} | ||
if !cmd.HasParent() { | ||
return "" | ||
} | ||
|
||
for parent := cmd; parent.HasParent() && parent.Name() != ""; parent = parent.Parent() { | ||
names = append([]string{parent.Name()}, names...) | ||
} | ||
|
||
if len(names) == 0 { | ||
return "" | ||
} | ||
|
||
return strings.Join(names, ".") + "." | ||
} |
Oops, something went wrong.