|
| 1 | +package devices |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/completion" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/output" |
| 9 | +) |
| 10 | + |
| 11 | +// ShowCommand creates the "zone devices show" command |
| 12 | +func ShowCommand() commands.Command { |
| 13 | + return &showCommand{ |
| 14 | + BaseCommand: commands.New("show", "Show detailed device availability for the given zone", "upctl zone devices show fi-hel2"), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type showCommand struct { |
| 19 | + *commands.BaseCommand |
| 20 | + completion.Zone |
| 21 | +} |
| 22 | + |
| 23 | +// Execute implements commands.MultipleArgumentCommand |
| 24 | +func (s *showCommand) Execute(exec commands.Executor, zone string) (output.Output, error) { |
| 25 | + svc := exec.All() |
| 26 | + d, err := svc.GetDevicesAvailability(exec.Context()) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + sections := output.Combined{} |
| 32 | + |
| 33 | + if len((*d)[zone].GPUPlans) > 0 { |
| 34 | + rows := []output.TableRow{} |
| 35 | + for name, data := range (*d)[zone].GPUPlans { |
| 36 | + rows = append(rows, output.TableRow{ |
| 37 | + name, |
| 38 | + data.Amount, |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + sections = append(sections, output.CombinedSection{ |
| 43 | + Key: "gpu_plans", |
| 44 | + Title: "GPU plans", |
| 45 | + Contents: output.Table{ |
| 46 | + Columns: []output.TableColumn{ |
| 47 | + {Key: "name", Header: "Name"}, |
| 48 | + {Key: "amount", Header: "Amount"}, |
| 49 | + }, |
| 50 | + Rows: rows, |
| 51 | + }, |
| 52 | + }) |
| 53 | + |
| 54 | + sort.Slice(rows, func(i, j int) bool { |
| 55 | + return rows[i][0].(string) < rows[j][0].(string) |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + return output.MarshaledWithHumanOutput{ |
| 60 | + Value: (*d)[zone], |
| 61 | + Output: sections, |
| 62 | + }, nil |
| 63 | +} |
0 commit comments