Skip to content

Commit f0d0c65

Browse files
author
Kristian
authored
Merge pull request #6 from itsdalmo/add_version_flag
Suggestion for version flag in binary.
2 parents b49a0e7 + dc7a498 commit f0d0c65

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ TARGET ?= darwin
33
ARCH ?= amd64
44
EXT ?= ""
55
DOCKER_REPO=itsdalmo/ssm-sh
6+
TRAVIS_TAG ?= ref-$(shell git rev-parse --short HEAD)
7+
LDFLAGS=-ldflags "-X=main.version=$(TRAVIS_TAG)"
68
SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")
79

810
default: test
911

1012
run: test
1113
@echo "== Run =="
12-
go run main.go
14+
go run $(LDFLAGS) main.go
1315

1416
build: test
1517
@echo "== Build =="
16-
go build -o $(BINARY_NAME) -v
18+
go build -o $(BINARY_NAME) -v $(LDFLAGS)
1719

1820
test:
1921
@echo "== Test =="
@@ -40,6 +42,6 @@ build-docker:
4042

4143
build-release:
4244
@echo "== Release build =="
43-
CGO_ENABLED=0 GOOS=$(TARGET) GOARCH=$(ARCH) go build -o $(BINARY_NAME)-$(TARGET)-$(ARCH)$(EXT) -v
45+
CGO_ENABLED=0 GOOS=$(TARGET) GOARCH=$(ARCH) go build $(LDFLAGS) -o $(BINARY_NAME)-$(TARGET)-$(ARCH)$(EXT) -v
4446

4547
.PHONY: default build test build-docker run-docker build-release

command/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
var Command RootCommand
44

55
type RootCommand struct {
6+
Version func() `short:"v" long:"version" description:"Print the version and exit."`
67
List ListCommand `command:"list" alias:"ls" description:"List managed instances."`
78
Shell ShellCommand `command:"shell" alias:"sh" description:"Start an interactive shell."`
89
Run RunCommand `command:"run" description:"Run a command on the targeted instances."`

command/version.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// CommandVersion (overridden in main.go)
9+
var CommandVersion = "unknown"
10+
11+
func init() {
12+
Command.Version = func() {
13+
fmt.Println(CommandVersion)
14+
os.Exit(0)
15+
}
16+
}

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import (
66
"os"
77
)
88

9+
// Version is set on build by the Git release tag.
10+
var version string
11+
912
func main() {
13+
command.CommandVersion = version
1014
_, err := flags.Parse(&command.Command)
1115
if err != nil {
1216
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {

0 commit comments

Comments
 (0)