11package main
22
33import (
4+ _ "embed"
45 "errors"
56 "fmt"
67 "io"
@@ -19,14 +20,36 @@ import (
1920 "github.com/urfave/cli"
2021)
2122
22- // version must be set from the contents of VERSION file by go build's
23- // -X main.version= option in the Makefile.
24- var version = "unknown"
23+ // version is set from the contents of VERSION file.
24+ //
25+ //go:embed VERSION
26+ var version string
27+
28+ // extraVersion is an optional suffix appended to runc version.
29+ // It can be set via Makefile ("make EXTRA_VERSION=xxx") or by
30+ // adding -X main.extraVersion=xxx option to the go build command.
31+ var extraVersion = ""
2532
2633// gitCommit will be the hash that the binary was built from
27- // and will be populated by the Makefile
34+ // and will be populated by the Makefile.
2835var gitCommit = ""
2936
37+ func printVersion (c * cli.Context ) {
38+ w := c .App .Writer
39+
40+ fmt .Fprintln (w , "runc version" , c .App .Version )
41+ if gitCommit != "" {
42+ fmt .Fprintln (w , "commit:" , gitCommit )
43+ }
44+ fmt .Fprintln (w , "spec:" , specs .Version )
45+ fmt .Fprintln (w , "go:" , runtime .Version ())
46+
47+ major , minor , micro := seccomp .Version ()
48+ if major + minor + micro > 0 {
49+ fmt .Fprintf (w , "libseccomp: %d.%d.%d\n " , major , minor , micro )
50+ }
51+ }
52+
3053const (
3154 specConfig = "config.json"
3255 usage = `Open Container Initiative runtime
@@ -57,21 +80,10 @@ value for "bundle" is the current directory.`
5780func main () {
5881 app := cli .NewApp ()
5982 app .Name = "runc"
83+ app .Version = strings .TrimSpace (version ) + extraVersion
6084 app .Usage = usage
6185
62- v := []string {version }
63-
64- if gitCommit != "" {
65- v = append (v , "commit: " + gitCommit )
66- }
67- v = append (v , "spec: " + specs .Version )
68- v = append (v , "go: " + runtime .Version ())
69-
70- major , minor , micro := seccomp .Version ()
71- if major + minor + micro > 0 {
72- v = append (v , fmt .Sprintf ("libseccomp: %d.%d.%d" , major , minor , micro ))
73- }
74- app .Version = strings .Join (v , "\n " )
86+ cli .VersionPrinter = printVersion
7587
7688 root := "/run/runc"
7789 xdgDirUsed := false
0 commit comments