Created
July 21, 2016 16:10
-
-
Save wyattjoh/3458965372fcb65dbd8a48d4077dd2f1 to your computer and use it in GitHub Desktop.
CLI Config Provider
This file contains hidden or 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
package main | |
import ( | |
"flag" | |
"strings" | |
"github.com/urfave/cli" | |
) | |
// CLIProvider provides configuration from the cli.Context passed into an | |
// Action. | |
type CLIProvider struct { | |
Context *cli.Context | |
} | |
// Provide implements the Provider interface. | |
func (clip CLIProvider) Provide() (map[string]string, error) { | |
var config = make(map[string]string) | |
// load all the route specific flags first | |
flagNames := clip.Context.FlagNames() | |
for _, flagName := range flagNames { | |
value := clip.Context.Generic(flagName).(flag.Value) | |
config[strings.ToUpper(flagName)] = value.String() | |
} | |
// then load the global flags | |
globalFlagNames := clip.Context.GlobalFlagNames() | |
for _, globalFlagName := range globalFlagNames { | |
value := clip.Context.Generic(globalFlagName).(flag.Value) | |
config[strings.ToUpper(globalFlagName)] = value.String() | |
} | |
return config, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment