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

feat(cli): add local and UTC time options toping cmd#16648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dannykopping merged 4 commits intomainfromdk/pingtime
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
Add local and UTC time options to ping cmd
Signed-off-by: Danny Kopping <danny@coder.com>
  • Loading branch information
@dannykopping
dannykopping committedFeb 21, 2025
commita4ee92b9bfedde9e621854d216a24132bac1229d
24 changes: 23 additions & 1 deletioncli/ping.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,6 +86,8 @@ func (r *RootCmd) ping() *serpent.Command {
pingNum int64
pingTimeout time.Duration
pingWait time.Duration
pingTime bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

nit: could we call thispingTimeLocal?

dannykopping reacted with thumbs up emoji
pingTimeUTC bool
appearanceConfig codersdk.AppearanceConfig
)

Expand DownExpand Up@@ -217,6 +219,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 {
Expand DownExpand Up@@ -268,7 +274,13 @@ func (r *RootCmd) ping() *serpent.Command {
)
}

_, _ = fmt.Fprintf(inv.Stdout, "pong from %s %s in %s\n",
var displayTime string
if pingTime || 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()),
Expand DownExpand Up@@ -321,6 +333,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(&pingTime),
},
{
Flag: "utc",
Description: "Show the response time of each pong in UTC (implies --time).",
Value: serpent.BoolOf(&pingTimeUTC),
},
}
return cmd
}
Expand Down
56 changes: 56 additions & 0 deletionscli/ping_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,4 +69,60 @@ func TestPing(t *testing.T) {
cancel()
<-cmdDone
})

t.Run("1PingWithTime", func(t *testing.T) {
t.Parallel()

tests := []struct {
name string
utc bool
}{
{name: "LocalTime"}, // --time renders the pong response time.
{name: "UTC", utc: true}, // --utc implies --time, so we expect it to also contain the pong time.
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

client, workspace, agentToken := setupWorkspaceForAgent(t)
args := []string{"ping", "-n", "1", workspace.Name, "--time"}
if tc.utc {
args = append(args, "--utc")
}

inv, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
inv.Stdin = pty.Input()
inv.Stderr = pty.Output()
inv.Stdout = pty.Output()

_ = agenttest.New(t, client.URL, agentToken)
_ = coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

cmdDone := tGo(t, func() {
err := inv.WithContext(ctx).Run()
assert.NoError(t, err)
})

// RFC3339 is the format used to render the pong times.
rfc3339 := `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?`

// Validate that dates are rendered as specified.
if tc.utc {
rfc3339 += `Z`
} else {
rfc3339 += `(?:Z|[+-]\d{2}:\d{2})`
}

pty.ExpectRegexMatch(`\[` + rfc3339 + `\] pong from ` + workspace.Name)
cancel()
<-cmdDone
})
}
})
}
6 changes: 6 additions & 0 deletionscli/testdata/coder_ping_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,9 +10,15 @@ OPTIONS:
Specifies the number of pings to perform. By default, pings will
continue until interrupted.

--time bool
Show the response time of each pong in local time.

-t, --timeout duration (default: 5s)
Specifies how long to wait for a ping to complete.

--utc bool
Show the response time of each pong in UTC (implies --time).

--wait duration (default: 1s)
Specifies how long to wait between pings.

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp