Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2b2580

Browse files
authored
chore:version sub command remove--version and-v flag (#2090)
* test: Add unit test for version cmd
1 parenta7a7e75 commite2b2580

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

‎cli/root.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func init() {
5353
funcRoot()*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
returncmd
111110
}
112111

112+
// versionCmd prints the coder version
113+
funcversionCmd()*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+
varstr strings.Builder
120+
_,_=str.WriteString(fmt.Sprintf("Coder %s",buildinfo.Version()))
121+
buildTime,valid:=buildinfo.Time()
122+
ifvalid {
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+
returnnil
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
funccreateClient(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-
funcversionTemplate()string {
290-
template:=`Coder {{printf "%s" .Version}}`
291-
buildTime,valid:=buildinfo.Time()
292-
ifvalid {
293-
template+=" "+buildTime.Format(time.UnixDate)
294-
}
295-
template+="\r\n"+buildinfo.ExternalURL()
296-
template+="\r\n"
297-
returntemplate
298-
}
299-
300309
// FormatCobraError colorizes and adds "--help" docs to cobra commands.
301310
funcFormatCobraError(errerror,cmd*cobra.Command)string {
302311
helpErrMsg:=fmt.Sprintf("Run '%s --help' for usage.",cmd.CommandPath())

‎cli/root_test.go

Lines changed: 17 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp