Skip to content

Commit 161a57c

Browse files
committed
Add version command
1 parent 9419d5d commit 161a57c

File tree

15 files changed

+63
-23
lines changed

15 files changed

+63
-23
lines changed

Makefile

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1+
OUT_DIR := ./out
2+
3+
PBCLI_BUILD_VERSION ?= $(shell git describe --tags)
4+
ifeq ($(PBCLI_BUILD_VERSION),)
5+
_ := $(error Cannot determine build version)
6+
endif
7+
8+
BUILD_VERSION_FLAG := github.com/pushbits/cli/internal/buildconfig.Version=$(PBCLI_BUILD_VERSION)
9+
110
.PHONY: build
211
build:
3-
mkdir -p ./out
4-
go build -ldflags="-w -s" -o ./out/pbcli ./cmd/pbcli
12+
mkdir -p $(OUT_DIR)
13+
go build -ldflags "-w -s -X $(BUILD_VERSION_FLAG)" -o $(OUT_DIR)/pbcli ./cmd/pbcli
14+
15+
.PHONY: clean
16+
clean:
17+
rm -rf $(OUT_DIR)
518

619
.PHONY: test
720
test:
8-
stdout=$$(gofmt -l . 2>&1); \
9-
if [ "$$stdout" ]; then \
10-
exit 1; \
11-
fi
21+
stdout=$$(gofumpt -l . 2>&1); if [ "$$stdout" ]; then exit 1; fi
1222
go vet ./...
13-
gocyclo -over 10 $(shell find . -iname '*.go' -type f)
23+
gocyclo -over 10 $(shell find . -type f -iname '*.go')
1424
staticcheck ./...
1525
go test -v -cover ./...
26+
@printf '\n%s\n' "> Test successful"
1627

1728
.PHONY: setup
1829
setup:
1930
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
2031
go install honnef.co/go/tools/cmd/staticcheck@latest
32+
go install mvdan.cc/gofumpt@latest
33+
34+
.PHONY: fmt
35+
fmt:
36+
gofumpt -l -w .

cmd/pbcli/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import (
66
"github.com/jessevdk/go-flags"
77

88
"github.com/pushbits/cli/internal/application"
9+
"github.com/pushbits/cli/internal/commands"
910
"github.com/pushbits/cli/internal/settings"
1011
"github.com/pushbits/cli/internal/ui"
1112
"github.com/pushbits/cli/internal/user"
1213
)
1314

14-
type commands struct {
15+
type options struct {
1516
settings.Settings
16-
Application application.Command `command:"application" alias:"a" description:"Configure applications"`
17-
User user.Command `command:"user" alias:"u" description:"Configure users"`
17+
Application application.Command `command:"application" alias:"a" description:"Configure applications"`
18+
User user.Command `command:"user" alias:"u" description:"Configure users"`
19+
Version commands.VersionCommand `command:"version" alias:"v" description:"Print the program version"`
1820
}
1921

2022
var (
21-
cmds commands
23+
cmds options
2224
parser = flags.NewParser(&cmds, flags.Default)
2325
)
2426

@@ -28,7 +30,7 @@ func main() {
2830
os.Exit(1)
2931
}
3032

31-
s := settings.Settings{
33+
s := &settings.Settings{
3234
URL: cmds.URL,
3335
Username: cmds.Username,
3436
}

internal/application/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *createCommand) Execute(args []string) error {
2424
return nil
2525
}
2626

27-
func (c *createCommand) Run(s settings.Settings, password string) {
27+
func (c *createCommand) Run(s *settings.Settings, password string) {
2828
data := map[string]interface{}{
2929
"name": c.Arguments.Name,
3030
"strict_compatibility": c.StrictCompatibility,

internal/application/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *deleteCommand) Execute(args []string) error {
2424
return nil
2525
}
2626

27-
func (c *deleteCommand) Run(s settings.Settings, password string) {
27+
func (c *deleteCommand) Run(s *settings.Settings, password string) {
2828
populatedEndpoint := fmt.Sprintf(deleteEndpoint, c.Arguments.ID)
2929

3030
resp, err := api.Delete(s.URL, populatedEndpoint, s.Username, password)

internal/application/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (c *listCommand) Execute(args []string) error {
2020
return nil
2121
}
2222

23-
func (c *listCommand) Run(s settings.Settings, password string) {
23+
func (c *listCommand) Run(s *settings.Settings, password string) {
2424
resp, err := api.Get(s.URL, listEndpoint, s.Username, password)
2525
if err != nil {
2626
log.Fatal(err)

internal/application/show.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *showCommand) Execute(args []string) error {
2424
return nil
2525
}
2626

27-
func (c *showCommand) Run(s settings.Settings, password string) {
27+
func (c *showCommand) Run(s *settings.Settings, password string) {
2828
populatedEndpoint := fmt.Sprintf(showEndpoint, c.Arguments.ID)
2929

3030
resp, err := api.Get(s.URL, populatedEndpoint, s.Username, password)

internal/application/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *updateCommand) Execute(args []string) error {
2727
return nil
2828
}
2929

30-
func (c *updateCommand) Run(s settings.Settings, password string) {
30+
func (c *updateCommand) Run(s *settings.Settings, password string) {
3131
if !c.RefreshToken && c.StrictCompatibility {
3232
log.Fatal("Can only enforce compatibility when refreshing the token of the application")
3333
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package buildconfig
2+
3+
var Version string

internal/commands/version.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/pushbits/cli/internal/buildconfig"
7+
"github.com/pushbits/cli/internal/settings"
8+
)
9+
10+
type VersionCommand struct{}
11+
12+
func (c *VersionCommand) Execute(args []string) error {
13+
settings.Runner = c
14+
return nil
15+
}
16+
17+
func (c *VersionCommand) Run(s *settings.Settings, password string) {
18+
fmt.Printf("pbcli %s\n", buildconfig.Version)
19+
}

internal/settings/mod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Settings struct {
88

99
// Runnable defines an interface for subcommands that take the global settings and a password.
1010
type Runnable interface {
11-
Run(Settings, string)
11+
Run(*Settings, string)
1212
}
1313

1414
// Runner is the subcommand to run after all arguments were parsed.

0 commit comments

Comments
 (0)