refactor: migrate docker-options subcommands to go
Ports the user-facing CLI subcommands and lifecycle triggers from bash to go, mirroring the layout used by the ports plugin. The on-disk format (`DOCKER_OPTIONS_BUILD/DEPLOY/RUN` files), the `docker-args-build/deploy/run` triggers, and every exported helper consumed by other plugins are unchanged. Reserves the `--process` and `--global` flags on add, remove, and clear so usage today exits with a not-yet-implemented error and the surface is locked in ahead of the process-scoping work tracked in #2441.
This commit is contained in:
57
plugins/docker-options/src/commands/commands.go
Normal file
57
plugins/docker-options/src/commands/commands.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
const (
|
||||
helpHeader = `Usage: dokku docker-options[:COMMAND]
|
||||
|
||||
Manage docker options for an app
|
||||
|
||||
Additional commands:`
|
||||
|
||||
helpContent = `
|
||||
docker-options:add <app> <phase(s)> OPTION, Add Docker option to app for phase (comma separated phase list)
|
||||
docker-options:clear <app> [phase(s)], Clear a docker options from application with an optional phase (comma separated phase list)
|
||||
docker-options:remove <app> <phase(s)> OPTION, Remove Docker option from app for phase (comma separated phase list)
|
||||
docker-options:report [<app>] [<flag>], Displays a docker options report for one or more apps`
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
||||
cmd := flag.Arg(0)
|
||||
switch cmd {
|
||||
case "docker-options", "docker-options:help":
|
||||
usage()
|
||||
case "help":
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
Command: "ps",
|
||||
Args: []string{"-o", "command=", strconv.Itoa(os.Getppid())},
|
||||
})
|
||||
if err == nil && strings.Contains(result.StdoutContents(), "--all") {
|
||||
fmt.Println(helpContent)
|
||||
} else {
|
||||
fmt.Print("\n docker-options, Manage docker options for an app\n")
|
||||
}
|
||||
default:
|
||||
dokkuNotImplementExitCode, err := strconv.Atoi(os.Getenv("DOKKU_NOT_IMPLEMENTED_EXIT"))
|
||||
if err != nil {
|
||||
fmt.Println("failed to retrieve DOKKU_NOT_IMPLEMENTED_EXIT environment variable")
|
||||
dokkuNotImplementExitCode = 10
|
||||
}
|
||||
os.Exit(dokkuNotImplementExitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func usage() {
|
||||
common.CommandUsage(helpHeader, helpContent)
|
||||
}
|
||||
109
plugins/docker-options/src/subcommands/subcommands.go
Normal file
109
plugins/docker-options/src/subcommands/subcommands.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
dockeroptions "github.com/dokku/dokku/plugins/docker-options"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func main() {
|
||||
parts := strings.Split(os.Args[0], "/")
|
||||
subcommand := parts[len(parts)-1]
|
||||
|
||||
var err error
|
||||
switch subcommand {
|
||||
case "add":
|
||||
args := flag.NewFlagSet("docker-options:add", flag.ExitOnError)
|
||||
args.SetInterspersed(false)
|
||||
processes := args.StringSlice("process", []string{}, "process types to scope this option to (reserved for future use)")
|
||||
global := args.Bool("global", false, "explicitly mark as a global option (reserved for future use)")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
if err = dockeroptions.ErrIfReservedFlagsUsed(*processes, *global); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
positional := args.Args()
|
||||
appName := ""
|
||||
phases := ""
|
||||
var optionParts []string
|
||||
if len(positional) > 0 {
|
||||
appName = positional[0]
|
||||
}
|
||||
if len(positional) > 1 {
|
||||
phases = positional[1]
|
||||
}
|
||||
if len(positional) > 2 {
|
||||
optionParts = positional[2:]
|
||||
}
|
||||
err = dockeroptions.CommandAdd(appName, phases, strings.Join(optionParts, " "))
|
||||
case "remove":
|
||||
args := flag.NewFlagSet("docker-options:remove", flag.ExitOnError)
|
||||
args.SetInterspersed(false)
|
||||
processes := args.StringSlice("process", []string{}, "process types to scope this option to (reserved for future use)")
|
||||
global := args.Bool("global", false, "explicitly mark as a global option (reserved for future use)")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
if err = dockeroptions.ErrIfReservedFlagsUsed(*processes, *global); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
positional := args.Args()
|
||||
appName := ""
|
||||
phases := ""
|
||||
var optionParts []string
|
||||
if len(positional) > 0 {
|
||||
appName = positional[0]
|
||||
}
|
||||
if len(positional) > 1 {
|
||||
phases = positional[1]
|
||||
}
|
||||
if len(positional) > 2 {
|
||||
optionParts = positional[2:]
|
||||
}
|
||||
err = dockeroptions.CommandRemove(appName, phases, strings.Join(optionParts, " "))
|
||||
case "clear":
|
||||
args := flag.NewFlagSet("docker-options:clear", flag.ExitOnError)
|
||||
args.SetInterspersed(false)
|
||||
processes := args.StringSlice("process", []string{}, "process types to scope this option to (reserved for future use)")
|
||||
global := args.Bool("global", false, "explicitly mark as a global option (reserved for future use)")
|
||||
args.Parse(os.Args[2:])
|
||||
|
||||
if err = dockeroptions.ErrIfReservedFlagsUsed(*processes, *global); err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
positional := args.Args()
|
||||
appName := ""
|
||||
phases := ""
|
||||
if len(positional) > 0 {
|
||||
appName = positional[0]
|
||||
}
|
||||
if len(positional) > 1 {
|
||||
phases = positional[1]
|
||||
}
|
||||
err = dockeroptions.CommandClear(appName, phases)
|
||||
case "report":
|
||||
args := flag.NewFlagSet("docker-options:report", flag.ExitOnError)
|
||||
format := args.String("format", "stdout", "format: [ stdout | json ]")
|
||||
osArgs, infoFlag, flagErr := common.ParseReportArgs("docker-options", os.Args[2:])
|
||||
if flagErr == nil {
|
||||
args.Parse(osArgs)
|
||||
appName := args.Arg(0)
|
||||
err = dockeroptions.CommandReport(appName, *format, infoFlag)
|
||||
} else {
|
||||
err = flagErr
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
common.LogFailWithError(err)
|
||||
}
|
||||
}
|
||||
41
plugins/docker-options/src/triggers/triggers.go
Normal file
41
plugins/docker-options/src/triggers/triggers.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
dockeroptions "github.com/dokku/dokku/plugins/docker-options"
|
||||
)
|
||||
|
||||
func main() {
|
||||
parts := strings.Split(os.Args[0], "/")
|
||||
trigger := parts[len(parts)-1]
|
||||
flag.Parse()
|
||||
|
||||
var err error
|
||||
switch trigger {
|
||||
case "post-app-clone-setup":
|
||||
oldAppName := flag.Arg(0)
|
||||
newAppName := flag.Arg(1)
|
||||
err = dockeroptions.TriggerPostAppCloneSetup(oldAppName, newAppName)
|
||||
case "post-app-rename-setup":
|
||||
oldAppName := flag.Arg(0)
|
||||
newAppName := flag.Arg(1)
|
||||
err = dockeroptions.TriggerPostAppRenameSetup(oldAppName, newAppName)
|
||||
case "post-delete":
|
||||
appName := flag.Arg(0)
|
||||
err = dockeroptions.TriggerPostDelete(appName)
|
||||
case "report":
|
||||
appName := flag.Arg(0)
|
||||
err = dockeroptions.ReportSingleApp(appName, "", "")
|
||||
default:
|
||||
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
common.LogFailWithError(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user