Skip to content

Commit b509594

Browse files
committed
Only support 'coder version'
1 parent ae779c6 commit b509594

File tree

2 files changed

+17
-53
lines changed

2 files changed

+17
-53
lines changed

cli/root.go

Lines changed: 12 additions & 27 deletions
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.
@@ -94,7 +93,6 @@ func Root() *cobra.Command {
9493
)
9594

9695
cmd.SetUsageTemplate(usageTemplate())
97-
cmd.SetVersionTemplate(versionTemplate())
9896

9997
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.")
10098
cmd.PersistentFlags().String(varToken, "", "Specify an authentication token.")
@@ -108,28 +106,26 @@ func Root() *cobra.Command {
108106
cmd.PersistentFlags().Bool(varNoOpen, false, "Block automatically opening URLs in the browser.")
109107
_ = cmd.PersistentFlags().MarkHidden(varNoOpen)
110108

111-
// Cobra automatically uses "-v" and "--version" for printing the version on
112-
// the root cmd. If we decide to use "-v" as a verbose flag on the root,
113-
// then that will be a behavior change. So we should just reserve "-v"
114-
// and make users use "coder --version" or "coder version"
115-
cmd.Flags().BoolP("placeholder", "v", false, "This flag is a placeholder to make the cobra version flag work appropriately")
116-
_ = cmd.Flags().MarkHidden("placeholder")
117-
118109
return cmd
119110
}
120111

121-
// versionCmd comes from example in cobra issue.
122-
// https://github.com/spf13/cobra/issues/724
112+
// versionCmd prints the coder version
123113
func versionCmd() *cobra.Command {
124114
return &cobra.Command{
125115
Use: "version",
126-
Short: "Version for coder",
116+
Short: "Show coder version",
127117
Example: "coder version",
128118
RunE: func(cmd *cobra.Command, args []string) error {
129-
// Use "--version" behavior on root.
130-
root := cmd.Root()
131-
root.SetArgs([]string{"--version"})
132-
return root.Execute()
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
133129
},
134130
}
135131
}
@@ -310,17 +306,6 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.
310306
{{end}}`
311307
}
312308

313-
func versionTemplate() string {
314-
template := `Coder {{printf "%s" .Version}}`
315-
buildTime, valid := buildinfo.Time()
316-
if valid {
317-
template += " " + buildTime.Format(time.UnixDate)
318-
}
319-
template += "\r\n" + buildinfo.ExternalURL()
320-
template += "\r\n"
321-
return template
322-
}
323-
324309
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
325310
func FormatCobraError(err error, cmd *cobra.Command) string {
326311
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath())

cli/root_test.go

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,17 @@ func TestRoot(t *testing.T) {
2323
require.Contains(t, errStr, "Run 'coder delete --help' for usage.")
2424
})
2525

26-
// The "-v" short flag should return the usage output, not a version output
27-
t.Run("TestVShortFlag", func(t *testing.T) {
26+
t.Run("TestVersion", func(t *testing.T) {
2827
t.Parallel()
2928

3029
buf := new(bytes.Buffer)
31-
cmd, _ := clitest.New(t, "-v")
30+
cmd, _ := clitest.New(t, "version")
3231
cmd.SetOut(buf)
33-
3432
err := cmd.Execute()
3533
require.NoError(t, err)
36-
// Check the usage string is the output when using "-v"
37-
require.Contains(t, buf.String(), cmd.UsageString())
38-
require.Contains(t, buf.String(), cmd.Root().Long)
39-
})
40-
41-
t.Run("TestVersion", func(t *testing.T) {
42-
t.Parallel()
43-
44-
bufFlag := new(bytes.Buffer)
45-
cmd, _ := clitest.New(t, "--version")
46-
cmd.SetOut(bufFlag)
47-
err := cmd.Execute()
48-
require.NoError(t, err)
49-
50-
bufCmd := new(bytes.Buffer)
51-
cmd, _ = clitest.New(t, "version")
52-
cmd.SetOut(bufCmd)
53-
err = cmd.Execute()
54-
require.NoError(t, err)
5534

56-
require.Equal(t, bufFlag.String(), bufCmd.String(), "cmd and flag identical output")
57-
require.Contains(t, bufFlag.String(), buildinfo.Version(), "has version")
58-
require.Contains(t, bufFlag.String(), buildinfo.ExternalURL(), "has url")
35+
output := buf.String()
36+
require.Contains(t, output, buildinfo.Version(), "has version")
37+
require.Contains(t, output, buildinfo.ExternalURL(), "has url")
5938
})
6039
}

0 commit comments

Comments
 (0)