Skip to content

Commit

Permalink
Merge pull request #143 from auth0/cli-39
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Mar 10, 2021
2 parents 666d061 + f52e3f7 commit c95e71a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
33 changes: 28 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#!/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 := $(shell git describe --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
Expand All @@ -14,18 +37,18 @@ 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
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 -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
Expand Down
45 changes: 45 additions & 0 deletions internal/build-info/build_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package buildinfo

import (
"fmt"
"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,
}
}

func GetVersionWithCommit() string {
return fmt.Sprintf("%v %v", Version, Revision)
}
3 changes: 2 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +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.
Expand All @@ -26,6 +26,7 @@ func Execute() {
SilenceErrors: true,
Short: "Supercharge your development workflow.",
Long: "Supercharge your development workflow.\n" + getLogin(cli),
Version: buildinfo.GetVersionWithCommit(),

PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// If the user is trying to login, no need to go
Expand Down

0 comments on commit c95e71a

Please sign in to comment.