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

Commit4c438bd

Browse files
authored
feat(cli): add local and UTC time options toping cmd (#16648)
It's sometimes useful to see when each pong was received, forcorrelating these times with other events.---------Signed-off-by: Danny Kopping <danny@coder.com>
1 parent39f42bc commit4c438bd

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

‎cli/ping.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ import (
2121

2222
"github.com/coder/pretty"
2323

24+
"github.com/coder/serpent"
25+
2426
"github.com/coder/coder/v2/cli/cliui"
2527
"github.com/coder/coder/v2/cli/cliutil"
2628
"github.com/coder/coder/v2/coderd/util/ptr"
2729
"github.com/coder/coder/v2/codersdk"
2830
"github.com/coder/coder/v2/codersdk/healthsdk"
2931
"github.com/coder/coder/v2/codersdk/workspacesdk"
30-
"github.com/coder/serpent"
3132
)
3233

3334
typepingSummarystruct {
@@ -86,6 +87,8 @@ func (r *RootCmd) ping() *serpent.Command {
8687
pingNumint64
8788
pingTimeout time.Duration
8889
pingWait time.Duration
90+
pingTimeLocalbool
91+
pingTimeUTCbool
8992
appearanceConfig codersdk.AppearanceConfig
9093
)
9194

@@ -217,6 +220,10 @@ func (r *RootCmd) ping() *serpent.Command {
217220

218221
ctx,cancel:=context.WithTimeout(ctx,pingTimeout)
219222
dur,p2p,pong,err=conn.Ping(ctx)
223+
pongTime:=time.Now()
224+
ifpingTimeUTC {
225+
pongTime=pongTime.UTC()
226+
}
220227
cancel()
221228
results.addResult(pong)
222229
iferr!=nil {
@@ -268,7 +275,13 @@ func (r *RootCmd) ping() *serpent.Command {
268275
)
269276
}
270277

271-
_,_=fmt.Fprintf(inv.Stdout,"pong from %s %s in %s\n",
278+
vardisplayTimestring
279+
ifpingTimeLocal||pingTimeUTC {
280+
displayTime=pretty.Sprintf(cliui.DefaultStyles.DateTimeStamp,"[%s] ",pongTime.Format(time.RFC3339))
281+
}
282+
283+
_,_=fmt.Fprintf(inv.Stdout,"%spong from %s %s in %s\n",
284+
displayTime,
272285
pretty.Sprint(cliui.DefaultStyles.Keyword,workspaceName),
273286
via,
274287
pretty.Sprint(cliui.DefaultStyles.DateTimeStamp,dur.String()),
@@ -321,6 +334,16 @@ func (r *RootCmd) ping() *serpent.Command {
321334
Description:"Specifies the number of pings to perform. By default, pings will continue until interrupted.",
322335
Value:serpent.Int64Of(&pingNum),
323336
},
337+
{
338+
Flag:"time",
339+
Description:"Show the response time of each pong in local time.",
340+
Value:serpent.BoolOf(&pingTimeLocal),
341+
},
342+
{
343+
Flag:"utc",
344+
Description:"Show the response time of each pong in UTC (implies --time).",
345+
Value:serpent.BoolOf(&pingTimeUTC),
346+
},
324347
}
325348
returncmd
326349
}

‎cli/ping_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,60 @@ func TestPing(t *testing.T) {
6969
cancel()
7070
<-cmdDone
7171
})
72+
73+
t.Run("1PingWithTime",func(t*testing.T) {
74+
t.Parallel()
75+
76+
tests:= []struct {
77+
namestring
78+
utcbool
79+
}{
80+
{name:"LocalTime"},// --time renders the pong response time.
81+
{name:"UTC",utc:true},// --utc implies --time, so we expect it to also contain the pong time.
82+
}
83+
84+
for_,tc:=rangetests {
85+
t.Run(tc.name,func(t*testing.T) {
86+
t.Parallel()
87+
88+
client,workspace,agentToken:=setupWorkspaceForAgent(t)
89+
args:= []string{"ping","-n","1",workspace.Name,"--time"}
90+
iftc.utc {
91+
args=append(args,"--utc")
92+
}
93+
94+
inv,root:=clitest.New(t,args...)
95+
clitest.SetupConfig(t,client,root)
96+
pty:=ptytest.New(t)
97+
inv.Stdin=pty.Input()
98+
inv.Stderr=pty.Output()
99+
inv.Stdout=pty.Output()
100+
101+
_=agenttest.New(t,client.URL,agentToken)
102+
_=coderdtest.AwaitWorkspaceAgents(t,client,workspace.ID)
103+
104+
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
105+
defercancel()
106+
107+
cmdDone:=tGo(t,func() {
108+
err:=inv.WithContext(ctx).Run()
109+
assert.NoError(t,err)
110+
})
111+
112+
// RFC3339 is the format used to render the pong times.
113+
rfc3339:=`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?`
114+
115+
// Validate that dates are rendered as specified.
116+
iftc.utc {
117+
rfc3339+=`Z`
118+
}else {
119+
rfc3339+=`(?:Z|[+-]\d{2}:\d{2})`
120+
}
121+
122+
pty.ExpectRegexMatch(`\[`+rfc3339+`\] pong from `+workspace.Name)
123+
cancel()
124+
<-cmdDone
125+
})
126+
}
127+
})
72128
}

‎cli/testdata/coder_ping_--help.golden

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ OPTIONS:
1010
Specifies the number of pings to perform. By default, pings will
1111
continue until interrupted.
1212

13+
--time bool
14+
Show the response time of each pong in local time.
15+
1316
-t, --timeout duration (default: 5s)
1417
Specifies how long to wait for a ping to complete.
1518

19+
--utc bool
20+
Show the response time of each pong in UTC (implies --time).
21+
1622
--wait duration (default: 1s)
1723
Specifies how long to wait between pings.
1824

‎docs/reference/cli/ping.md

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp