From 9595d7900e74506c13d58186895903d65aac66ef Mon Sep 17 00:00:00 2001 From: umohnani8 Date: Fri, 16 Jun 2017 12:27:06 -0400 Subject: [PATCH] Add kpod version Signed-off-by: umohnani8 --- Makefile | 7 ++++++- cmd/kpod/main.go | 6 +++++- cmd/kpod/version.go | 43 ++++++++++++++++++++++++++++++++++++++++++ completions/bash/kpod | 9 +++++++++ docs/kpod-version.1.md | 33 ++++++++++++++++++++++++++++++++ test/helpers.bash | 2 ++ test/kpod.bats | 9 +++++++++ 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 cmd/kpod/version.go create mode 100644 docs/kpod-version.1.md create mode 100644 test/kpod.bats diff --git a/Makefile b/Makefile index ffeb6f85..0c04d9b2 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ ETCDIR_CRIO ?= ${ETCDIR}/crio BUILDTAGS := selinux seccomp $(shell hack/btrfs_tag.sh) $(shell hack/libdm_tag.sh) BASHINSTALLDIR=${PREFIX}/share/bash-completion/completions +GIT_COMMIT := $(shell git rev-parse --short HEAD) +BUILD_INFO := $(shell date +%s) + # If GOPATH not specified, use one in the local directory ifeq ($(GOPATH),) export GOPATH := $(CURDIR)/_output @@ -25,6 +28,8 @@ GOPKGBASEDIR := $(shell dirname "$(GOPKGDIR)") # Update VPATH so make finds .gopathok VPATH := $(VPATH):$(GOPATH) +LDFLAGS := -ldflags '-X main.gitCommit=${GIT_COMMIT} -X main.buildInfo=${BUILD_INFO}' + all: binaries crio.conf docs default: help @@ -77,7 +82,7 @@ crioctl: .gopathok $(shell hack/find-godeps.sh $(GOPKGDIR) cmd/crioctl $(PROJECT $(GO) build -o $@ $(PROJECT)/cmd/crioctl kpod: .gopathok $(shell hack/find-godeps.sh $(GOPKGDIR) cmd/kpod $(PROJECT)) - $(GO) build -o $@ $(PROJECT)/cmd/kpod + $(GO) build $(LDFLAGS) -o $@ $(PROJECT)/cmd/kpod crio.conf: crio ./crio --config="" config --default > crio.conf diff --git a/cmd/kpod/main.go b/cmd/kpod/main.go index c1c34e4b..26a354a1 100644 --- a/cmd/kpod/main.go +++ b/cmd/kpod/main.go @@ -7,14 +7,18 @@ import ( "github.com/urfave/cli" ) +//Version of kpod +const Version string = "0.0.1" + func main() { app := cli.NewApp() app.Name = "kpod" app.Usage = "manage pods and images" - app.Version = "0.0.1" + app.Version = Version app.Commands = []cli.Command{ launchCommand, + versionCommand, } if err := app.Run(os.Args); err != nil { diff --git a/cmd/kpod/version.go b/cmd/kpod/version.go new file mode 100644 index 00000000..a2971a05 --- /dev/null +++ b/cmd/kpod/version.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "runtime" + "strconv" + "time" + + "github.com/urfave/cli" +) + +// Overwritten at build time +var ( + gitCommit string + buildInfo string +) + +// versionCmd gets and prints version info for version command +func versionCmd(c *cli.Context) error { + + // Converts unix time from string to int64 + buildTime, err := strconv.ParseInt(buildInfo, 10, 64) + if err != nil { + return err + } + + fmt.Println("Version: ", Version) + fmt.Println("Go Version: ", runtime.Version()) + fmt.Println("Git Commit: ", gitCommit) + + // Prints out the build time in readable format + fmt.Println("Built: ", time.Unix(buildTime, 0).Format(time.ANSIC)) + fmt.Println("OS/Arch: ", runtime.GOOS+"/"+runtime.GOARCH) + + return nil +} + +// Cli command to print out the full version of kpod +var versionCommand = cli.Command{ + Name: "version", + Usage: "Display the KPOD Version Information", + Action: versionCmd, +} diff --git a/completions/bash/kpod b/completions/bash/kpod index 9b96bc42..d11acf94 100644 --- a/completions/bash/kpod +++ b/completions/bash/kpod @@ -27,6 +27,14 @@ _kpod_launch() { _complete_ "$options_with_args" "$boolean_options" } +_kpod_version() { + local options_with_args=" + " + local boolean_options=" + " + _complete_ "$options_with_args" "$boolean_options" +} + _kpod_kpod() { local options_with_args=" " @@ -36,6 +44,7 @@ _kpod_kpod() { " commands=" launch + version " case "$prev" in diff --git a/docs/kpod-version.1.md b/docs/kpod-version.1.md new file mode 100644 index 00000000..385bded4 --- /dev/null +++ b/docs/kpod-version.1.md @@ -0,0 +1,33 @@ +% kpod(8) # kpod-version - Simple tool to view version information +% Dan Walsh +% SEPTEMBER 2016 +# NAME +kpod-version - Display the KPOD Version Information + +# SYNOPSIS +**kpod version** +[**--help**|**-h**] + +# DESCRIPTION +Shows the the following information: Version, Go Version, Git Commit, Build Time, +OS, and Architecture. + +**kpod [GLOBAL OPTIONS]** + +**kpod [GLOBAL OPTIONS] version [OPTIONS]** + +# GLOBAL OPTIONS + +**--help, -h** + Print usage statement + +# COMMANDS + +## version +Display the KPOD Version Information + +# SEE ALSO +kpod(1), crio(8), crio.conf(5) + +# HISTORY +Dec 2016, Originally compiled by Dan Walsh diff --git a/test/helpers.bash b/test/helpers.bash index 54aa891d..a3ec021a 100644 --- a/test/helpers.bash +++ b/test/helpers.bash @@ -13,6 +13,8 @@ CRIO_ROOT=${CRIO_ROOT:-$(cd "$INTEGRATION_ROOT/../.."; pwd -P)} CRIO_BINARY=${CRIO_BINARY:-${CRIO_ROOT}/cri-o/crio} # Path of the crioctl binary. OCIC_BINARY=${OCIC_BINARY:-${CRIO_ROOT}/cri-o/crioctl} +# Path to kpod binary. +KPOD_BINARY=${KPOD_BINARY:-${CRIO_ROOT}/cri-o/kpod} # Path of the conmon binary. CONMON_BINARY=${CONMON_BINARY:-${CRIO_ROOT}/cri-o/conmon/conmon} # Path of the pause binary. diff --git a/test/kpod.bats b/test/kpod.bats new file mode 100644 index 00000000..fb8cfaf1 --- /dev/null +++ b/test/kpod.bats @@ -0,0 +1,9 @@ +#!/usr/bin/env bats + +load helpers + +@test "kpod version test" { + run ${KPOD_BINARY} version + echo "$output" + [ "$status" -eq 0 ] +}