diff --git a/Makefile b/Makefile index 98128083a1..23f652e40d 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,42 @@ .DEFAULT_GOAL: build + + +# The CLI binary to build +BIN ?= kyverno + +GIT_VERSION := $(shell git describe --dirty --always --tags) +GIT_HASH := $(shell git log -1 --pretty=format:"%H") +TIMESTAMP := $(shell date '+%Y-%m-%d_%I:%M:%S%p') + PACKAGE ?=github.com/nirmata/kyverno MAIN ?=$(PACKAGE) -LD_FLAGS ="-s -w" + +LD_FLAGS="-s -w -X $(PACKAGE)/pkg/kyverno/version.buildVersion=$(GIT_VERSION) -X $(PACKAGE)/pkg/kyverno/version.buildHash=$(GIT_HASH) -X $(PACKAGE)/pkg/kyverno/version.buildTime=$(TIMESTAMP)" REPO=nirmata/kyverno TAG=0.1 +GOOS ?= $(shell go env GOOS) +OUTPUT=$(shell pwd)/_output/cli/$(BIN) + build: GOOS=linux go build -ldflags=$(LD_FLAGS) $(MAIN) local: go build -ldflags=$(LD_FLAGS) $(MAIN) +cli: cli-dirs + GOOS=$(GOOS) \ + go build \ + -o $(OUTPUT) \ + -ldflags $(LD_FLAGS) \ + $(PACKAGE)/cmd/$(BIN) + + go build -ldflags=$(LD_FLAGS) $(CLI) + +cli-dirs: + @mkdir -p _output/cli + image: docker build -t $(REPO):$(TAG) . docker tag $(REPO):$(TAG) $(REPO):latest diff --git a/pkg/kyverno/cmd.go b/pkg/kyverno/cmd.go index 66aafc5e20..ac31e2a24f 100644 --- a/pkg/kyverno/cmd.go +++ b/pkg/kyverno/cmd.go @@ -5,6 +5,7 @@ import ( "os" "github.com/nirmata/kyverno/pkg/kyverno/apply" + "github.com/nirmata/kyverno/pkg/kyverno/version" "github.com/spf13/cobra" ) @@ -21,5 +22,6 @@ func NewKyvernoCommand(in io.Reader, out, errout io.Writer) *cobra.Command { } cmds.AddCommand(apply.NewCmdApply(in, out, errout)) + cmds.AddCommand(version.NewCmdVersion(out)) return cmds } diff --git a/pkg/kyverno/version/version.go b/pkg/kyverno/version/version.go new file mode 100644 index 0000000000..ece4845ce5 --- /dev/null +++ b/pkg/kyverno/version/version.go @@ -0,0 +1,34 @@ +package version + +import ( + "fmt" + "io" + + "github.com/spf13/cobra" +) + +var ( + buildVersion = "--" + buildHash = "--" + buildTime = "--" +) + +// NewCmdVersion is a command to display the build version +func NewCmdVersion(cmdOut io.Writer) *cobra.Command { + + versionCmd := &cobra.Command{ + Use: "version", + Short: "", + Run: func(cmd *cobra.Command, args []string) { + showVersion() + }, + } + + return versionCmd +} + +func showVersion() { + fmt.Printf("Version: %s\n", buildVersion) + fmt.Printf("Time: %s\n", buildTime) + fmt.Printf("Git commit ID: %s\n", buildHash) +}