From 5734121eb21cc1794d48fe4c4293c785c6e61efe Mon Sep 17 00:00:00 2001 From: Sergiy Kulanov Date: Fri, 13 Oct 2023 17:30:25 +0300 Subject: [PATCH] feat: Add version command Signed-off-by: Sergiy Kulanov --- .goreleaser.yaml | 2 +- Makefile | 12 ++++++++++-- pkg/cmd/root.go | 7 ++++++- pkg/cmd/root_test.go | 2 +- pkg/cmd/version/version.go | 30 ++++++++++++++++++++++++++++++ pkg/cmd/version/version_test.go | 24 ++++++++++++++++++++++++ pkg/test/params.go | 7 ++++++- 7 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 pkg/cmd/version/version.go create mode 100644 pkg/cmd/version/version_test.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index bb88d49..2038bcb 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -25,7 +25,7 @@ builds: ldflags: # use commit date instead of current date as main.date # only needed if you actually use those things in your main package, otherwise can be ignored. - - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} + - -w -X github.com/sergk/tkn-graph/pkg/cmd/cliVersion={{.Version}} archives: - name_template: >- diff --git a/Makefile b/Makefile index 577fb37..ada46d1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ +PACKAGE=github.com/sergk/tkn-graph/pkg/cmd + # Go parameters GOCMD=go -GOBUILD=$(GOCMD) build +GOBUILD=$(GOCMD) build -v GOTEST=$(GOCMD) test GOLINT=golangci-lint run GOFMT=$(GOCMD) fmt @@ -10,6 +12,12 @@ CURRENT_DIR=$(shell pwd) # Binary name BINARY_NAME=tkn-graph +# Versioning +GIT_DESCRIBE=$(shell git describe --tags --always --dirty) +LDFLAGS=-ldflags "-X $(PACKAGE)/cliVersion=$(GIT_DESCRIBE)" + +override GCFLAGS +=all=-trimpath=${CURRENT_DIR} + .DEFAULT_GOAL:=help # Directories @@ -20,7 +28,7 @@ BIN_DIR=./bin # Build targets .PHONY: build build: ## build the binary - $(GOBUILD) -o $(DIST_DIR)/$(BINARY_NAME) $(SRC_DIR)/... + CGO_ENABLED=0 $(GOBUILD) $(LDFLAGS) -o $(DIST_DIR)/$(BINARY_NAME) -gcflags '${GCFLAGS}' $(SRC_DIR)/... # Test targets .PHONY: test diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 6f83cc2..416821f 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/sergk/tkn-graph/pkg/cmd/pipeline" "github.com/sergk/tkn-graph/pkg/cmd/pipelinerun" + "github.com/sergk/tkn-graph/pkg/cmd/version" "github.com/spf13/cobra" "github.com/tektoncd/cli/pkg/cli" ) @@ -16,7 +17,10 @@ Aliases: Examples: {{.Example}}{{end}}{{if .HasAvailableSubCommands}} -Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} +Available Commands:{{range .Commands}}{{if (eq .Annotations.commandType "main")}} + {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} + +Other Commands:{{range .Commands}}{{if (or (eq .Annotations.commandType "utility") (eq .Name "help"))}} {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}} Flags: @@ -44,6 +48,7 @@ func Root(p cli.Params) *cobra.Command { cmd.AddCommand( pipeline.Command(p), pipelinerun.Command(p), + version.Command(), ) return cmd diff --git a/pkg/cmd/root_test.go b/pkg/cmd/root_test.go index a4c3392..55250a4 100644 --- a/pkg/cmd/root_test.go +++ b/pkg/cmd/root_test.go @@ -26,7 +26,7 @@ func TestRoot(t *testing.T) { } // Assert that the command has the expected subcommands. - if len(cmd.Commands()) != 4 { + if len(cmd.Commands()) != 5 { t.Errorf("Command does not have the expected subcommands: %v", cmd.Commands()) } } diff --git a/pkg/cmd/version/version.go b/pkg/cmd/version/version.go new file mode 100644 index 0000000..bce09dd --- /dev/null +++ b/pkg/cmd/version/version.go @@ -0,0 +1,30 @@ +package version + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +const ( + // Version is the current version of the CLI + devVersion = "dev" +) + +var ( + cliVersion = devVersion +) + +// Command returns the version command +func Command() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Prints version information", + Annotations: map[string]string{ + "commandType": "utility", + }, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", cliVersion) + }, + } +} diff --git a/pkg/cmd/version/version_test.go b/pkg/cmd/version/version_test.go new file mode 100644 index 0000000..f43ad39 --- /dev/null +++ b/pkg/cmd/version/version_test.go @@ -0,0 +1,24 @@ +package version + +import ( + "bytes" + "testing" +) + +func TestVersionCommand(t *testing.T) { + cmd := Command() + + // Create a Buffer to capture the output. + out := new(bytes.Buffer) + cmd.SetOut(out) + + // Execute the command. + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + // Assert that the command is valid. + if cmd == nil || cmd.Name() != "version" { + t.Errorf("Command is not valid: %v", cmd) + } +} diff --git a/pkg/test/params.go b/pkg/test/params.go index 68378bf..852746b 100644 --- a/pkg/test/params.go +++ b/pkg/test/params.go @@ -24,6 +24,11 @@ import ( "k8s.io/client-go/rest" ) +const ( + TestYear = 1984 + TestDay = 4 +) + type Params struct { ns, kubeCfg, kubeCtx string Tekton versioned.Interface @@ -93,5 +98,5 @@ func (p *Params) Time() clockwork.Clock { } func FakeClock() clockwork.FakeClock { - return clockwork.NewFakeClockAt(time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC)) + return clockwork.NewFakeClockAt(time.Date(TestYear, time.April, TestDay, 0, 0, 0, 0, time.UTC)) }