Skip to content

Commit 751bad4

Browse files
committed
retrieve device by ID and device list should have the same output
1 parent c1f103b commit 751bad4

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

internal/devices/retrieve.go

+30-32
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strconv"
2727
"strings"
2828

29+
"github.com/equinix/equinix-sdk-go/services/metalv1"
2930
"github.com/spf13/cobra"
3031
)
3132

@@ -53,52 +54,49 @@ func (c *Client) Retrieve() *cobra.Command {
5354
}
5455
cmd.SilenceUsage = true
5556

57+
devices := make([]metalv1.Device, 0)
58+
5659
if deviceID != "" {
5760
device, _, err := c.Service.FindDeviceById(context.Background(), deviceID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil)).Execute()
5861
if err != nil {
5962
return fmt.Errorf("Could not get Devices: %w", err)
6063
}
61-
header := []string{"ID", "Hostname", "OS", "State", "Created"}
62-
63-
data := make([][]string, 1)
64-
data[0] = []string{device.GetId(), device.GetHostname(), device.OperatingSystem.GetName(), fmt.Sprintf("%v", device.GetState()), device.GetCreatedAt().String()}
65-
66-
return c.Out.Output(device, header, &data)
67-
}
64+
devices = append(devices, *device)
65+
} else {
66+
request := c.Service.FindProjectDevices(context.Background(), projectID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil))
67+
filters := c.Servicer.Filters()
68+
if filters["type"] != "" {
69+
request = request.Type_(filters["type"])
70+
}
6871

69-
request := c.Service.FindProjectDevices(context.Background(), projectID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil))
70-
filters := c.Servicer.Filters()
71-
if filters["type"] != "" {
72-
request = request.Type_(filters["type"])
73-
}
72+
if filters["facility"] != "" {
73+
request = request.Facility(filters["facility"])
74+
}
7475

75-
if filters["facility"] != "" {
76-
request = request.Facility(filters["facility"])
77-
}
76+
if filters["hostname"] != "" {
77+
request = request.Hostname(filters["hostname"])
78+
}
7879

79-
if filters["hostname"] != "" {
80-
request = request.Hostname(filters["hostname"])
81-
}
80+
if filters["reserved"] != "" {
81+
value := filters["reserved"]
82+
reserve, rerr := strconv.ParseBool(value)
83+
if rerr != nil {
84+
request = request.Reserved(reserve)
85+
}
86+
}
8287

83-
if filters["reserved"] != "" {
84-
value := filters["reserved"]
85-
reserve, rerr := strconv.ParseBool(value)
86-
if rerr != nil {
87-
request = request.Reserved(reserve)
88+
if filters["tag"] != "" {
89+
request = request.Tag(filters["tag"])
8890
}
89-
}
9091

91-
if filters["tag"] != "" {
92-
request = request.Tag(filters["tag"])
92+
resp, err := request.ExecuteWithPagination()
93+
if err != nil {
94+
return fmt.Errorf("Could not list Devices: %w", err)
95+
}
96+
devices = append(devices, resp.Devices...)
9397
}
9498

95-
resp, err := request.ExecuteWithPagination()
96-
if err != nil {
97-
return fmt.Errorf("Could not list Devices: %w", err)
98-
}
99-
devices := resp.Devices
10099
data := make([][]string, len(devices))
101-
102100
for i, dc := range devices {
103101
ips := make([]string, 0)
104102
for _, ip := range dc.GetIpAddresses() {

0 commit comments

Comments
 (0)