-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist.go
82 lines (69 loc) · 1.86 KB
/
list.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
package thing
import (
"fmt"
"strings"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/table"
"github.com/arduino/iot-cloud-cli/command/thing"
"github.com/spf13/cobra"
)
var listFlags struct {
ids []string
deviceID string
variables bool
}
func initListCommand() *cobra.Command {
listCommand := &cobra.Command{
Use: "list",
Short: "List things",
Long: "List things on Arduino IoT Cloud",
RunE: runListCommand,
}
// list only the things corresponding to the passed ids
listCommand.Flags().StringSliceVarP(&listFlags.ids, "ids", "i", []string{}, "List of thing IDs to be retrieved")
// list only the thing associated to the passed device id
listCommand.Flags().StringVarP(&listFlags.deviceID, "device-id", "d", "", "ID of Device associated to the thing to be retrieved")
listCommand.Flags().BoolVarP(&listFlags.variables, "show-variables", "s", false, "Show thing variables")
return listCommand
}
func runListCommand(cmd *cobra.Command, args []string) error {
fmt.Println("Listing things")
params := &thing.ListParams{
IDs: listFlags.ids,
Variables: listFlags.variables,
}
if listFlags.deviceID != "" {
params.DeviceID = &listFlags.deviceID
}
things, err := thing.List(params)
if err != nil {
return err
}
feedback.PrintResult(result{things})
return nil
}
type result struct {
things []thing.ThingInfo
}
func (r result) Data() interface{} {
return r.things
}
func (r result) String() string {
if len(r.things) == 0 {
return "No things found."
}
t := table.New()
h := []interface{}{"Name", "ID", "Device"}
if listFlags.variables {
h = append(h, "Variables")
}
t.SetHeader(h...)
for _, thing := range r.things {
r := []interface{}{thing.Name, thing.ID, thing.DeviceID}
if listFlags.variables {
r = append(r, strings.Join(thing.Variables, ", "))
}
t.AddRow(r...)
}
return t.Render()
}