Skip to content

Commit

Permalink
cli: make the commands actually work (#11)
Browse files Browse the repository at this point in the history
* 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
desimone committed Nov 29, 2021
1 parent c78823c commit 3d9dfa6
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 634 deletions.
54 changes: 46 additions & 8 deletions Makefile
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.
4 changes: 3 additions & 1 deletion cmd/main.go → cmd/pomerium-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/pomerium/cli/version"
"github.com/pomerium/pomerium/pkg/cryptutil"
)

var rootCmd = &cobra.Command{
Use: "pomerium-cli",
Use: "pomerium-cli",
Version: version.FullVersion(),
}

func main() {
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions cmd/pomerium-cli/version.go
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())
},
}
625 changes: 0 additions & 625 deletions go.sum

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions version/version.go
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)
}
72 changes: 72 additions & 0 deletions version/version_test.go
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)
}
})
}
}

0 comments on commit 3d9dfa6

Please sign in to comment.