-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Enable to display current command's version (#20)
- Loading branch information
Shu Kutsuzawa
authored
Mar 11, 2020
1 parent
8c9aacf
commit 7082817
Showing
4 changed files
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// version is embedded when building this command using ldflags. | ||
// if nothing is embedded, version is "dev" | ||
version = "dev" | ||
) | ||
|
||
func newVersionCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "version", | ||
Short: "Display command's version.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintln(cmd.OutOrStdout(), version) | ||
}, | ||
} | ||
} |
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,46 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVersionCmd(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
version string | ||
expect string | ||
}{ | ||
{ | ||
name: "0.0.1 is embedded as version", | ||
version: "0.0.1", | ||
expect: "0.0.1", | ||
}, | ||
{ | ||
name: "0.1.0 is embedded as version", | ||
version: "0.1.0", | ||
expect: "0.1.0", | ||
}, | ||
{ | ||
name: "version is not embedded", | ||
expect: "dev", | ||
}, | ||
} | ||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
tv := version | ||
if c.version != "" { | ||
version = c.version | ||
} | ||
defer func() { version = tv }() | ||
cmd := newVersionCmd() | ||
buf := bytes.NewBuffer(nil) | ||
cmd.SetOut(buf) | ||
cmd.Execute() | ||
assert.Equal(t, fmt.Sprintf("%s\n", c.expect), buf.String()) | ||
}) | ||
} | ||
} |