Skip to content

Commit 4c438bd

Browse files
authored
feat(cli): add local and UTC time options to ping cmd (#16648)
It's sometimes useful to see when each pong was received, for correlating these times with other events. --------- Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 39f42bc commit 4c438bd

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
type pingSummary struct {
@@ -86,6 +87,8 @@ func (r *RootCmd) ping() *serpent.Command {
8687
pingNum int64
8788
pingTimeout time.Duration
8889
pingWait time.Duration
90+
pingTimeLocal bool
91+
pingTimeUTC bool
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+
if pingTimeUTC {
225+
pongTime = pongTime.UTC()
226+
}
220227
cancel()
221228
results.addResult(pong)
222229
if err != 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+
var displayTime string
279+
if pingTimeLocal || 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
return cmd
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+
name string
78+
utc bool
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 := range tests {
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+
if tc.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+
defer cancel()
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+
if tc.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 about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy