From 86f75e7986e71fec7671be668d9ff8ac5985625f Mon Sep 17 00:00:00 2001 From: myishay Date: Sun, 2 May 2021 18:43:32 +0300 Subject: [PATCH 1/2] fix: add version command --- .goreleaser.yml | 13 ++++++++----- .travis.yml | 1 + cmd/root.go | 8 +++++++- cmd/test/main.go | 5 +++-- cmd/version/main.go | 19 +++++++++++++++++++ cmd/version/main_test.go | 21 +++++++++++++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 cmd/version/main.go create mode 100644 cmd/version/main_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 1647efb174..36ed9aa8bc 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 5f8312f316..27b1c7b749 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 e28ed4383e..d3ec039263 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/test/main.go b/cmd/test/main.go index f6b5343880..d92c98ba5e 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.go b/cmd/version/main.go new file mode 100644 index 0000000000..fdad096c3c --- /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 of Hugo", + Long: `All software has versions. This is Hugo's`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("datree version: ", cliVersion) + }, + } + return versionCmd +} diff --git a/cmd/version/main_test.go b/cmd/version/main_test.go new file mode 100644 index 0000000000..61b4cb0bc5 --- /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)) + } +} From 64dd750f1aede47b20aa7202f7f7c244b9df671a Mon Sep 17 00:00:00 2001 From: myishay Date: Mon, 3 May 2021 10:16:50 +0300 Subject: [PATCH 2/2] fix: correct version command description --- cmd/version/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/version/main.go b/cmd/version/main.go index fdad096c3c..4c7ea9c62a 100644 --- a/cmd/version/main.go +++ b/cmd/version/main.go @@ -9,10 +9,10 @@ import ( func NewVersionCommand(cliVersion string) *cobra.Command { var versionCmd = &cobra.Command{ Use: "version", - Short: "Print the version number of Hugo", - Long: `All software has versions. This is Hugo's`, + Short: "Print the version number", + Long: "Print the version number", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("datree version: ", cliVersion) + fmt.Println(cliVersion) }, } return versionCmd