11package server
22
33import (
4+ "sort"
5+ "strings"
6+
47 "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
58 "github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
9+ "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
610)
711
812// PlanListCommand creates the "server plans" command
@@ -18,14 +22,28 @@ type planListCommand struct {
1822
1923// ExecuteWithoutArguments implements commands.NoArgumentCommand
2024func (s * planListCommand ) ExecuteWithoutArguments (exec commands.Executor ) (output.Output , error ) {
21- plans , err := exec .All ().GetPlans (exec .Context ())
25+ plansObj , err := exec .All ().GetPlans (exec .Context ())
2226 if err != nil {
2327 return nil , err
2428 }
2529
26- rows := []output.TableRow {}
27- for _ , p := range plans .Plans {
28- rows = append (rows , output.TableRow {
30+ plans := plansObj .Plans
31+ sort .Slice (plans , func (i , j int ) bool {
32+ if plans [i ].CoreNumber != plans [j ].CoreNumber {
33+ return plans [i ].CoreNumber < plans [j ].CoreNumber
34+ }
35+
36+ if plans [i ].MemoryAmount != plans [j ].MemoryAmount {
37+ return plans [i ].MemoryAmount < plans [j ].MemoryAmount
38+ }
39+
40+ return plans [i ].StorageSize < plans [j ].StorageSize
41+ })
42+
43+ rows := make (map [string ][]output.TableRow )
44+ for _ , p := range plans {
45+ key := planType (p )
46+ rows [key ] = append (rows [key ], output.TableRow {
2947 p .Name ,
3048 p .CoreNumber ,
3149 p .MemoryAmount ,
@@ -37,7 +55,33 @@ func (s *planListCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu
3755
3856 return output.MarshaledWithHumanOutput {
3957 Value : plans ,
40- Output : output.Table {
58+ Output : output.Combined {
59+ planSection ("general_purpose" , "General purpose" , rows ["general_purpose" ]),
60+ planSection ("high_cpu" , "High CPU" , rows ["high_cpu" ]),
61+ planSection ("high_memory" , "High memory" , rows ["high_memory" ]),
62+ planSection ("developer" , "Developer" , rows ["developer" ]),
63+ },
64+ }, nil
65+ }
66+
67+ func planType (p upcloud.Plan ) string {
68+ if strings .HasPrefix (p .Name , "DEV-" ) {
69+ return "developer"
70+ }
71+ if strings .HasPrefix (p .Name , "HICPU-" ) {
72+ return "high_cpu"
73+ }
74+ if strings .HasPrefix (p .Name , "HIMEM-" ) {
75+ return "high_memory"
76+ }
77+ return "general_purpose"
78+ }
79+
80+ func planSection (key , title string , rows []output.TableRow ) output.CombinedSection {
81+ return output.CombinedSection {
82+ Key : key ,
83+ Title : title ,
84+ Contents : output.Table {
4185 Columns : []output.TableColumn {
4286 {Key : "name" , Header : "Name" },
4387 {Key : "cores" , Header : "Cores" },
@@ -48,5 +92,5 @@ func (s *planListCommand) ExecuteWithoutArguments(exec commands.Executor) (outpu
4892 },
4993 Rows : rows ,
5094 },
51- }, nil
95+ }
5296}
0 commit comments