diff --git a/main.go b/main.go index b97fb696..5c3561a6 100644 --- a/main.go +++ b/main.go @@ -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 { @@ -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.