feat: simplify flag parsing in proxy plugin

Commands should be written such that they take arguments as is.
This commit is contained in:
Jose Diaz-Gonzalez committed 2020-05-29 01:59:50 -04:00
1 parent a60467cb2d
commit c22f78c913
3 files changed
+60 -77

No files matched your search

@@ -12,3 +12,4 @@ The `tls` name is no longer a reserved app name, and can be used by applications
The `apps:destroy` command no longer takes a second argument `force`. Instead, this can be passed as the `--force` flag, either globally or as a flag to the command itself. The `apps:destroy` command no longer takes a second argument `force`. Instead, this can be passed as the `--force` flag, either globally or as a flag to the command itself.
The `proxy:enable` and `proxy:disable` no longer restart apps when setting the `DOKKU_DISABLE_PROXY` environment variable. A future release will disable usage of `DOKKU_DISABLE_PROXY` altogether.
+24 -9
View File
@@ -19,40 +19,55 @@ func main() {
switch subcommand { switch subcommand {
case "disable": case "disable":
args := flag.NewFlagSet("proxy:disable", flag.ExitOnError) args := flag.NewFlagSet("proxy:disable", flag.ExitOnError)
skipRestart := args.Bool("no-restart", false, "--no-restart: skip restart of the app")
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandDisable(args.Args()) appName := args.Arg(0)
err = proxy.CommandDisable(appName, *skipRestart)
case "enable": case "enable":
args := flag.NewFlagSet("proxy:enable", flag.ExitOnError) args := flag.NewFlagSet("proxy:enable", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandEnable(args.Args()) appName := args.Arg(0)
err = proxy.CommandEnable(appName)
case "ports": case "ports":
args := flag.NewFlagSet("proxy:ports", flag.ExitOnError) args := flag.NewFlagSet("proxy:ports", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandPorts(args.Args()) appName := args.Arg(0)
err = proxy.CommandPorts(appName)
case "ports-add": case "ports-add":
args := flag.NewFlagSet("proxy:ports-add", flag.ExitOnError) args := flag.NewFlagSet("proxy:ports-add", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandPortsAdd(args.Args()) appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsAdd(appName, portMaps)
case "ports-clear": case "ports-clear":
args := flag.NewFlagSet("proxy:ports-clear", flag.ExitOnError) args := flag.NewFlagSet("proxy:ports-clear", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandPortsClear(args.Args()) appName := args.Arg(0)
err = proxy.CommandPortsClear(appName)
case "ports-remove": case "ports-remove":
args := flag.NewFlagSet("proxy:ports-remove", flag.ExitOnError) args := flag.NewFlagSet("proxy:ports-remove", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandPortsRemove(args.Args()) appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsRemove(appName, portMaps)
case "ports-set": case "ports-set":
args := flag.NewFlagSet("proxy:ports-set", flag.ExitOnError) args := flag.NewFlagSet("proxy:ports-set", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandPortsSet(args.Args()) appName := args.Arg(0)
_, portMaps := common.ShiftString(args.Args())
err = proxy.CommandPortsSet(appName, portMaps)
case "report": case "report":
args := flag.NewFlagSet("proxy:report", flag.ExitOnError) args := flag.NewFlagSet("proxy:report", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandReport(args.Args()) appName := args.Arg(0)
infoFlag := args.Arg(1)
err = proxy.CommandReport(appName, infoFlag)
case "set": case "set":
args := flag.NewFlagSet("proxy:set", flag.ExitOnError) args := flag.NewFlagSet("proxy:set", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
err = proxy.CommandSet(args.Args()) appName := args.Arg(0)
proxyType := args.Arg(1)
err = proxy.CommandSet(appName, proxyType)
default: default:
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
} }
+35 -68
View File
@@ -9,10 +9,9 @@ import (
) )
// CommandDisable disables the proxy for app via command line // CommandDisable disables the proxy for app via command line
func CommandDisable(args []string) error { func CommandDisable(appName string, skipRestart bool) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if !IsAppProxyEnabled(appName) { if !IsAppProxyEnabled(appName) {
@@ -21,16 +20,11 @@ func CommandDisable(args []string) error {
} }
common.LogInfo1("Disabling proxy for app") common.LogInfo1("Disabling proxy for app")
restart := true
if len(args) >= 2 && args[1] == "--no-restart" {
restart = false
}
entries := map[string]string{ entries := map[string]string{
"DOKKU_DISABLE_PROXY": "1", "DOKKU_DISABLE_PROXY": "1",
} }
if err := config.SetMany(appName, entries, restart); err != nil { if err := config.SetMany(appName, entries, false); err != nil {
return err return err
} }
@@ -38,10 +32,9 @@ func CommandDisable(args []string) error {
} }
// CommandEnable enables the proxy for app via command line // CommandEnable enables the proxy for app via command line
func CommandEnable(args []string) error { func CommandEnable(appName string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if IsAppProxyEnabled(appName) { if IsAppProxyEnabled(appName) {
@@ -50,13 +43,8 @@ func CommandEnable(args []string) error {
} }
common.LogInfo1("Enabling proxy for app") common.LogInfo1("Enabling proxy for app")
restart := true
if len(args) >= 2 && args[1] == "--no-restart" {
restart = false
}
keys := []string{"DOKKU_DISABLE_PROXY"} keys := []string{"DOKKU_DISABLE_PROXY"}
if err := config.UnsetMany(appName, keys, restart); err != nil { if err := config.UnsetMany(appName, keys, false); err != nil {
return err return err
} }
@@ -64,28 +52,25 @@ func CommandEnable(args []string) error {
} }
// CommandPorts is a cmd wrapper to list proxy port mappings for an app // CommandPorts is a cmd wrapper to list proxy port mappings for an app
func CommandPorts(args []string) error { func CommandPorts(appName string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
return listAppProxyPorts(appName) return listAppProxyPorts(appName)
} }
// CommandPortsAdd adds proxy port mappings to an app // CommandPortsAdd adds proxy port mappings to an app
func CommandPortsAdd(args []string) error { func CommandPortsAdd(appName string, portMaps []string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if len(args) == 1 { if len(portMaps) == 0 {
return errors.New("No port mapping specified") return errors.New("No port mapping specified")
} }
stringPortMap := strings.Join(args[1:], " ") proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
proxyPortMap, err := parseProxyPortMapString(stringPortMap)
if err != nil { if err != nil {
return err return err
} }
@@ -98,15 +83,13 @@ func CommandPortsAdd(args []string) error {
} }
// CommandPortsClear clears all proxy port mappings for an app // CommandPortsClear clears all proxy port mappings for an app
func CommandPortsClear(args []string) error { func CommandPortsClear(appName string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
keys := []string{"DOKKU_PROXY_PORT_MAP"} keys := []string{"DOKKU_PROXY_PORT_MAP"}
err = config.UnsetMany(appName, keys, false) if err := config.UnsetMany(appName, keys, false); err != nil {
if err != nil {
return err return err
} }
@@ -114,18 +97,16 @@ func CommandPortsClear(args []string) error {
} }
// CommandPortsRemove removes specific proxy port mappings from an app // CommandPortsRemove removes specific proxy port mappings from an app
func CommandPortsRemove(args []string) error { func CommandPortsRemove(appName string, portMaps []string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if len(args) == 1 { if len(portMaps) == 0 {
return errors.New("No port mapping specified") return errors.New("No port mapping specified")
} }
stringPortMap := strings.Join(args[1:], " ") proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
proxyPortMap, err := parseProxyPortMapString(stringPortMap)
if err != nil { if err != nil {
return err return err
} }
@@ -138,18 +119,16 @@ func CommandPortsRemove(args []string) error {
} }
// CommandPortsSet sets proxy port mappings for an app // CommandPortsSet sets proxy port mappings for an app
func CommandPortsSet(args []string) error { func CommandPortsSet(appName string, portMaps []string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if len(args) == 1 { if len(portMaps) == 0 {
return errors.New("No port mapping specified") return errors.New("No port mapping specified")
} }
stringPortMap := strings.Join(args[1:], " ") proxyPortMap, err := parseProxyPortMapString(strings.Join(portMaps, " "))
proxyPortMap, err := parseProxyPortMapString(stringPortMap)
if err != nil { if err != nil {
return err return err
} }
@@ -162,17 +141,7 @@ func CommandPortsSet(args []string) error {
} }
// CommandReport displays a proxy report for one or more apps // CommandReport displays a proxy report for one or more apps
func CommandReport(args []string) error { func CommandReport(appName string, infoFlag string) error {
appName, err := getAppName(args)
if err != nil {
return err
}
infoFlag := ""
if len(args) > 1 {
infoFlag = args[1]
}
if strings.HasPrefix(appName, "--") { if strings.HasPrefix(appName, "--") {
infoFlag = appName infoFlag = appName
appName = "" appName = ""
@@ -195,17 +164,15 @@ func CommandReport(args []string) error {
} }
// CommandSet sets a proxy for an app // CommandSet sets a proxy for an app
func CommandSet(args []string) error { func CommandSet(appName string, proxyType string) error {
appName, err := getAppName(args) if appName == "" {
if err != nil { return errors.New("Please specify an app to run the command on")
return err
} }
if len(appName) < 2 { if len(proxyType) < 2 {
return errors.New("Please specify a proxy type") return errors.New("Please specify a proxy type")
} }
proxyType := args[1]
entries := map[string]string{ entries := map[string]string{
"DOKKU_APP_PROXY_TYPE": proxyType, "DOKKU_APP_PROXY_TYPE": proxyType,
} }