Skip to content

Commit

Permalink
feat: Add version command
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 13, 2023
1 parent ce3e002 commit 5734121
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >-
Expand Down
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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:
Expand Down Expand Up @@ -44,6 +48,7 @@ func Root(p cli.Params) *cobra.Command {
cmd.AddCommand(
pipeline.Command(p),
pipelinerun.Command(p),
version.Command(),
)

return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
30 changes: 30 additions & 0 deletions pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
}
24 changes: 24 additions & 0 deletions pkg/cmd/version/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 6 additions & 1 deletion pkg/test/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

0 comments on commit 5734121

Please sign in to comment.