Skip to content

Commit

Permalink
Add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Nov 30, 2019
1 parent 1d3831f commit 9c4fa8a
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 7 deletions.
40 changes: 35 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,61 @@ IMG ?= controller:latest
export GO111MODULE=on
export GOPROXY=https://proxy.golang.org

SHELL := /bin/bash -o pipefail
VERSION_PACKAGE = github.com/replicatedhq/troubleshoot/pkg/version
VERSION ?=`git describe --tags --dirty`
DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`

GIT_TREE = $(shell git rev-parse --is-inside-work-tree 2>/dev/null)
ifneq "$(GIT_TREE)" ""
define GIT_UPDATE_INDEX_CMD
git update-index --assume-unchanged
endef
define GIT_SHA
`git rev-parse HEAD`
endef
else
define GIT_UPDATE_INDEX_CMD
echo "Not a git repo, skipping git update-index"
endef
define GIT_SHA
""
endef
endif

define LDFLAGS
-ldflags "\
-X ${VERSION_PACKAGE}.version=${VERSION} \
-X ${VERSION_PACKAGE}.gitSHA=${GIT_SHA} \
-X ${VERSION_PACKAGE}.buildTime=${DATE} \
"
endef

all: test manager

.PHONY: ffi
ffi: fmt vet
go build -o bin/troubleshoot.so -buildmode=c-shared ffi/main.go
go build ${LDFLAGS} -o bin/troubleshoot.so -buildmode=c-shared ffi/main.go

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

.PHONY: manager
manager: generate fmt vet
go build -o bin/manager github.com/replicatedhq/troubleshoot/cmd/manager
go build ${LDFLAGS} -o bin/manager github.com/replicatedhq/troubleshoot/cmd/manager

.PHONY: support-bundle
support-bundle: generate fmt vet
go build -o bin/support-bundle github.com/replicatedhq/troubleshoot/cmd/troubleshoot
go build ${LDFLAGS} -o bin/support-bundle github.com/replicatedhq/troubleshoot/cmd/troubleshoot

.PHONY: preflight
preflight: generate fmt vet
go build -o bin/preflight github.com/replicatedhq/troubleshoot/cmd/preflight
go build ${LDFLAGS} -o bin/preflight github.com/replicatedhq/troubleshoot/cmd/preflight

.PHONY: analyze
analyze: generate fmt vet
go build -o bin/analyze github.com/replicatedhq/troubleshoot/cmd/analyze
go build ${LDFLAGS} -o bin/analyze github.com/replicatedhq/troubleshoot/cmd/analyze

.PHONY: run
run: generate fmt vet
Expand Down
3 changes: 3 additions & 0 deletions cmd/preflight/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "preflight [url]",
Args: cobra.MinimumNArgs(1),
Short: "Run and retrieve preflight checks in a cluster",
Long: `A preflight check is a set of validations that can and should be run to ensure
that a cluster meets the requirements to run an application.`,
Expand All @@ -37,6 +38,8 @@ that a cluster meets the requirements to run an application.`,

cobra.OnInitialize(initConfig)

cmd.AddCommand(VersionCmd())

cmd.Flags().Bool("interactive", true, "interactive preflights")
cmd.Flags().String("format", "human", "output format, one of human, json, yaml. only used when interactive is set to false")
cmd.Flags().String("preflight", "", "name of the preflight to use")
Expand Down
23 changes: 23 additions & 0 deletions cmd/preflight/cli/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/replicatedhq/troubleshoot/pkg/version"
)

func VersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the current version and exit",
Long: `Print the current version and exit`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Replicated Preflight %s\n", version.Version())

return nil
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/troubleshoot/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ from a server that can be used to assist when troubleshooting a server.`,
cobra.OnInitialize(initConfig)

cmd.AddCommand(Analyze())
cmd.AddCommand(VersionCmd())

cmd.Flags().String("collectors", "", "name of the collectors to use")
cmd.Flags().String("image", "", "the full name of the collector image to use")
Expand Down
23 changes: 21 additions & 2 deletions cmd/troubleshoot/cli/version.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
package cli

import (
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"gopkg.in/yaml.v2"
"fmt"

"io/ioutil"
"path/filepath"

troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1"
"github.com/replicatedhq/troubleshoot/pkg/version"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

func VersionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the current version and exit",
Long: `Print the current version and exit`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Replicated Preflight %s\n", version.Version())

return nil
},
}
return cmd
}

const VersionFilename = "version.yaml"

func writeVersionFile(path string) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/version/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package version

// NOTE: these variables are injected at build time

var (
version, gitSHA, buildTime string
)
12 changes: 12 additions & 0 deletions pkg/version/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package version

import (
"time"
)

var RunAt time.Time

func init() {
RunAt = time.Now().UTC()
initBuild()
}
72 changes: 72 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package version

import (
"runtime"
"time"
)

var (
build Build
)

// Build holds details about this build of the binary
type Build struct {
Version string `json:"version,omitempty"`
GitSHA string `json:"git,omitempty"`
BuildTime time.Time `json:"buildTime,omitempty"`
TimeFallback string `json:"buildTimeFallback,omitempty"`
GoInfo GoInfo `json:"go,omitempty"`
RunAt *time.Time `json:"runAt,omitempty"`
}

type GoInfo struct {
Version string `json:"version,omitempty"`
Compiler string `json:"compiler,omitempty"`
OS string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
}

// initBuild sets up the version info from build args
func initBuild() {
build.Version = version
if len(gitSHA) >= 7 {
build.GitSHA = gitSHA[:7]
}
var err error
build.BuildTime, err = time.Parse(time.RFC3339, buildTime)
if err != nil {
build.TimeFallback = buildTime
}

build.GoInfo = getGoInfo()
build.RunAt = &RunAt
}

// GetBuild gets the build
func GetBuild() Build {
return build
}

// Version gets the version
func Version() string {
return build.Version
}

// GitSHA gets the gitsha
func GitSHA() string {
return build.GitSHA
}

// BuildTime gets the build time
func BuildTime() time.Time {
return build.BuildTime
}

func getGoInfo() GoInfo {
return GoInfo{
Version: runtime.Version(),
Compiler: runtime.Compiler,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}
}

0 comments on commit 9c4fa8a

Please sign in to comment.