Skip to content

Commit 522a8d6

Browse files
committed
Fix list subcommand
1 parent ea016ac commit 522a8d6

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

internal/sandbox/sandbox.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ type CreateOptions struct {
3535
}
3636

3737
type Instance struct {
38-
Name string
39-
Status string
40-
Endpoint string
41-
Expires string
38+
Name string `json:"instance_name"`
39+
Status string `json:"status"`
40+
Endpoint string `json:"endpoint_url"`
41+
Expires string `json:"expiry_time"`
4242
}
4343

4444
type Client struct {
@@ -225,47 +225,53 @@ func (c *Client) closeBody(body io.ReadCloser) {
225225
}
226226

227227
func parseInstances(body []byte) ([]Instance, error) {
228-
var direct []Instance
229-
if err := json.Unmarshal(body, &direct); err == nil {
230-
return direct, nil
228+
var items []any
229+
if err := json.Unmarshal(body, &items); err == nil {
230+
instances := make([]Instance, 0, len(items))
231+
for _, item := range items {
232+
m, ok := item.(map[string]any)
233+
if !ok {
234+
continue
235+
}
236+
instances = append(instances, instanceFromMap(m))
237+
}
238+
return instances, nil
231239
}
232240
var wrapped map[string][]map[string]any
233241
if err := json.Unmarshal(body, &wrapped); err != nil {
234242
return nil, err
235243
}
236-
var raw []map[string]any
237244
for _, key := range []string{"instances", "items", "data"} {
238245
if v, ok := wrapped[key]; ok {
239-
raw = v
240-
break
246+
return instancesFromMaps(v), nil
241247
}
242248
}
243-
if raw == nil {
244-
return nil, fmt.Errorf("sandbox list response did not contain instances")
245-
}
246-
instances := make([]Instance, 0, len(raw))
247-
for _, m := range raw {
248-
instances = append(instances, Instance{
249-
Name: fieldString(m, "instance_name", "instanceName", "name", "id"),
250-
Status: fieldString(m, "status"),
251-
Endpoint: fieldString(m, "endpoint_url", "endpointUrl", "endpoint"),
252-
Expires: fieldString(m, "expiry_time", "expiryTime", "expires_at", "expiresAt", "expires"),
253-
})
254-
}
255-
return instances, nil
249+
return nil, fmt.Errorf("sandbox list response did not contain instances")
256250
}
257251

258252
func parseInstance(body []byte) (Instance, error) {
259253
var m map[string]any
260254
if err := json.Unmarshal(body, &m); err != nil {
261255
return Instance{}, err
262256
}
257+
return instanceFromMap(m), nil
258+
}
259+
260+
func instancesFromMaps(maps []map[string]any) []Instance {
261+
instances := make([]Instance, 0, len(maps))
262+
for _, m := range maps {
263+
instances = append(instances, instanceFromMap(m))
264+
}
265+
return instances
266+
}
267+
268+
func instanceFromMap(m map[string]any) Instance {
263269
return Instance{
264270
Name: fieldString(m, "instance_name", "instanceName", "name", "id"),
265271
Status: fieldString(m, "status"),
266272
Endpoint: fieldString(m, "endpoint_url", "endpointUrl", "endpoint"),
267273
Expires: fieldString(m, "expiry_time", "expiryTime", "expires_at", "expiresAt", "expires"),
268-
}, nil
274+
}
269275
}
270276

271277
func parseLogLines(body []byte) ([]string, error) {

0 commit comments

Comments
 (0)