feat: simplify flag parsing in config plugin
Commands should be written such that they take arguments as is.
This commit is contained in:
@@ -10,58 +10,87 @@ import (
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
)
|
||||
|
||||
func getKeys(args []string, global bool) []string {
|
||||
keys := args
|
||||
if !global && len(keys) > 1 {
|
||||
keys = keys[1:]
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// main entrypoint to all subcommands
|
||||
func main() {
|
||||
parts := strings.Split(os.Args[0], "/")
|
||||
subcommand := parts[len(parts)-1]
|
||||
|
||||
var err error
|
||||
switch subcommand {
|
||||
case "bundle":
|
||||
args := flag.NewFlagSet("config:bundle", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandBundle(args.Args(), *global, *merged)
|
||||
appName := args.Arg(0)
|
||||
err = config.CommandBundle(appName, *global, *merged)
|
||||
case "clear":
|
||||
args := flag.NewFlagSet("config:clear", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandClear(args.Args(), *global, *noRestart)
|
||||
appName := args.Arg(0)
|
||||
err = config.CommandClear(appName, *global, *noRestart)
|
||||
case "export":
|
||||
args := flag.NewFlagSet("config:export", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
format := args.String("format", "exports", "--format: [ exports | envfile | docker-args | shell | pretty | json | json-list ] which format to export as)")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandExport(args.Args(), *global, *merged, *format)
|
||||
appName := args.Arg(0)
|
||||
err = config.CommandExport(appName, *global, *merged, *format)
|
||||
case "get":
|
||||
args := flag.NewFlagSet("config:get", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
quoted := args.Bool("quoted", false, "--quoted: get the value quoted")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandGet(args.Args(), *global, *quoted)
|
||||
appName := args.Arg(0)
|
||||
keys := getKeys(args.Args(), *global)
|
||||
err = config.CommandGet(appName, keys, *global, *quoted)
|
||||
case "keys":
|
||||
args := flag.NewFlagSet("config:keys", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: merge app environment and global environment")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandKeys(args.Args(), *global, *merged)
|
||||
appName := args.Arg(0)
|
||||
err = config.CommandKeys(appName, *global, *merged)
|
||||
case "show":
|
||||
args := flag.NewFlagSet("config:show", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
merged := args.Bool("merged", false, "--merged: display the app's environment merged with the global environment")
|
||||
args.Parse(os.Args[2:])
|
||||
appName := args.Arg(0)
|
||||
err = config.CommandShow(appName, *global, *merged, false, false)
|
||||
case "set":
|
||||
args := flag.NewFlagSet("config:set", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
encoded := args.Bool("encoded", false, "--encoded: interpret VALUEs as base64")
|
||||
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandSet(args.Args(), *global, *noRestart, *encoded)
|
||||
appName := args.Arg(0)
|
||||
pairs := getKeys(args.Args(), *global)
|
||||
err = config.CommandSet(appName, pairs, *global, *noRestart, *encoded)
|
||||
case "unset":
|
||||
args := flag.NewFlagSet("config:unset", flag.ExitOnError)
|
||||
global := args.Bool("global", false, "--global: use the global environment")
|
||||
noRestart := args.Bool("no-restart", false, "--no-restart: no restart")
|
||||
args.Parse(os.Args[2:])
|
||||
config.CommandUnset(args.Args(), *global, *noRestart)
|
||||
appName := args.Arg(0)
|
||||
keys := getKeys(args.Args(), *global)
|
||||
err = config.CommandUnset(appName, keys, *global, *noRestart)
|
||||
default:
|
||||
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user