Skip to content

Commit

Permalink
Merge pull request #37 from go-ksplit/laverya/version-command
Browse files Browse the repository at this point in the history
add version command
  • Loading branch information
laverya committed May 5, 2021
2 parents 630edfc + 550545e commit 15b102c
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 7 deletions.
33 changes: 32 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
SHELL := /bin/bash -o pipefail
SRC = $(shell find . -name "*.go")

VERSION ?=`git describe --tags`
DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
VERSION_PACKAGE = github.com/go-ksplit/ksplit/pkg/version
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

test:
GO111MODULE=on go test --race -v ./...

Expand All @@ -16,4 +44,7 @@ build: bin/ksplit

bin/ksplit: $(SRC) go.mod go.sum
@mkdir -p bin/
go build -o bin/ksplit ./ksplit
go build \
-o bin/ksplit \
${LDFLAGS} \
./ksplit
4 changes: 2 additions & 2 deletions ksplit/cmd/allsplit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/go-ksplit/ksplit/pkg"
"github.com/go-ksplit/ksplit/pkg/splitter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -23,7 +23,7 @@ func AllSplitCmd() *cobra.Command {
return errors.New("Please supply a directory")
}

err := pkg.MaybeSplitMultidocYamlFs(args[0])
err := splitter.MaybeSplitMultidocYamlFs(args[0])
if err != nil {
return errors.Wrap(err, "allsplit cmd")
}
Expand Down
4 changes: 2 additions & 2 deletions ksplit/cmd/crdsplit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"github.com/go-ksplit/ksplit/pkg"
"github.com/go-ksplit/ksplit/pkg/splitter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -23,7 +23,7 @@ func CrdSplitCmd() *cobra.Command {
return errors.New("Please supply a directory")
}

err := pkg.MaybeSplitCRDsFs(args[0])
err := splitter.MaybeSplitCRDsFs(args[0])
if err != nil {
return errors.Wrap(err, "crdsplit cmd")
}
Expand Down
36 changes: 36 additions & 0 deletions ksplit/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"
"time"

"github.com/go-ksplit/ksplit/pkg/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Version() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "ksplit version information",
Long: `Prints the current version of ksplit`,
SilenceErrors: true,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()
if v.GetBool("verbose") {
fmt.Printf("ksplit %s built at %s with sha %s\n", version.Version(), version.BuildTime().Format(time.RFC3339), version.GitSHA())
} else {
fmt.Printf("ksplit %s\n", version.Version())
}

return nil
},
}

cmd.Flags().Bool("verbose", false, "when set, also print build info")

return cmd
}
1 change: 1 addition & 0 deletions ksplit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func RootCmd() *cobra.Command {

cmd.AddCommand(cmd2.CrdSplitCmd())
cmd.AddCommand(cmd2.AllSplitCmd())
cmd.AddCommand(cmd2.Version())

_ = viper.BindPFlags(cmd.Flags())
_ = viper.BindPFlags(cmd.PersistentFlags())
Expand Down
2 changes: 1 addition & 1 deletion pkg/ksplit.go → pkg/splitter/ksplit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package splitter

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/ksplit_test.go → pkg/splitter/ksplit_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package splitter

import (
"os"
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
)
64 changes: 64 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package version

import (
"time"
)

var (
build Build
hasBuilt = false
)

// Build holds details about this build of the Ship 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"`
}

// Init sets up the version info from build args
func Init() {
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
}
hasBuilt = true
}

// GetBuild gets the build
func GetBuild() Build {
if !hasBuilt {
Init()
}
return build
}

// Version gets the version
func Version() string {
if !hasBuilt {
Init()
}
return build.Version
}

// GitSHA gets the gitsha
func GitSHA() string {
if !hasBuilt {
Init()
}
return build.GitSHA
}

// BuildTime gets the build time
func BuildTime() time.Time {
if !hasBuilt {
Init()
}
return build.BuildTime
}

0 comments on commit 15b102c

Please sign in to comment.