-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: make the commands actually work (#11)
* cli: make the commands actually work Signed-off-by: Bobby DeSimone <[email protected]> * Update cmd/pomerium-cli/main.go * update sum * fix * precommit
- Loading branch information
Showing
10 changed files
with
186 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,57 @@ | ||
PREFIX?=$(shell pwd) | ||
NAME := pomerium-cli | ||
PKG := github.com/pomerium/cli | ||
|
||
|
||
BUILDDIR := ${PREFIX}/dist | ||
BINDIR := ${PREFIX}/bin | ||
|
||
GITCOMMIT := $(shell git rev-parse --short HEAD) | ||
BUILDMETA:= | ||
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) | ||
ifneq ($(GITUNTRACKEDCHANGES),) | ||
BUILDMETA := dirty | ||
endif | ||
|
||
CTIMEVAR=-X $(PKG)/version.GitCommit=$(GITCOMMIT) \ | ||
-X $(PKG)/version.BuildMeta=$(BUILDMETA) \ | ||
-X $(PKG)/version.ProjectName=$(NAME) \ | ||
-X $(PKG)/version.ProjectURL=$(PKG) | ||
|
||
GO ?= "go" | ||
GO_LDFLAGS=-ldflags "-s -w $(CTIMEVAR)" | ||
GOOSARCHES = linux/amd64 darwin/amd64 windows/amd64 | ||
|
||
|
||
.PHONY: test | ||
test: | ||
test: ## test everything | ||
go test ./... | ||
|
||
.PHONY: lint | ||
lint: | ||
lint: ## run go mod tidy | ||
go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./... | ||
|
||
.PHONY: tidy | ||
tidy: | ||
tidy: ## run go mod tidy | ||
go mod tidy -compat=1.17 | ||
|
||
.PHONY: tools | ||
go install -u google.golang.org/protobuf/cmd/protoc-gen-go | ||
go install -u google.golang.org/grpc/cmd/protoc-gen-go-grpc | ||
|
||
.PHONY: proto | ||
proto: tools | ||
tools: ## generate protobuff files | ||
go install google.golang.org/protobuf/cmd/protoc-gen-go | ||
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc | ||
buf generate | ||
|
||
.PHONY: clean | ||
clean: ## Cleanup any build binaries or packages. | ||
@echo "==> $@" | ||
$(RM) -r $(BINDIR) | ||
$(RM) -r $(BUILDDIR) | ||
|
||
.PHONY: build | ||
build: ## Build everything. | ||
@echo "==> $@" | ||
@CGO_ENABLED=0 GO111MODULE=on go build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(BINDIR)/$(NAME) ./cmd/"$(NAME)" | ||
|
||
.PHONY: help | ||
help: | ||
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/pomerium/cli/version" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(versionCmd) | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "version", | ||
Long: `Print the cli version.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("pomerium:", version.FullVersion()) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Package version enables setting build-time version using ldflags. | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// ProjectName is the canonical project name set by ldl flags | ||
ProjectName = "" | ||
// ProjectURL is the canonical project url set by ldl flags | ||
ProjectURL = "" | ||
// Version specifies Semantic versioning increment (MAJOR.MINOR.PATCH). | ||
Version = "v0.0.0" | ||
// GitCommit specifies the git commit sha, set by the compiler. | ||
GitCommit = "" | ||
// BuildMeta specifies release type (dev,rc1,beta,etc) | ||
BuildMeta = "" | ||
|
||
runtimeVersion = runtime.Version() | ||
) | ||
|
||
// FullVersion returns a version string. | ||
func FullVersion() string { | ||
var sb strings.Builder | ||
sb.Grow(len(Version) + len(GitCommit) + len(BuildMeta) + len("-") + len("+")) | ||
sb.WriteString(Version) | ||
if BuildMeta != "" { | ||
sb.WriteString("-" + BuildMeta) | ||
} | ||
if GitCommit != "" { | ||
sb.WriteString("+" + GitCommit) | ||
} | ||
return sb.String() | ||
} | ||
|
||
// UserAgent returns a user-agent string as specified in RFC 2616:14.43 | ||
// https://tools.ietf.org/html/rfc2616 | ||
func UserAgent() string { | ||
return fmt.Sprintf("%s/%s (+%s; %s; %s)", ProjectName, Version, ProjectURL, GitCommit, runtimeVersion) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package version | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestFullVersionVersion(t *testing.T) { | ||
tests := []struct { | ||
Version string | ||
GitCommit string | ||
BuildMeta string | ||
|
||
expected string | ||
}{ | ||
{"", "", "", ""}, | ||
{"1.0.0", "", "", "1.0.0"}, | ||
{"1.0.0", "314501b", "", "1.0.0+314501b"}, | ||
{"1.0.0", "314501b", "dev", "1.0.0-dev+314501b"}, | ||
} | ||
for _, tt := range tests { | ||
Version = tt.Version | ||
GitCommit = tt.GitCommit | ||
BuildMeta = tt.BuildMeta | ||
|
||
if got := FullVersion(); got != tt.expected { | ||
t.Errorf("expected (%s) got (%s) for Version(%s), GitCommit(%s) BuildMeta(%s)", | ||
tt.expected, | ||
got, | ||
tt.Version, | ||
tt.GitCommit, | ||
tt.BuildMeta) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkFullVersion(b *testing.B) { | ||
Version = "1.0.0" | ||
GitCommit = "314501b" | ||
BuildMeta = "dev" | ||
for i := 0; i < b.N; i++ { | ||
FullVersion() | ||
} | ||
} | ||
|
||
func TestUserAgent(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
Version string | ||
GitCommit string | ||
BuildMeta string | ||
ProjectName string | ||
ProjectURL string | ||
want string | ||
}{ | ||
{"good user agent", "1.0.0", "314501b", "dev", "pomerium", "github.com/pomerium", fmt.Sprintf("pomerium/1.0.0 (+github.com/pomerium; 314501b; %s)", runtime.Version())}, | ||
} | ||
for _, tt := range tests { | ||
Version = tt.Version | ||
GitCommit = tt.GitCommit | ||
BuildMeta = tt.BuildMeta | ||
ProjectName = tt.ProjectName | ||
ProjectURL = tt.ProjectURL | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
if got := UserAgent(); got != tt.want { | ||
t.Errorf("UserAgent() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |