diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index b2f6ec7f..16e3e213 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -1,6 +1,9 @@ package datacenter import ( + "slices" + "strconv" + "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/base" @@ -35,27 +38,41 @@ var DescribeCmd = base.DescribeCmd{ cmd.Printf(" City:\t\t%s\n", datacenter.Location.City) cmd.Printf(" Latitude:\t%f\n", datacenter.Location.Latitude) cmd.Printf(" Longitude:\t%f\n", datacenter.Location.Longitude) - cmd.Printf("Server Types:\n") - printServerTypes := func(list []*hcloud.ServerType) { - for _, t := range list { - cmd.Printf(" - ID:\t\t %d\n", t.ID) - cmd.Printf(" Name:\t %s\n", s.Client().ServerType().ServerTypeName(t.ID)) - cmd.Printf(" Description: %s\n", s.Client().ServerType().ServerTypeDescription(t.ID)) - } + type ServerTypeStatus struct { + ID int64 + Available bool + Supported bool } - cmd.Printf(" Available:\n") - if len(datacenter.ServerTypes.Available) > 0 { - printServerTypes(datacenter.ServerTypes.Available) - } else { - cmd.Printf(" No available server types\n") + allServerTypeStatus := make([]ServerTypeStatus, 0, len(datacenter.ServerTypes.Supported)) + for _, serverType := range datacenter.ServerTypes.Supported { + allServerTypeStatus = append(allServerTypeStatus, ServerTypeStatus{ID: serverType.ID, Supported: true}) } - cmd.Printf(" Supported:\n") - if len(datacenter.ServerTypes.Supported) > 0 { - printServerTypes(datacenter.ServerTypes.Supported) + + for _, serverType := range datacenter.ServerTypes.Available { + index := slices.IndexFunc(allServerTypeStatus, func(i ServerTypeStatus) bool { return serverType.ID == i.ID }) + if index >= 0 { + allServerTypeStatus[index].Available = true + } else { + allServerTypeStatus = append(allServerTypeStatus, ServerTypeStatus{ID: serverType.ID, Available: true}) + } + } + + slices.SortFunc(allServerTypeStatus, func(a, b ServerTypeStatus) int { return int(a.ID - b.ID) }) + + cmd.Printf("Server Types:\n") + if len(allServerTypeStatus) > 0 { + for _, t := range allServerTypeStatus { + cmd.Printf(" - ID: %-8d Name: %-8s Supported: %-8s Available: %s\n", + t.ID, + s.Client().ServerType().ServerTypeName(t.ID), + strconv.FormatBool(t.Supported), + strconv.FormatBool(t.Available), + ) + } } else { - cmd.Printf(" No supported server types\n") + cmd.Printf(" No server types\n") } return nil diff --git a/internal/cmd/datacenter/describe_test.go b/internal/cmd/datacenter/describe_test.go index fd31d946..c8fc375e 100644 --- a/internal/cmd/datacenter/describe_test.go +++ b/internal/cmd/datacenter/describe_test.go @@ -40,10 +40,7 @@ Location: Latitude: 0.000000 Longitude: 0.000000 Server Types: - Available: - No available server types - Supported: - No supported server types + No server types ` assert.NoError(t, err) @@ -89,20 +86,15 @@ func TestDescribeWithTypes(t *testing.T) { Location: &hcloud.Location{Name: "fsn1"}, Description: "Falkenstein 1 virtual DC 14", ServerTypes: hcloud.DatacenterServerTypes{ - Available: serverTypes, Supported: serverTypes, + Available: serverTypes[:2], }, }, nil, nil) - for i := 0; i < 2; i++ { - for _, st := range serverTypes { - fx.Client.ServerTypeClient.EXPECT(). - ServerTypeName(st.ID). - Return(st.Name) - fx.Client.ServerTypeClient.EXPECT(). - ServerTypeDescription(st.ID). - Return(st.Description) - } + for _, st := range serverTypes { + fx.Client.ServerTypeClient.EXPECT(). + ServerTypeName(st.ID). + Return(st.Name) } out, errOut, err := fx.Run(cmd, []string{"test"}) @@ -118,32 +110,10 @@ Location: Latitude: 0.000000 Longitude: 0.000000 Server Types: - Available: - - ID: 3 - Name: cx22 - Description: CX22 - - ID: 5 - Name: cx32 - Description: CX32 - - ID: 7 - Name: cx42 - Description: CX42 - - ID: 9 - Name: cx52 - Description: CX52 - Supported: - - ID: 3 - Name: cx22 - Description: CX22 - - ID: 5 - Name: cx32 - Description: CX32 - - ID: 7 - Name: cx42 - Description: CX42 - - ID: 9 - Name: cx52 - Description: CX52 + - ID: 3 Name: cx22 Supported: true Available: true + - ID: 5 Name: cx32 Supported: true Available: true + - ID: 7 Name: cx42 Supported: true Available: false + - ID: 9 Name: cx52 Supported: true Available: false ` assert.NoError(t, err)