Skip to content

Instantly share code, notes, and snippets.

@wyattjoh
Created July 21, 2016 16:10
Show Gist options
  • Save wyattjoh/3458965372fcb65dbd8a48d4077dd2f1 to your computer and use it in GitHub Desktop.
Save wyattjoh/3458965372fcb65dbd8a48d4077dd2f1 to your computer and use it in GitHub Desktop.
CLI Config Provider
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