-
Notifications
You must be signed in to change notification settings - Fork 3
/
devices.go
128 lines (116 loc) · 3.58 KB
/
devices.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package cmd
import (
"fmt"
"os"
"github.com/foxglove/foxglove-cli/foxglove/api"
"github.com/foxglove/foxglove-cli/foxglove/util"
"github.com/spf13/cobra"
)
func newListDevicesCommand(params *baseParams) *cobra.Command {
var format string
var isJsonFormat bool
deviceListCmd := &cobra.Command{
Use: "list",
Short: "List devices registered to your organization",
Run: func(cmd *cobra.Command, args []string) {
client := api.NewRemoteFoxgloveClient(
params.baseURL, *params.clientID,
params.token,
params.userAgent,
)
format = ResolveFormat(format, isJsonFormat)
err := renderList(
os.Stdout,
api.DevicesRequest{},
client.Devices,
format,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list devices: %s\n", err)
}
},
}
deviceListCmd.InheritedFlags()
AddFormatFlag(deviceListCmd, &format)
AddJsonFlag(deviceListCmd, &isJsonFormat)
return deviceListCmd
}
func newAddDeviceCommand(params *baseParams) *cobra.Command {
var name string
var serialNumber string
var propertyPairs []string
addDeviceCmd := &cobra.Command{
Use: "add",
Short: "Add a device for your organization",
Run: func(cmd *cobra.Command, args []string) {
client := api.NewRemoteFoxgloveClient(
params.baseURL, *params.clientID,
params.token,
params.userAgent,
)
if serialNumber != "" {
fmt.Fprintf(os.Stderr, "Warning: serial-number is deprecated and will be removed in the next release\n")
}
properties, err := util.DeviceProperties(propertyPairs, client)
if err != nil {
dief("Failed to create device: %s", err)
}
resp, err := client.CreateDevice(api.CreateDeviceRequest{
Name: name,
Properties: properties,
})
if err != nil {
dief("Failed to create device: %s", err)
}
fmt.Fprintf(os.Stderr, "Device created: %s\n", resp.ID)
},
}
addDeviceCmd.InheritedFlags()
addDeviceCmd.PersistentFlags().StringVarP(&name, "name", "", "", "name of the device")
addDeviceCmd.PersistentFlags().StringVarP(&serialNumber, "serial-number", "", "", "Deprecated. Value will be ignored.")
addDeviceCmd.PersistentFlags().StringArrayVarP(&propertyPairs, "property", "p", []string{}, "Custom property colon-separated key value pair. Multiple may be specified.")
return addDeviceCmd
}
func newEditDeviceCommand(params *baseParams) *cobra.Command {
var name string
var propertyPairs []string
addDeviceCmd := &cobra.Command{
Use: "edit",
Short: "Edit a device",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
client := api.NewRemoteFoxgloveClient(
params.baseURL, *params.clientID,
params.token,
params.userAgent,
)
properties, err := util.DeviceProperties(propertyPairs, client)
if err != nil {
dief("Failed to edit device: %s", err)
}
nameOrId := args[0]
reqBody := api.CreateDeviceRequest{}
if properties == nil && name == "" {
dief("Nothing to update")
}
if name != "" {
reqBody.Name = name
}
if properties != nil {
reqBody.Properties = properties
}
resp, err := client.EditDevice(nameOrId, api.CreateDeviceRequest{
Name: name,
Properties: properties,
})
if err != nil {
dief("Failed to edit device: %s", err)
}
fmt.Fprintf(os.Stderr, "Device updated: %s\n", resp.Name)
},
}
addDeviceCmd.InheritedFlags()
addDeviceCmd.PersistentFlags().StringVarP(&name, "name", "", "", "New name for the device")
addDeviceCmd.PersistentFlags().StringArrayVarP(&propertyPairs, "property", "p", []string{}, "Custom property colon-separated key value pair. Multiple may be specified.")
return addDeviceCmd
}