Repository: datreeio/datree
Pull request #24 (2021-05-02 12:00:01)

fix: add version command


Patch:
diff --git a/.goreleaser.yml b/.goreleaser.yml
index 1647efb..36ed9aa 100644
--- a/.goreleaser.yml
+++ b/.goreleaser.yml
@@ -1,7 +1,7 @@
 # This is an example .goreleaser.yml file with some sane defaults.
 # Make sure to check the documentation at http://goreleaser.com
 project_name: datree-cli
-release: 
+release:
   prerelease: auto
 before:
   hooks:
@@ -17,6 +17,8 @@ builds:
     main: ./main.go
     flags:
       - -tags={{.Env.DATREE_DEPLOYMENT}}
+    ldflags:
+      - -X github.com/datreeio/datree/cmd.CliVersion={{.Env.DATREE_BUILD_VERSION}}
   - env:
       - CGO_ENABLED=0
     binary: datree
@@ -26,6 +28,8 @@ builds:
     main: ./main.go
     flags:
       - -tags={{.Env.DATREE_DEPLOYMENT}}
+    ldflags:
+      - -X github.com/datreeio/datree/cmd.CliVersion={{.Env.DATREE_BUILD_VERSION}}
 archives:
   - replacements:
       darwin: Darwin
@@ -33,13 +37,12 @@ archives:
       amd64: x86_64
     format: zip
 checksum:
-  name_template: 'checksums.txt'
+  name_template: "checksums.txt"
 snapshot:
   name_template: "{{ .Tag }}-next"
 changelog:
   sort: asc
   filters:
     exclude:
-      - '^docs:'
-      - '^test:'
-      
\ No newline at end of file
+      - "^docs:"
+      - "^test:"
diff --git a/.travis.yml b/.travis.yml
index 5f8312f..27b1c7b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -32,3 +32,4 @@ env:
     - secure: "sEqRG6AlcLr7S4j4j8HznfV8tp+eaNjqP5d/wKtBBabEcAkRVTxKb/qiW56JkfE+v4KhtihNNYIX2nOKqOmVZLXkLWlpUqDsZ77owLIuQVb6ajr4HU0bw1vr20RPXThTqpIikeCoyiabrx2hQo0Epl11roay8V53fpPxwnenJETIlR95VXWxtDqTzow4QCyQy5AzlE51EGgPlSL9JqspvL7AwPQc2IY2v8M/Zhgdo2wQ2DVDnd2E1Bakt65ej9rvECfQIJ9d6kDHPQ8L8wqK1ALXbVrv/f2vlKorG6Nm5nNND2xYc5NUFUEq62K002U0HEUD7twHHoukeMB45R14SK+hBvQ9pPAIux7hzctSBp/PV1qCMMSGvEQmtTe9op1iGBvFfeCBVV4wC/IE49ymoh1yLz0noIAPcWC73UJWKJQkCTRFl0lI27yOkgSmgC/fOoCQmrsDrBPrxIR1mpNkDaqq0NXgZ73yIzI5NZepPY0QvmqSAO/FGwu/JvahYPoXsT0ZY6IXMTf50pOMphEwxv94euYu8sE7xxozkvfaoD3swuXTZD4YdNZCQcwQS5yz4rPSwjJFdq5BfE6GUckmaEXPDEqrBuqU0We35W4mjt9Gcm68Kx8bDhMrbAdO0LdqHXy4IsEkkrdvx2SDwxba4iDgEOvNW4tHNZMzzji2uXs="
     - GORELEASER_VERSION="0.157.0"
     - DATREE_DEPLOYMENT=$TRAVIS_BRANCH
