Skip to content

Commit

Permalink
feat: add version command (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
particledecay authored Apr 27, 2020
1 parent 91c9220 commit 1997788
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ builds:
- binary: kconf
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X github.com/particledecay/kconf/build.Version={{ .Version }} -X github.com/particledecay/kconf/build.Commit={{ .ShortCommit }} -X github.com/particledecay/kconf/build.Date={{ .Date }}
archives:
- replacements:
darwin: Darwin
Expand Down
59 changes: 59 additions & 0 deletions build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package build

import (
"bytes"
"fmt"
"text/template"
)

var (
// Version holds the tag of the build
Version = ""
// Commit holds the git commit sha
Commit = ""
// Date is the build date
Date = ""
)

const versionTpl = `
Version: v{{ .Version }}
SHA: {{ .Commit }}
Built On: {{ .Date }}`

// PrintVersion outputs the short version info
func PrintVersion() {
if Version != "" {
fmt.Printf("v%s\n", Version)
}
}

// PrintLongVersion outputs the full version info
func PrintLongVersion() error {
if Version == "" {
return nil
}

data := struct {
Version string
Commit string
Date string
}{
Version: Version,
Commit: Commit,
Date: Date,
}

var tpl bytes.Buffer

t, err := template.New("build").Parse(versionTpl)
if err != nil {
return err
}

if err := t.Execute(&tpl, data); err != nil {
return err
}

fmt.Println(tpl.String())
return nil
}
13 changes: 13 additions & 0 deletions build/version_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package build

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestKubeconfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Build Suite")
}
100 changes: 100 additions & 0 deletions build/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package build

import (
"io/ioutil"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Build/PrintVersion", func() {
It("Should print nothing if a version is not set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
Version = ""
PrintVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(out).To(BeEmpty())
})

It("Should print a version if it was set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
Version = "1.2.3"
PrintVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(string(out)).To(Equal("v1.2.3\n"))
})
})

var _ = Describe("Build/PrintLongVersion", func() {
It("Should print nothing if a version is not set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
Version = ""
PrintLongVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(out).To(BeEmpty())
})

It("Should print a version if it was set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
Version = "1.2.3"
Commit = "abcdef1234"
Date = "20200101"
PrintLongVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(string(out)).To(ContainSubstring("Version:"))
Expect(string(out)).To(ContainSubstring("v1.2.3"))
Expect(string(out)).To(ContainSubstring("SHA:"))
Expect(string(out)).To(ContainSubstring("abcdef1234"))
Expect(string(out)).To(ContainSubstring("Built On:"))
Expect(string(out)).To(ContainSubstring("20200101"))
})
})
19 changes: 19 additions & 0 deletions cmd/kconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

"github.com/particledecay/kconf/build"
"github.com/particledecay/kconf/pkg/kubeconfig"
)

Expand Down Expand Up @@ -198,6 +199,23 @@ var useCmd = &cobra.Command{
},
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version",
Long: `Print version information`,
Aliases: []string{"ver"},
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if verbose == true {
if err := build.PrintLongVersion(); err != nil {
log.Error().Msgf("%v", err)
}
} else {
build.PrintVersion()
}
},
}

func init() {
// flags
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "display debug messages")
Expand All @@ -211,6 +229,7 @@ func Execute() {
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(viewCmd)
rootCmd.AddCommand(useCmd)
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal().Msgf("Error during execution: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -227,8 +228,6 @@ k8s.io/apimachinery v0.17.0 h1:xRBnuie9rXcPxUkDizUsGvPf1cnlZCFu210op7J7LJo=
k8s.io/apimachinery v0.17.0/go.mod h1:b9qmWdKlLuU9EBh+06BtLcSf/Mu89rWL33naRxs1uZg=
k8s.io/client-go v0.17.0 h1:8QOGvUGdqDMFrm9sD6IUFl256BcffynGoe80sxgTEDg=
k8s.io/client-go v0.17.0/go.mod h1:TYgR6EUHs6k45hb6KWjVD6jFZvJV4gHDikv/It0xz+k=
k8s.io/client-go v1.5.1 h1:XaX/lo2/u3/pmFau8HN+sB5C/b4dc4Dmm2eXjBH4p1E=
k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
Expand Down

0 comments on commit 1997788

Please sign in to comment.