Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gojek/darkroom/internal/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newVersionCmd() *cobra.Command { | ||
args := struct { | ||
detailed bool | ||
}{} | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "Print version", | ||
Long: `Print version.`, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
buildInfo := version.Build | ||
if args.detailed { | ||
cmd.Println(fmt.Sprintf("Version: %s", buildInfo.Version)) | ||
cmd.Println(fmt.Sprintf("Git Tag: %s", buildInfo.GitTag)) | ||
cmd.Println(fmt.Sprintf("Git Commit: %s", buildInfo.GitCommit)) | ||
cmd.Println(fmt.Sprintf("Build Date: %s", buildInfo.BuildDate)) | ||
} else { | ||
cmd.Println(buildInfo.Version) | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.PersistentFlags().BoolVarP(&args.detailed, "detailed", "a", false, "Print detailed version") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"github.com/gojek/darkroom/internal/version" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/suite" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type VersionCmdSuite struct { | ||
suite.Suite | ||
rootCmd *cobra.Command | ||
buf *bytes.Buffer | ||
} | ||
|
||
func TestVersionCmd(t *testing.T) { | ||
suite.Run(t, new(VersionCmdSuite)) | ||
} | ||
|
||
func (s *VersionCmdSuite) SetupSuite() { | ||
version.Build = version.BuildInfo{ | ||
Version: "0.1.0", | ||
GitTag: "v0.1.0", | ||
GitCommit: "c910e75b573b48961c7dcc1dd1063a543164d963", | ||
BuildDate: "2020-03-03T10:59:06Z", | ||
} | ||
} | ||
|
||
func (s *VersionCmdSuite) SetupTest() { | ||
s.rootCmd = &cobra.Command{ | ||
Use: "app", | ||
} | ||
s.rootCmd.AddCommand(newVersionCmd()) | ||
s.buf = &bytes.Buffer{} | ||
s.rootCmd.SetOut(s.buf) | ||
} | ||
|
||
func (s *VersionCmdSuite) TestVersionOutput() { | ||
// given | ||
s.rootCmd.SetArgs([]string{"version"}) | ||
// when | ||
err := s.rootCmd.Execute() | ||
// then | ||
s.NoError(err) | ||
// and | ||
s.Equal(strings.TrimSpace(`0.1.0`), strings.TrimSpace(s.buf.String())) | ||
} | ||
|
||
func (s *VersionCmdSuite) TestVersionDetailedOutput() { | ||
// given | ||
s.rootCmd.SetArgs([]string{"version", "--detailed"}) | ||
// when | ||
err := s.rootCmd.Execute() | ||
// then | ||
s.NoError(err) | ||
// and | ||
s.Equal(strings.TrimSpace(` | ||
Version: 0.1.0 | ||
Git Tag: v0.1.0 | ||
Git Commit: c910e75b573b48961c7dcc1dd1063a543164d963 | ||
Build Date: 2020-03-03T10:59:06Z | ||
`), strings.TrimSpace(s.buf.String())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package version | ||
|
||
var ( | ||
version = "unknown" | ||
gitTag = "unknown" | ||
gitCommit = "unknown" | ||
buildDate = "unknown" | ||
) | ||
|
||
type BuildInfo struct { | ||
Version string | ||
GitTag string | ||
GitCommit string | ||
BuildDate string | ||
} | ||
|
||
var ( | ||
Build BuildInfo | ||
) | ||
|
||
func init() { | ||
Build = BuildInfo{ | ||
Version: version, | ||
GitTag: gitTag, | ||
GitCommit: gitCommit, | ||
BuildDate: buildDate, | ||
} | ||
} |