From 3630b6fdcf51cf2ccefd79b245e24e35a12c7fce Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 19 Sep 2016 02:21:38 -0400 Subject: [PATCH] Add --version to show version information Signed-off-by: Lei Jitang --- Makefile | 8 +++++--- cmd/oci-create-runtime-bundle/main.go | 26 +++++++++++++++++++++----- cmd/oci-image-validate/main.go | 25 +++++++++++++++++++++---- cmd/oci-unpack/main.go | 25 ++++++++++++++++++++----- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 3654a39..3320c3d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ GO15VENDOREXPERIMENT=1 export GO15VENDOREXPERIMENT +COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true) + EPOCH_TEST_COMMIT ?= v0.2.0 default: help @@ -19,9 +21,9 @@ check-license: @./.tool/check-license tools: - go build ./cmd/oci-create-runtime-bundle - go build ./cmd/oci-unpack - go build ./cmd/oci-image-validate + go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-create-runtime-bundle + go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-unpack + go build -ldflags "-X main.gitCommit=${COMMIT}" ./cmd/oci-image-validate lint: @echo "checking lint" diff --git a/cmd/oci-create-runtime-bundle/main.go b/cmd/oci-create-runtime-bundle/main.go index e9b99ab..c517d09 100644 --- a/cmd/oci-create-runtime-bundle/main.go +++ b/cmd/oci-create-runtime-bundle/main.go @@ -20,10 +20,15 @@ import ( "os" "strings" + specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" "github.com/spf13/cobra" ) +// gitCommit will be the hash that the binary was built from +// and will be populated by the Makefile +var gitCommit = "" + // supported bundle types var bundleTypes = []string{ image.TypeImageLayout, @@ -31,11 +36,12 @@ var bundleTypes = []string{ } type bundleCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to bundle, can be empty string - ref string - root string + stdout *log.Logger + stderr *log.Logger + typ string // the type to bundle, can be empty string + ref string + root string + version bool } func main() { @@ -81,10 +87,20 @@ func newBundleCmd(stdout, stderr *log.Logger) *cobra.Command { It is strongly recommended to keep the default value.`, ) + cmd.Flags().BoolVar( + &v.version, "version", false, + `Print version information and exit`, + ) return cmd } func (v *bundleCmd) Run(cmd *cobra.Command, args []string) { + if v.version { + v.stdout.Printf("commit: %s", gitCommit) + v.stdout.Printf("spec: %s", specs.Version) + os.Exit(0) + + } if len(args) != 2 { v.stderr.Print("both src and dest must be provided") if err := cmd.Usage(); err != nil { diff --git a/cmd/oci-image-validate/main.go b/cmd/oci-image-validate/main.go index f8849bd..5143b63 100644 --- a/cmd/oci-image-validate/main.go +++ b/cmd/oci-image-validate/main.go @@ -21,11 +21,16 @@ import ( "strings" "github.com/opencontainers/image-spec/schema" + specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" "github.com/pkg/errors" "github.com/spf13/cobra" ) +// gitCommit will be the hash that the binary was built from +// and will be populated by the Makefile +var gitCommit = "" + // supported validation types var validateTypes = []string{ image.TypeImageLayout, @@ -36,10 +41,11 @@ var validateTypes = []string{ } type validateCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to validate, can be empty string - refs []string + stdout *log.Logger + stderr *log.Logger + typ string // the type to validate, can be empty string + refs []string + version bool } func main() { @@ -78,10 +84,21 @@ func newValidateCmd(stdout, stderr *log.Logger) *cobra.Command { `A set of refs pointing to the manifests to be validated. Each reference must be present in the "refs" subdirectory of the image. Only applicable if type is image or imageLayout.`, ) + cmd.Flags().BoolVar( + &v.version, "version", false, + `Print version information and exit`, + ) + return cmd } func (v *validateCmd) Run(cmd *cobra.Command, args []string) { + if v.version { + v.stdout.Printf("commit: %s", gitCommit) + v.stdout.Printf("spec: %s", specs.Version) + os.Exit(0) + } + if len(args) < 1 { v.stderr.Printf("no files specified") if err := cmd.Usage(); err != nil { diff --git a/cmd/oci-unpack/main.go b/cmd/oci-unpack/main.go index 463db62..ed0ffa7 100644 --- a/cmd/oci-unpack/main.go +++ b/cmd/oci-unpack/main.go @@ -20,10 +20,15 @@ import ( "os" "strings" + specs "github.com/opencontainers/image-spec/specs-go" "github.com/opencontainers/image-tools/image" "github.com/spf13/cobra" ) +// gitCommit will be the hash that the binary was built from +// and will be populated by the Makefile +var gitCommit = "" + // supported unpack types var unpackTypes = []string{ image.TypeImageLayout, @@ -31,10 +36,11 @@ var unpackTypes = []string{ } type unpackCmd struct { - stdout *log.Logger - stderr *log.Logger - typ string // the type to unpack, can be empty string - ref string + stdout *log.Logger + stderr *log.Logger + typ string // the type to unpack, can be empty string + ref string + version bool } func main() { @@ -73,11 +79,20 @@ func newUnpackCmd(stdout, stderr *log.Logger) *cobra.Command { &v.ref, "ref", "v1.0", `The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image.`, ) - + cmd.Flags().BoolVar( + &v.version, "version", false, + `Print version information and exit`, + ) return cmd } func (v *unpackCmd) Run(cmd *cobra.Command, args []string) { + if v.version { + v.stdout.Printf("commit: %s", gitCommit) + v.stdout.Printf("spec: %s", specs.Version) + os.Exit(0) + } + if len(args) != 2 { v.stderr.Print("both src and dest must be provided") if err := cmd.Usage(); err != nil {