Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
main: Add varabies for cli flags, commands and before func
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jodh-intel committed Sep 7, 2017
1 parent b278bfa commit 4018a0f
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4018a0f

Please sign in to comment.