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

Commit

Permalink
main: Use function to generate version string
Browse files Browse the repository at this point in the history
Added a variable and function to generate the full version string to
simplify main() and allow for easier testing.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Sep 7, 2017
1 parent f112d4a commit 31d915d
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ var runtimeBeforeSubcommands = beforeSubcommands
// runtimeCommandNotFound is the function to handle an invalid sub-command.
var runtimeCommandNotFound = commandNotFound

// runtimeVersion is the function that returns the full version
// string describing the runtime.
var runtimeVersion = makeVersionString

// beforeSubcommands is the function to perform preliminary checks
// before command-line parsing occurs.
func beforeSubcommands(context *cli.Context) error {
Expand Down Expand Up @@ -183,23 +187,34 @@ func commandNotFound(c *cli.Context, command string) {
fatal(err)
}

func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
app.CommandNotFound = runtimeCommandNotFound

cli.AppHelpTemplate = fmt.Sprintf(`%s%s`, cli.AppHelpTemplate, notes)

// makeVersionString returns a multi-line string describing the runtime
// version along with the version of the OCI specification it supports.
func makeVersionString() string {
v := make([]string, 0, 3)

if version != "" {
v = append(v, name+" : "+version)
}

if commit != "" {
v = append(v, " commit : "+commit)
}
v = append(v, " OCI specs: "+specs.Version)
app.Version = strings.Join(v, "\n")

if specs.Version != "" {
v = append(v, " OCI specs: "+specs.Version)
}

return strings.Join(v, "\n")
}

func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
app.CommandNotFound = runtimeCommandNotFound

cli.AppHelpTemplate = fmt.Sprintf(`%s%s`, cli.AppHelpTemplate, notes)
app.Version = runtimeVersion()

// Override the default function to display version details to
// ensure the "--version" option and "version" command are identical.
Expand Down

0 comments on commit 31d915d

Please sign in to comment.