+    - DATREE_BUILD_VERSION=0.1.$TRAVIS_BUILD_NUMBER-$TRAVIS_BRANCH
diff --git a/cmd/root.go b/cmd/root.go
index e28ed43..d3ec039 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -5,6 +5,7 @@ import (
 
 	"github.com/datreeio/datree/bl"
 	"github.com/datreeio/datree/cmd/test"
+	"github.com/datreeio/datree/cmd/version"
 	"github.com/datreeio/datree/pkg/cliClient"
 	"github.com/datreeio/datree/pkg/deploymentConfig"
 	"github.com/datreeio/datree/pkg/localConfig"
@@ -19,13 +20,18 @@ var rootCmd = &cobra.Command{
 	Long:  `Datree is a static code analysis tool for kubernetes files. Full code can be found at https://github.com/datreeio/datree`,
 }
 
+var CliVersion string
+
 func init() {
 	app := startup()
 
-	rootCmd.AddCommand(test.CreateTestCommand(&test.TestCommandContext{
+	rootCmd.AddCommand(test.NewTestCommand(&test.TestCommandContext{
+		CliVersion:  CliVersion,
 		Evaluator:   app.Context.Evaluator,
 		LocalConfig: app.Context.LocalConfig,
 	}))
+
+	rootCmd.AddCommand(version.NewVersionCommand(CliVersion))
 }
 
 func Execute() {
diff --git a/cmd/version/main.go b/cmd/version/main.go
new file mode 100644
index 0000000..4c7ea9c
--- /dev/null
+++ b/cmd/version/main.go
@@ -0,0 +1,19 @@
+package version
+
+import (
+	"fmt"
+
+	"github.com/spf13/cobra"
+)
+
+func NewVersionCommand(cliVersion string) *cobra.Command {
+	var versionCmd = &cobra.Command{
+		Use:   "version",
+		Short: "Print the version number",
+		Long:  "Print the version number",
+		Run: func(cmd *cobra.Command, args []string) {
+			fmt.Println(cliVersion)
+		},
+	}
+	return versionCmd
+}


Test patch:
diff --git a/cmd/test/main.go b/cmd/test/main.go
index f6b5343..d92c98b 100644
--- a/cmd/test/main.go
+++ b/cmd/test/main.go
@@ -23,6 +23,7 @@ type Evaluator interface {
 }
 
 type TestCommandContext struct {
+	CliVersion  string
 	LocalConfig LocalConfigManager
 	Evaluator   Evaluator
 }
@@ -31,7 +32,7 @@ type TestCommandFlags struct {
 	Output string
 }
 
-func CreateTestCommand(ctx *TestCommandContext) *cobra.Command {
+func NewTestCommand(ctx *TestCommandContext) *cobra.Command {
 	testCommand := &cobra.Command{
 		Use:   "test",
 		Short: "Execute static analysis for pattern",
@@ -74,7 +75,7 @@ func test(ctx *TestCommandContext, pattern string, flags TestCommandFlags) error
 		return err
 	}
 
-	evaluationResponse, fileParsingErrors, err := ctx.Evaluator.Evaluate(absolutePath, config.CliId, 50, "0.0.1")
+	evaluationResponse, fileParsingErrors, err := ctx.Evaluator.Evaluate(absolutePath, config.CliId, 50, ctx.CliVersion)
 	s.Stop()
 
 	if err != nil {
diff --git a/cmd/version/main_test.go b/cmd/version/main_test.go
new file mode 100644
index 0000000..61b4cb0
--- /dev/null
+++ b/cmd/version/main_test.go
@@ -0,0 +1,21 @@
+package version
+
+import (
+	"bytes"
+	"io/ioutil"
+	"testing"
+)
+
+func Test_ExecuteCommand(t *testing.T) {
+	cmd := NewVersionCommand("1.2.3")
+	b := bytes.NewBufferString("1.2.3")
+	cmd.SetOut(b)
+	cmd.Execute()
+	out, err := ioutil.ReadAll(b)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if string(out) != "1.2.3" {
+		t.Fatalf("expected \"%s\" got \"%s\"", "1.2.3", string(out))
+	}
+}