/
ping.go
53 lines (44 loc) · 1.2 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
// Copyright (C) 2021 Toitware ApS. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
package commands
import (
"fmt"
"github.com/spf13/cobra"
"github.com/toitlang/jaguar/cmd/jag/directory"
)
func PingCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ping",
Short: "Ping a Jaguar device to see if it is active",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := directory.GetDeviceConfig()
if err != nil {
return err
}
deviceSelect, err := parseDeviceFlag(cmd)
if err != nil {
return err
}
ctx := cmd.Context()
sdk, err := GetSDK(ctx)
if err != nil {
return err
}
device, err := GetDevice(ctx, cfg, sdk, false, deviceSelect)
if err != nil {
return err
}
if !device.Ping(ctx, sdk) {
cmd.SilenceUsage = true
return fmt.Errorf("couldn't ping the device")
}
fmt.Println("Got pong from the device")
return nil
},
}
cmd.Flags().StringP("device", "d", "", "use device with a given name, id, or address")
cmd.Flags().DurationP("timeout", "t", pingTimeout, "how long to wait for a reply")
return cmd
}