Skip to content

Commit

Permalink
Merge pull request #6 from itsdalmo/add_version_flag
Browse files Browse the repository at this point in the history
Suggestion for version flag in binary.
  • Loading branch information
Kristian authored Feb 16, 2018
2 parents b49a0e7 + dc7a498 commit f0d0c65
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ TARGET ?= darwin
ARCH ?= amd64
EXT ?= ""
DOCKER_REPO=itsdalmo/ssm-sh
TRAVIS_TAG ?= ref-$(shell git rev-parse --short HEAD)
LDFLAGS=-ldflags "-X=main.version=$(TRAVIS_TAG)"
SRC=$(shell find . -type f -name '*.go' -not -path "./vendor/*")

default: test

run: test
@echo "== Run =="
go run main.go
go run $(LDFLAGS) main.go

build: test
@echo "== Build =="
go build -o $(BINARY_NAME) -v
go build -o $(BINARY_NAME) -v $(LDFLAGS)

test:
@echo "== Test =="
Expand All @@ -40,6 +42,6 @@ build-docker:

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

.PHONY: default build test build-docker run-docker build-release
1 change: 1 addition & 0 deletions command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
var Command RootCommand

type RootCommand struct {
Version func() `short:"v" long:"version" description:"Print the version and exit."`
List ListCommand `command:"list" alias:"ls" description:"List managed instances."`
Shell ShellCommand `command:"shell" alias:"sh" description:"Start an interactive shell."`
Run RunCommand `command:"run" description:"Run a command on the targeted instances."`
Expand Down
16 changes: 16 additions & 0 deletions command/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package command

import (
"fmt"
"os"
)

// CommandVersion (overridden in main.go)
var CommandVersion = "unknown"

func init() {
Command.Version = func() {
fmt.Println(CommandVersion)
os.Exit(0)
}
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
"os"
)

// Version is set on build by the Git release tag.
var version string

func main() {
command.CommandVersion = version
_, err := flags.Parse(&command.Command)
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
Expand Down

0 comments on commit f0d0c65

Please sign in to comment.