-
Notifications
You must be signed in to change notification settings - Fork 224
/
ping.go
57 lines (42 loc) · 1.11 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package agent
import (
"bytes"
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/iostreams"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/render"
)
func newPing() (cmd *cobra.Command) {
const (
short = "Ping the Fly agent"
long = short + "\n"
)
cmd = command.New("ping", short, long, runPing)
cmd.Args = cobra.NoArgs
return
}
func runPing(ctx context.Context) (err error) {
var client *agent.Client
if client, err = dial(ctx); err != nil {
return
}
var pong agent.PingResponse
if pong, err = client.Ping(ctx); err != nil {
err = fmt.Errorf("failed pinging agent: %w", err)
return
}
if out := iostreams.FromContext(ctx).Out; config.FromContext(ctx).JSONOutput {
err = render.JSON(out, pong)
} else {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%-10s: %d\n", "PID", pong.PID)
fmt.Fprintf(&buf, "%-10s: %s\n", "Version", pong.Version)
fmt.Fprintf(&buf, "%-10s: %t\n", "Background", pong.Background)
_, err = buf.WriteTo(out)
}
return
}