Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #24 from datreeio/DAT-3181-add-dynamic-version
Browse files Browse the repository at this point in the history
fix: add version command
  • Loading branch information
Yishay Mendelsohn authored May 3, 2021
2 parents 82d2fd7 + 64dd750 commit cecf6cf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
13 changes: 8 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand All @@ -26,20 +28,21 @@ 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
linux: Linux
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:'

- "^docs:"
- "^test:"
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Evaluator interface {
}

type TestCommandContext struct {
CliVersion string
LocalConfig LocalConfigManager
Evaluator Evaluator
}
Expand All @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions cmd/version/main.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions cmd/version/main_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit cecf6cf

Please sign in to comment.