Skip to content

Commit 0292824

Browse files
authored
Merge pull request #89 from replicatedhq/version-command
Add version command
2 parents 1d3831f + 9c4fa8a commit 0292824

File tree

8 files changed

+174
-7
lines changed

8 files changed

+174
-7
lines changed

Makefile

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,61 @@ IMG ?= controller:latest
44
export GO111MODULE=on
55
export GOPROXY=https://proxy.golang.org
66

7+
SHELL := /bin/bash -o pipefail
8+
VERSION_PACKAGE = github.com/replicatedhq/troubleshoot/pkg/version
9+
VERSION ?=`git describe --tags --dirty`
10+
DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
11+
12+
GIT_TREE = $(shell git rev-parse --is-inside-work-tree 2>/dev/null)
13+
ifneq "$(GIT_TREE)" ""
14+
define GIT_UPDATE_INDEX_CMD
15+
git update-index --assume-unchanged
16+
endef
17+
define GIT_SHA
18+
`git rev-parse HEAD`
19+
endef
20+
else
21+
define GIT_UPDATE_INDEX_CMD
22+
echo "Not a git repo, skipping git update-index"
23+
endef
24+
define GIT_SHA
25+
""
26+
endef
27+
endif
28+
29+
define LDFLAGS
30+
-ldflags "\
31+
-X ${VERSION_PACKAGE}.version=${VERSION} \
32+
-X ${VERSION_PACKAGE}.gitSHA=${GIT_SHA} \
33+
-X ${VERSION_PACKAGE}.buildTime=${DATE} \
34+
"
35+
endef
36+
737
all: test manager
838

939
.PHONY: ffi
1040
ffi: fmt vet
11-
go build -o bin/troubleshoot.so -buildmode=c-shared ffi/main.go
41+
go build ${LDFLAGS} -o bin/troubleshoot.so -buildmode=c-shared ffi/main.go
1242

1343
# Run tests
1444
test: generate fmt vet manifests
1545
go test ./pkg/... ./cmd/... -coverprofile cover.out
1646

1747
.PHONY: manager
1848
manager: generate fmt vet
19-
go build -o bin/manager github.com/replicatedhq/troubleshoot/cmd/manager
49+
go build ${LDFLAGS} -o bin/manager github.com/replicatedhq/troubleshoot/cmd/manager
2050

2151
.PHONY: support-bundle
2252
support-bundle: generate fmt vet
23-
go build -o bin/support-bundle github.com/replicatedhq/troubleshoot/cmd/troubleshoot
53+
go build ${LDFLAGS} -o bin/support-bundle github.com/replicatedhq/troubleshoot/cmd/troubleshoot
2454

2555
.PHONY: preflight
2656
preflight: generate fmt vet
27-
go build -o bin/preflight github.com/replicatedhq/troubleshoot/cmd/preflight
57+
go build ${LDFLAGS} -o bin/preflight github.com/replicatedhq/troubleshoot/cmd/preflight
2858

2959
.PHONY: analyze
3060
analyze: generate fmt vet
31-
go build -o bin/analyze github.com/replicatedhq/troubleshoot/cmd/analyze
61+
go build ${LDFLAGS} -o bin/analyze github.com/replicatedhq/troubleshoot/cmd/analyze
3262

3363
.PHONY: run
3464
run: generate fmt vet

