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>
This commit is contained in:
Danny Kopping
2025-02-24 07:38:17 +02:00
committed by GitHub
parent 39f42bc11d
commit 4c438bd4d3
4 changed files with 103 additions and 2 deletions

View File

@ -21,13 +21,14 @@ import (
"github.com/coder/pretty"
"github.com/coder/serpent"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/cli/cliutil"
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/healthsdk"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/serpent"
)
type pingSummary struct {
@ -86,6 +87,8 @@ func (r *RootCmd) ping() *serpent.Command {
pingNum int64
pingTimeout time.Duration
pingWait time.Duration
pingTimeLocal bool
pingTimeUTC bool
appearanceConfig codersdk.AppearanceConfig
)
@ -217,6 +220,10 @@ func (r *RootCmd) ping() *serpent.Command {
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
dur, p2p, pong, err = conn.Ping(ctx)
pongTime := time.Now()
if pingTimeUTC {
pongTime = pongTime.UTC()
}
cancel()
results.addResult(pong)
if err != nil {
@ -268,7 +275,13 @@ func (r *RootCmd) ping() *serpent.Command {
)
}
_, _ = fmt.Fprintf(inv.Stdout, "pong from %s %s in %s\n",
var displayTime string
if pingTimeLocal || pingTimeUTC {
displayTime = pretty.Sprintf(cliui.DefaultStyles.DateTimeStamp, "[%s] ", pongTime.Format(time.RFC3339))
}
_, _ = fmt.Fprintf(inv.Stdout, "%spong from %s %s in %s\n",
displayTime,
pretty.Sprint(cliui.DefaultStyles.Keyword, workspaceName),
via,
pretty.Sprint(cliui.DefaultStyles.DateTimeStamp, dur.String()),
@ -321,6 +334,16 @@ func (r *RootCmd) ping() *serpent.Command {
Description: "Specifies the number of pings to perform. By default, pings will continue until interrupted.",
Value: serpent.Int64Of(&pingNum),
},
{
Flag: "time",
Description: "Show the response time of each pong in local time.",
Value: serpent.BoolOf(&pingTimeLocal),
},
{
Flag: "utc",
Description: "Show the response time of each pong in UTC (implies --time).",
Value: serpent.BoolOf(&pingTimeUTC),
},
}
return cmd
}