From 7c4363dec53d0c286c921057c156ac94106c8c76 Mon Sep 17 00:00:00 2001 From: alastairruhm Date: Sat, 2 Dec 2017 16:00:29 +0800 Subject: [PATCH] feat: add --version flags --- cmd/root.go | 7 ++++--- glide.lock | 4 ++-- makefile | 7 ++++--- test/command/command_test.go | 22 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 1fca7ab..14828f2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,9 +19,10 @@ var ( // RootCmd ... var RootCmd = &cobra.Command{ - Use: "zj-db-cluster", - Short: "zijin database cluster command line tool", - Long: ``, + Use: "zj-db-cluster", + Short: "zijin database cluster command line tool", + Long: ``, + Version: VERSION, Run: func(cmd *cobra.Command, args []string) { // default command output usage string cmd.Usage() diff --git a/glide.lock b/glide.lock index 066f144..e027ed6 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 3c52349fa1fc3b07449a456690f1d36eb4479a5c7f39e6cf749305ac107add40 -updated: 2017-12-01T17:51:59.583475+08:00 +updated: 2017-12-02T15:51:59.879395+08:00 imports: - name: github.com/bouk/monkey version: b96e337f6e5b36906584cd0cf9803bbab09284f7 @@ -16,7 +16,7 @@ imports: - name: github.com/logrusorgru/aurora version: b9c3722b64a1976e0624448ee7f5a0744b8cf629 - name: github.com/spf13/cobra - version: 1be1d2841c773c01bee8289f55f7463b6e2c2539 + version: b1ec2ce1ad4e6e09b859c2420ecf3230275e58b7 - name: github.com/spf13/pflag version: 4c012f6dcd9546820e378d0bdda4d8fc772cdfea - name: golang.org/x/net diff --git a/makefile b/makefile index 31ff2a2..8b60f28 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,7 @@ ### Makefile --- + +# https://stackoverflow.com/questions/3931741/why-does-make-think-the-target-is-up-to-date +.PHONY: all test clean all: build @@ -12,12 +15,10 @@ lint: vendorcheck ./... test: - go test -v ./... + go test -v -gcflags=-l ./... clean: rm -rf bin -integration-test: - go test -tags=integration -v ### Makefile ends here diff --git a/test/command/command_test.go b/test/command/command_test.go index 8ecb3aa..dc2bf56 100644 --- a/test/command/command_test.go +++ b/test/command/command_test.go @@ -53,6 +53,28 @@ func TestVersionCmd(t *testing.T) { }) } +func TestVersionFlags(t *testing.T) { + RootCmd := cmd.RootCmd + Convey("TestVersionCmd", t, func() { + // some global val + Convey("version command should work properly", func() { + buf := new(bytes.Buffer) + RootCmd.SetArgs([]string{"--version"}) + RootCmd.SetOutput(buf) + + err := RootCmd.Execute() + + if err != nil { + t.Error(err) + } + + actual := buf.String() + expected := "zj-db-cluster version " + cmd.VERSION + "\n" + So(actual, ShouldEqual, expected) + }) + }) +} + func TestConfigCmd(t *testing.T) { rootCmd := cmd.RootCmd Convey("TestConfigCmd", t, func() {