Skip to content

Commit 8ab786f

Browse files
committed
version: Add a version command
And adjust 'runc --version' to print exactly the same content. The $ COMMAND --version interface is a popular one for this information and makes a lot of sense for single-action commands. For example, printf(1) [1] is only ever going to do one meaningful thing, so --version, --help, and other command-like actions work well as options. Umbrella commands with multiple sub-commands, on the other hand, have a more consistent interface if '... and exit' actions get their own sub-commands. That allows you to declare an interface like: $ COMMAND [global-options] SUB-COMMAND [sub-command-specific-options] [sub-command-specific-arguments] without having to say "except if you use a sub-command-like global option, in which case SUB-COMMAND is not allowed". With this commit, we add support for 'version' while keeping --version, because while 'version' makes more sense for umbrella commands, a user may not know if runc is an umbrella command when they ask for the version. Existing umbrella commands are not particularly consistent: * git(1) supports both --version and 'version', --help and 'help'. * btrfs(8) supports both --version and 'version', --help and 'help'. * ip(1) supports -V / -Version and 'help'. * npm supports 'version' and 'help'. * pip supports '--version', --help and 'help'. [1]: http://www.man7.org/linux/man-pages/man1/printf.1.html Signed-off-by: W. Trevor King <[email protected]>
1 parent 41b12c0 commit 8ab786f

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

main.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"strings"
76

87
"github.com/Sirupsen/logrus"
9-
"github.com/opencontainers/runtime-spec/specs-go"
108
"github.com/urfave/cli"
119
)
1210

13-
// version will be populated by the Makefile, read from
14-
// VERSION file of the source code.
15-
var version = ""
16-
17-
// gitCommit will be the hash that the binary was built from
18-
// and will be populated by the Makefile
19-
var gitCommit = ""
20-
2111
const (
2212
specConfig = "config.json"
2313
usage = `Open Container Initiative runtime
@@ -50,15 +40,9 @@ func main() {
5040
app.Name = "runc"
5141
app.Usage = usage
5242

53-
var v []string
54-
if version != "" {
55-
v = append(v, version)
56-
}
57-
if gitCommit != "" {
58-
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
43+
cli.VersionPrinter = func(context *cli.Context) {
44+
printVersion(context)
5945
}
60-
v = append(v, fmt.Sprintf("spec: %s", specs.Version))
61-
app.Version = strings.Join(v, "\n")
6246
app.Flags = []cli.Flag{
6347
cli.BoolFlag{
6448
Name: "debug",
@@ -107,6 +91,7 @@ func main() {
10791
startCommand,
10892
stateCommand,
10993
updateCommand,
94+
versionCommand,
11095
}
11196
app.Before = func(context *cli.Context) error {
11297
if context.GlobalBool("debug") {

tests/integration/version.bats

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
load helpers
44

5-
@test "runc version" {
5+
@test "runc --version" {
66
runc -v
77
[ "$status" -eq 0 ]
8-
[[ ${lines[0]} =~ runc\ version\ [0-9]+\.[0-9]+\.[0-9]+ ]]
8+
[[ ${lines[0]} =~ runC [0-9]+\.[0-9]+\.[0-9]+ ]]
9+
[[ ${lines[1]} =~ commit:+ ]]
10+
[[ ${lines[2]} =~ spec:\ [0-9]+\.[0-9]+\.[0-9]+ ]]
11+
}
12+
13+
@test "runc version" {
14+
runc version
15+
[ "$status" -eq 0 ]
16+
[[ ${lines[0]} =~ runC [0-9]+\.[0-9]+\.[0-9]+ ]]
917
[[ ${lines[1]} =~ commit:+ ]]
1018
[[ ${lines[2]} =~ spec:\ [0-9]+\.[0-9]+\.[0-9]+ ]]
1119
}

version.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/opencontainers/runtime-spec/specs-go"
8+
"github.com/urfave/cli"
9+
)
10+
11+
// version will be populated by the Makefile, read from
12+
// VERSION file of the source code.
13+
var version = ""
14+
15+
// gitCommit will be the hash that the binary was built from
16+
// and will be populated by the Makefile
17+
var gitCommit = ""
18+
19+
var versionCommand = cli.Command{
20+
Name: "version",
21+
Usage: "output the runtime version",
22+
Description: `The version command outputs the runtime version.`,
23+
Action: printVersion,
24+
}
25+
26+
func printVersion(context *cli.Context) (err error) {
27+
if version == "" {
28+
_, err = os.Stdout.WriteString("runC unknown\n")
29+
} else {
30+
_, err = os.Stdout.WriteString(fmt.Sprintf("runC %s\n", version))
31+
}
32+
if err != nil {
33+
return err
34+
}
35+
if gitCommit != "" {
36+
_, err = os.Stdout.WriteString(fmt.Sprintf("commit: %s\n", gitCommit))
37+
if err != nil {
38+
return err
39+
}
40+
}
41+
_, err = os.Stdout.WriteString(fmt.Sprintf("spec: %s\n", specs.Version))
42+
return err
43+
}

0 commit comments

Comments
 (0)