Skip to content

Commit 84504cb

Browse files
Emyrkkylecarbs
authored andcommitted
chore: version sub command remove --version and -v flag (#2090)
* test: Add unit test for version cmd
1 parent 44f6d11 commit 84504cb

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

cli/root.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func init() {
5353
func Root() *cobra.Command {
5454
cmd := &cobra.Command{
5555
Use: "coder",
56-
Version: buildinfo.Version(),
5756
SilenceErrors: true,
5857
SilenceUsage: true,
5958
Long: `Coder — A tool for provisioning self-hosted development environments.
@@ -90,10 +89,10 @@ func Root() *cobra.Command {
9089
users(),
9190
portForward(),
9291
workspaceAgent(),
92+
versionCmd(),
9393
)
9494

9595
cmd.SetUsageTemplate(usageTemplate())
96-
cmd.SetVersionTemplate(versionTemplate())
9796

9897
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
9998
cmd.PersistentFlags().String(varToken, "", "Specify an authentication token.")
@@ -110,6 +109,27 @@ func Root() *cobra.Command {
110109
return cmd
111110
}
112111

112+
// versionCmd prints the coder version
113+
func versionCmd() *cobra.Command {
114+
return &cobra.Command{
115+
Use: "version",
116+
Short: "Show coder version",
117+
Example: "coder version",
118+
RunE: func(cmd *cobra.Command, args []string) error {
119+
var str strings.Builder
120+
_, _ = str.WriteString(fmt.Sprintf("Coder %s", buildinfo.Version()))
121+
buildTime, valid := buildinfo.Time()
122+
if valid {
123+
_, _ = str.WriteString(" " + buildTime.Format(time.UnixDate))
124+
}
125+
_, _ = str.WriteString("\r\n" + buildinfo.ExternalURL() + "\r\n")
126+
127+
_, _ = fmt.Fprint(cmd.OutOrStdout(), str.String())
128+
return nil
129+
},
130+
}
131+
}
132+
113133
// createClient returns a new client from the command context.
114134
// It reads from global configuration files if flags are not set.
115135
func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
@@ -286,17 +306,6 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.
286306
{{end}}`
287307
}
288308

289-
func versionTemplate() string {
290-
template := `Coder {{printf "%s" .Version}}`
291-
buildTime, valid := buildinfo.Time()
292-
if valid {
293-
template += " " + buildTime.Format(time.UnixDate)
294-
}
295-
template += "\r\n" + buildinfo.ExternalURL()
296-
template += "\r\n"
297-
return template
298-
}
299-
300309
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
301310
func FormatCobraError(err error, cmd *cobra.Command) string {
302311
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())

cli/root_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cli_test
22

33
import (
4+
"bytes"
45
"testing"
56

7+
"github.com/coder/coder/buildinfo"
8+
69
"github.com/stretchr/testify/require"
710

811
"github.com/coder/coder/cli"
@@ -19,4 +22,18 @@ func TestRoot(t *testing.T) {
1922
errStr := cli.FormatCobraError(err, cmd)
2023
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
2124
})
25+
26+
t.Run("Version", func(t *testing.T) {
27+
t.Parallel()
28+
29+
buf := new(bytes.Buffer)
30+
cmd, _ := clitest.New(t, "version")
31+
cmd.SetOut(buf)
32+
err := cmd.Execute()
33+
require.NoError(t, err)
34+
35+
output := buf.String()
36+
require.Contains(t, output, buildinfo.Version(), "has version")
37+
require.Contains(t, output, buildinfo.ExternalURL(), "has url")
38+
})
2239
}

0 commit comments

Comments
 (0)