From a0e4bf133cbac339324e230b2a67dc59700319ca Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 8 Mar 2021 22:13:05 -0500 Subject: [PATCH 1/5] chore: setup build-info package and env variables in Makefile --- Makefile | 25 ++++++++++++++++++++++ internal/build-info/build.go | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 internal/build-info/build.go diff --git a/Makefile b/Makefile index 6209c685f..8def7d69c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,30 @@ #!/usr/bin/env make +# setup variables +NAME := auth0-cli +PKG := github.com/auth0/$(NAME) +BUILDINFOPKG := $(PKG)/internal/build-info + +## setup variables for build-info +BUILDUSER := $(shell whoami) +BUILDTIME := $(shell date -u '+%Y-%m-%d %H:%M:%S') +VERSION := $(GITCOMMIT) +#BUILDVERSION := $(shell git describe --exact-match --abbrev=0) +GITCOMMIT := $(shell git rev-parse --short HEAD) + +GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) +ifneq ($(GITUNTRACKEDCHANGES),) + GITCOMMIT := $(GITCOMMIT)-dirty +endif + +GITBRANCH ?= $(shell git rev-parse --verify --abbrev-ref HEAD) +CTIMEVAR = -X '$(BUILDINFOPKG).Version=$(VERSION)' \ + -X '$(BUILDINFOPKG).Revision=$(GITCOMMIT)' \ + -X '$(BUILDINFOPKG).Branch=$(GITBRANCH)' \ + -X '$(BUILDINFOPKG).BuildUser=$(BUILDUSER)' \ + -X '$(BUILDINFOPKG).BuildDate=$(BUILDTIME)' + + generate: go generate ./... .PHONY: generate diff --git a/internal/build-info/build.go b/internal/build-info/build.go new file mode 100644 index 000000000..2a31f38ba --- /dev/null +++ b/internal/build-info/build.go @@ -0,0 +1,40 @@ +package buildinfo + +import ( + "runtime" +) + +var ( + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion = runtime.Version() +) + +type BuildInfo struct { + Version string + Revision string + Branch string + BuildUser string + BuildDate string + GoVersion string +} + +// NewDefaultBuildInfo returns the build information obtained from ldflags +func NewDefaultBuildInfo() BuildInfo { + return NewBuildInfo(Version, Branch, BuildDate, BuildUser, GoVersion, Revision) +} + +// NewBuildInfo returns an object with the build information +func NewBuildInfo(version, branch, buildDate, buildUser, goVersion, revision string) BuildInfo { + return BuildInfo{ + Version: version, + Branch: branch, + BuildDate: buildDate, + BuildUser: buildUser, + GoVersion: goVersion, + Revision: revision, + } +} From dc2d6a0bd5536ad7434f1d43b71274b8c87f71c5 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Mon, 8 Mar 2021 23:06:37 -0500 Subject: [PATCH 2/5] chore: setup ldflags --- Makefile | 12 +++++------- internal/build-info/{build.go => build_info.go} | 0 internal/cli/root.go | 2 ++ 3 files changed, 7 insertions(+), 7 deletions(-) rename internal/build-info/{build.go => build_info.go} (100%) diff --git a/Makefile b/Makefile index 8def7d69c..663c60428 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,7 @@ BUILDINFOPKG := $(PKG)/internal/build-info ## setup variables for build-info BUILDUSER := $(shell whoami) BUILDTIME := $(shell date -u '+%Y-%m-%d %H:%M:%S') -VERSION := $(GITCOMMIT) -#BUILDVERSION := $(shell git describe --exact-match --abbrev=0) +VERSION := $(shell git describe --abbrev=0) GITCOMMIT := $(shell git rev-parse --short HEAD) GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) @@ -24,7 +23,6 @@ CTIMEVAR = -X '$(BUILDINFOPKG).Version=$(VERSION)' \ -X '$(BUILDINFOPKG).BuildUser=$(BUILDUSER)' \ -X '$(BUILDINFOPKG).BuildDate=$(BUILDTIME)' - generate: go generate ./... .PHONY: generate @@ -39,7 +37,7 @@ lint: # Build for the native platform build: - go build -o auth0 cmd/auth0/main.go + go build -ldflags "$(CTIMEVAR)" -o auth0 cmd/auth0/main.go .PHONY: build # Build for the native platform @@ -48,9 +46,9 @@ build: # Build a beta version of stripe for all supported platforms build-all-platforms: - env GOOS=darwin go build -o auth0-darwin cmd/auth0/main.go - env GOOS=linux go build -o auth0-linux cmd/auth0/main.go - env GOOS=windows go build -o auth0-windows.exe cmd/auth0/main.go + env GOOS=darwin go build -ldflags "$(CTIMEVAR)" -o auth0-darwin cmd/auth0/main.go + env GOOS=linux go build -ldflags "$(CTIMEVAR)" -o auth0-linux cmd/auth0/main.go + env GOOS=windows go build -ldflags "$(CTIMEVAR)" -o auth0-windows.exe cmd/auth0/main.go .PHONY: build-all-platforms # Run all the tests and code checks diff --git a/internal/build-info/build.go b/internal/build-info/build_info.go similarity index 100% rename from internal/build-info/build.go rename to internal/build-info/build_info.go diff --git a/internal/cli/root.go b/internal/cli/root.go index 9c3292e17..97cb6a2a4 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -5,6 +5,7 @@ import ( "os" "github.com/auth0/auth0-cli/internal/ansi" + "github.com/auth0/auth0-cli/internal/build-info" "github.com/auth0/auth0-cli/internal/display" "github.com/spf13/cobra" ) @@ -26,6 +27,7 @@ func Execute() { SilenceErrors: true, Short: "Supercharge your development workflow.", Long: "Supercharge your development workflow.\n" + getLogin(cli), + Version: buildinfo.Version, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // If the user is trying to login, no need to go From fa0ac1bb75c518bf7199a3ad2ab1f8897b9ca841 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Tue, 9 Mar 2021 00:02:33 -0500 Subject: [PATCH 3/5] feat: add version to user-agent --- internal/cli/cli.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 8ef322dc7..47a3cb31b 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -17,6 +17,7 @@ import ( "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth" "github.com/auth0/auth0-cli/internal/auth0" + "github.com/auth0/auth0-cli/internal/build-info" "github.com/auth0/auth0-cli/internal/display" "github.com/lestrrat-go/jwx/jwt" "github.com/spf13/cobra" @@ -156,7 +157,7 @@ func (c *cli) setup(ctx context.Context) error { if t.AccessToken != "" { m, err := management.New(t.Domain, management.WithStaticToken(t.AccessToken), - management.WithUserAgent(userAgent)) + management.WithUserAgent(fmt.Sprintf("%v/%v", userAgent, strings.TrimPrefix(buildinfo.Version, "v")))) if err != nil { return err } From 651c0255bac734cd0cef725f709a8a74c1884eb3 Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Tue, 9 Mar 2021 00:16:43 -0500 Subject: [PATCH 4/5] chore: add git commit to version output --- internal/build-info/build_info.go | 5 +++++ internal/cli/root.go | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/build-info/build_info.go b/internal/build-info/build_info.go index 2a31f38ba..5227849de 100644 --- a/internal/build-info/build_info.go +++ b/internal/build-info/build_info.go @@ -1,6 +1,7 @@ package buildinfo import ( + "fmt" "runtime" ) @@ -38,3 +39,7 @@ func NewBuildInfo(version, branch, buildDate, buildUser, goVersion, revision str Revision: revision, } } + +func GetVersionWithCommit() string { + return fmt.Sprintf("%v %v", Version, Revision) +} diff --git a/internal/cli/root.go b/internal/cli/root.go index 97cb6a2a4..9003656b3 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -2,12 +2,11 @@ package cli import ( "context" - "os" - "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/build-info" "github.com/auth0/auth0-cli/internal/display" "github.com/spf13/cobra" + "os" ) // Execute is the primary entrypoint of the CLI app. @@ -27,7 +26,7 @@ func Execute() { SilenceErrors: true, Short: "Supercharge your development workflow.", Long: "Supercharge your development workflow.\n" + getLogin(cli), - Version: buildinfo.Version, + Version: buildinfo.GetVersionWithCommit(), PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // If the user is trying to login, no need to go From f52e3f70c7844028176b19ba948fed4c954c836a Mon Sep 17 00:00:00 2001 From: Prasanth Vaaheeswaran Date: Tue, 9 Mar 2021 09:56:31 -0500 Subject: [PATCH 5/5] chore: rename us to auth0-cli, sorry stripe is taken --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 663c60428..431d73d7d 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ build: build: .PHONY: build -# Build a beta version of stripe for all supported platforms +# Build a beta version of auth0-cli for all supported platforms build-all-platforms: env GOOS=darwin go build -ldflags "$(CTIMEVAR)" -o auth0-darwin cmd/auth0/main.go env GOOS=linux go build -ldflags "$(CTIMEVAR)" -o auth0-linux cmd/auth0/main.go