-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add version command to print the build version (#51)
* add test for version command * implement version command * add test for detailed version command * implement detailed version command * add newVersionCmd to rootCmd * add ldflags for build * remove TDD cycle comments
- Loading branch information
1 parent
c910e75
commit 28e6dc1
Showing
6 changed files
with
166 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,57 @@ | ||
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() { | ||
s.rootCmd.SetArgs([]string{"version"}) | ||
err := s.rootCmd.Execute() | ||
s.NoError(err) | ||
s.Equal(strings.TrimSpace(`0.1.0`), strings.TrimSpace(s.buf.String())) | ||
} | ||
|
||
func (s *VersionCmdSuite) TestVersionDetailedOutput() { | ||
s.rootCmd.SetArgs([]string{"version", "--detailed"}) | ||
err := s.rootCmd.Execute() | ||
s.NoError(err) | ||
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, | ||
} | ||
} |