Skip to content

Commit

Permalink
feat: add version command
Browse files Browse the repository at this point in the history
Signed-off-by: Jae Aeich <jh4official@gmail.com>
  • Loading branch information
JaeAeich committed Feb 13, 2024
1 parent b95a9b2 commit 169fbd2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
23 changes: 15 additions & 8 deletions Makefile
@@ -1,29 +1,36 @@
PROJECT_PKG=github.com/goharbor/harbor-cli
VERSION_PKG=$(PROJECT_PKG)/cmd/harbor/internal/version
GITCOMMIT := $(shell git rev-parse --short=8 HEAD)
PROJECT_PKG = github.com/goharbor/harbor-cli
RELEASE_CHANNEL="edge"
LDFLAGS = "-w -s -X $(PROJECT_PKG)/version.GitCommit=$(GITCOMMIT) -X $(PROJECT_PKG)/version.ReleaseChannel=$(RELEASE_CHANNEL)"
GO_VERSION := $(shell go version | cut -c 14- | cut -d' ' -f1)
BUILD_TIME := "$(shell date +'%a_%b_%d_%T_%Y')"
RELEASE_CHANNEL=edge
LDFLAGS := -w -s \
-X $(VERSION_PKG).GitCommit=$(GITCOMMIT) \
-X $(VERSION_PKG).GoVersion=$(GO_VERSION) \
-X $(VERSION_PKG).BuildTime=$(BUILD_TIME) \
-X $(VERSION_PKG).ReleaseChannel=$(RELEASE_CHANNEL)
ARCH := amd64
GO_EXE = go

make:
gofmt -l -s -w .
go build -ldflags=${LDFLAGS} -o harbor cmd/harbor/main.go
go build -v -ldflags "${LDFLAGS}" -o harbor cmd/harbor/main.go

windows:
go build -ldflags=${LDFLAGS} -o harbor.exe cmd/harbor/main.go
go build -ldflags "${LDFLAGS}" -o harbor.exe cmd/harbor/main.go

.PHONY: build-win-amd64
build-win-amd64: ## build for windows amd64
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=windows $(GO_EXE) build -v --ldflags=$(LDFLAGS) \
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=windows $(GO_EXE) build -v --ldflags "$(LDFLAGS)" \
-o bin/harbor-windows-$(ARCH).exe ./cmd/harbor/main.go
.PHONY: build-linux-amd64
build-linux-amd64: ## build for linux amd64
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=linux $(GO_EXE) build -v --ldflags=$(LDFLAGS) \
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=linux $(GO_EXE) build -v --ldflags "$(LDFLAGS)" \
-o bin/harbor-linux-$(ARCH) ./cmd/harbor/main.go

.PHONY: build-darwin-amd64
build-darwin-amd64: ## build for darwin amd64
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=darwin $(GO_EXE) build -v --ldflags=$(LDFLAGS) \
CGO_ENABLED=0 GOARCH=$(ARCH) GOOS=darwin $(GO_EXE) build -v --ldflags "$(LDFLAGS)" \
-o bin/harbor-darwin-$(ARCH) ./cmd/harbor/main.go

.PHONY: clean
Expand Down
36 changes: 30 additions & 6 deletions cmd/harbor/internal/version/version.go
@@ -1,10 +1,34 @@
package version

import "runtime/debug"

var (
Version = "0.1.0"
GitCommit = ""
)
Version = "0.1.0"
GitCommit = ""
BuildTime = ""
ReleaseChannel = "dev"
GoVersion = ""
OS = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "GOOS" {
return setting.Value
}
}
}

func GetVersion() string {
return Version
}
return ""
}
Arch = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "GOARCH" {
return setting.Value
}
}
}

return ""
}
System = OS() + "/" + Arch()
)
20 changes: 20 additions & 0 deletions cmd/harbor/root/cmd.go
@@ -1,12 +1,31 @@
package root

import (
"fmt"

"github.com/goharbor/harbor-cli/cmd/harbor/internal/version"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
"github.com/goharbor/harbor-cli/pkg/constants"
"github.com/spf13/cobra"
)

// versionCommand creates a new `harbor version` command
func versionCommand() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "get Harbor CLI version",
Long: `Get Harbor CLI version, git commit, go version, build time, release channel, os/arch, etc.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Version: %s\n", version.Version)
fmt.Printf("Go version: %s\n", version.GoVersion)
fmt.Printf("Git commit: %s\n", version.GitCommit)
fmt.Printf("Built: %s\n", version.BuildTime)
fmt.Printf("OS/Arch: %s\n", version.System)
},
}
}

// newGetCommand creates a new `harbor get` command
func newGetCommand() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -84,6 +103,7 @@ func New() *cobra.Command {
}

cmd.AddCommand(
versionCommand(),
LoginCommand(),
newGetCommand(),
newListCommand(),
Expand Down

0 comments on commit 169fbd2

Please sign in to comment.