From 4018a0f348dd61639c57129867bff49d59b05ad2 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 7 Sep 2017 14:36:01 +0100 Subject: [PATCH] main: Add varabies for cli flags, commands and before func Add variables for CLI flags, commands and the before function and setup the CLI app using these variables. This simplifies main() and will make testing easier. Signed-off-by: James O. D. Hunt --- main.go | 96 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/main.go b/main.go index 291d6754..3f31c1d0 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,57 @@ var defaultOutputFile = os.Stdout // messages to. var defaultErrorFile = os.Stderr +// runtimeFlags is the list of supported global command-line flags +var runtimeFlags = []cli.Flag{ + cli.StringFlag{ + Name: "cc-config", + Usage: project + " config file path", + }, + cli.BoolFlag{ + Name: "debug", + Usage: "enable debug output for logging", + }, + cli.StringFlag{ + Name: "log", + Value: "/dev/null", + Usage: "set the log file path where internal debug information is written", + }, + cli.StringFlag{ + Name: "log-format", + Value: "text", + Usage: "set the format used by logs ('text' (default), or 'json')", + }, + cli.StringFlag{ + Name: "root", + Value: defaultRootDirectory, + Usage: "root directory for storage of container state (this should be located in tmpfs)", + }, +} + +// runtimeCommands is the list of supported command-line (sub-) +// commands. +var runtimeCommands = []cli.Command{ + checkCLICommand, + envCLICommand, + createCLICommand, + deleteCLICommand, + execCLICommand, + killCLICommand, + listCLICommand, + runCLICommand, + pauseCLICommand, + resumeCLICommand, + startCLICommand, + stateCLICommand, + versionCLICommand, +} + +// runtimeBeforeSubcommands is the function to run before command-line +// parsing occurs. +var runtimeBeforeSubcommands = beforeSubcommands + +// beforeSubcommands is the function to perform preliminary checks +// before command-line parsing occurs. func beforeSubcommands(context *cli.Context) error { if userWantsUsage(context) || (context.NArg() == 1 && (context.Args()[0] == "cc-check")) { // No setup required if the user just @@ -145,49 +196,10 @@ func main() { fmt.Println(c.App.Version) } - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "cc-config", - Usage: project + " config file path", - }, - cli.BoolFlag{ - Name: "debug", - Usage: "enable debug output for logging", - }, - cli.StringFlag{ - Name: "log", - Value: "/dev/null", - Usage: "set the log file path where internal debug information is written", - }, - cli.StringFlag{ - Name: "log-format", - Value: "text", - Usage: "set the format used by logs ('text' (default), or 'json')", - }, - cli.StringFlag{ - Name: "root", - Value: defaultRootDirectory, - Usage: "root directory for storage of container state (this should be located in tmpfs)", - }, - } - - app.Commands = []cli.Command{ - checkCLICommand, - envCLICommand, - createCLICommand, - deleteCLICommand, - execCLICommand, - killCLICommand, - listCLICommand, - runCLICommand, - pauseCLICommand, - resumeCLICommand, - startCLICommand, - stateCLICommand, - versionCLICommand, - } + app.Flags = runtimeFlags + app.Commands = runtimeCommands + app.Before = runtimeBeforeSubcommands - app.Before = beforeSubcommands // If the command returns an error, cli takes upon itself to print // the error on cli.ErrWriter and exit. // Use our own writer here to ensure the log gets sent to the right