cmd/preflight/cli/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
func RootCmd() *cobra.Command {
1919
cmd := &cobra.Command{
2020
Use: "preflight [url]",
21+
Args: cobra.MinimumNArgs(1),
2122
Short: "Run and retrieve preflight checks in a cluster",
2223
Long: `A preflight check is a set of validations that can and should be run to ensure
2324
that a cluster meets the requirements to run an application.`,
@@ -37,6 +38,8 @@ that a cluster meets the requirements to run an application.`,
3738

3839
cobra.OnInitialize(initConfig)
3940

41+
cmd.AddCommand(VersionCmd())
42+
4043
cmd.Flags().Bool("interactive", true, "interactive preflights")
4144
cmd.Flags().String("format", "human", "output format, one of human, json, yaml. only used when interactive is set to false")
4245
cmd.Flags().String("preflight", "", "name of the preflight to use")

cmd/preflight/cli/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/replicatedhq/troubleshoot/pkg/version"
9+
)
10+
11+
func VersionCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "version",
14+
Short: "Print the current version and exit",
15+
Long: `Print the current version and exit`,
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
fmt.Printf("Replicated Preflight %s\n", version.Version())
18+
19+
return nil
20+
},
21+
}
22+
return cmd
23+
}

cmd/troubleshoot/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from a server that can be used to assist when troubleshooting a server.`,
4242
cobra.OnInitialize(initConfig)
4343

4444
cmd.AddCommand(Analyze())
45+
cmd.AddCommand(VersionCmd())
4546

4647
cmd.Flags().String("collectors", "", "name of the collectors to use")
4748
cmd.Flags().String("image", "", "the full name of the collector image to use")

cmd/troubleshoot/cli/version.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
package cli
22

33
import (
4-
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
5-
"gopkg.in/yaml.v2"
4+
"fmt"
5+
66
"io/ioutil"
77
"path/filepath"
8+
9+
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
10+
"github.com/replicatedhq/troubleshoot/pkg/version"
11+
"github.com/spf13/cobra"
12+
"gopkg.in/yaml.v2"
813
)
914

15+
func VersionCmd() *cobra.Command {
16+
cmd := &cobra.Command{
17+
Use: "version",
18+
Short: "Print the current version and exit",
19+
Long: `Print the current version and exit`,
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
fmt.Printf("Replicated Preflight %s\n", version.Version())
22+
23+
return nil
24+
},
25+
}
26+
return cmd
27+
}
28+
1029
const VersionFilename = "version.yaml"
1130

1231
func writeVersionFile(path string) error {

pkg/version/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package version
2+
3+
// NOTE: these variables are injected at build time
4+
5+
var (
6+
version, gitSHA, buildTime string
7+
)

pkg/version/run.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package version
2+
3+
import (
4+
"time"
5+
)
6+
7+
var RunAt time.Time
8+
9+
func init() {
10+
RunAt = time.Now().UTC()
11+
initBuild()
12+
}

pkg/version/version.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package version
2+
3+
import (
4+
"runtime"
5+
"time"
6+
)
7+
8+
var (
9+
build Build
10+
)
11+
12+
// Build holds details about this build of the binary
13+
type Build struct {
14+
Version string `json:"version,omitempty"`
15+
GitSHA string `json:"git,omitempty"`
16+
BuildTime time.Time `json:"buildTime,omitempty"`
17+
TimeFallback string `json:"buildTimeFallback,omitempty"`
18+
GoInfo GoInfo `json:"go,omitempty"`
19+
RunAt *time.Time `json:"runAt,omitempty"`
20+
}
21+
22+
type GoInfo struct {
23+
Version string `json:"version,omitempty"`
24+
Compiler string `json:"compiler,omitempty"`
25+
OS string `json:"os,omitempty"`
26+
Arch string `json:"arch,omitempty"`
27+
}
28+
29+
// initBuild sets up the version info from build args
30+
func initBuild() {
31+
build.Version = version
32+
if len(gitSHA) >= 7 {
33+
build.GitSHA = gitSHA[:7]
34+
}
35+
var err error
36+
build.BuildTime, err = time.Parse(time.RFC3339, buildTime)
37+
if err != nil {
38+
build.TimeFallback = buildTime
39+
}
40+
41+
build.GoInfo = getGoInfo()
42+
build.RunAt = &RunAt
43+
}
44+
45+
// GetBuild gets the build
46+
func GetBuild() Build {
47+
return build
48+
}
49+
50+
// Version gets the version
51+
func Version() string {
52+
return build.Version
53+
}
54+
55+
// GitSHA gets the gitsha
56+
func GitSHA() string {
57+
return build.GitSHA
58+
}
59+
60+
// BuildTime gets the build time
61+
func BuildTime() time.Time {
62+
return build.BuildTime
63+
}
64+
65+
func getGoInfo() GoInfo {
66+
return GoInfo{
67+
Version: runtime.Version(),
68+
Compiler: runtime.Compiler,
69+
OS: runtime.GOOS,
70+
Arch: runtime.GOARCH,
71+
}
72+
}

0 commit comments

Comments
 (0)