From f91798bef9cd749cce42a74317348ebfac5a6a16 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 27 Aug 2024 21:44:52 +0200 Subject: [PATCH 1/3] feat: improve datacenter describe server types readability --- internal/cmd/datacenter/describe.go | 49 +++++++++++++++-------- internal/cmd/datacenter/describe_test.go | 50 +++++------------------- 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index b2f6ec7f..f6cf5c8c 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,39 @@ 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 + } + + 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(" Available:\n") - if len(datacenter.ServerTypes.Available) > 0 { - printServerTypes(datacenter.ServerTypes.Available) - } else { - cmd.Printf(" No available server types\n") + 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}) + } } - cmd.Printf(" Supported:\n") - if len(datacenter.ServerTypes.Supported) > 0 { - printServerTypes(datacenter.ServerTypes.Supported) - } else { - cmd.Printf(" No supported server types\n") + + slices.SortFunc(allServerTypeStatus, func(a, b *ServerTypeStatus) int { return int(a.ID - b.ID) }) + + if len(allServerTypeStatus) > 0 { + cmd.Printf("Server Types:\n") + 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), + ) + } } return nil diff --git a/internal/cmd/datacenter/describe_test.go b/internal/cmd/datacenter/describe_test.go index fd31d946..ffda45b0 100644 --- a/internal/cmd/datacenter/describe_test.go +++ b/internal/cmd/datacenter/describe_test.go @@ -39,11 +39,6 @@ Location: City: Latitude: 0.000000 Longitude: 0.000000 -Server Types: - Available: - No available server types - Supported: - No supported server types ` assert.NoError(t, err) @@ -89,20 +84,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 +108,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) From bfad1814ddcbcc835b2b8c1fd1c1004c79c4a780 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 28 Aug 2024 17:19:14 +0200 Subject: [PATCH 2/3] print when no server type in dc --- internal/cmd/datacenter/describe.go | 4 +++- internal/cmd/datacenter/describe_test.go | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index f6cf5c8c..462525f3 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -61,8 +61,8 @@ var DescribeCmd = base.DescribeCmd{ slices.SortFunc(allServerTypeStatus, func(a, b *ServerTypeStatus) int { return int(a.ID - b.ID) }) + cmd.Printf("Server Types:\n") if len(allServerTypeStatus) > 0 { - cmd.Printf("Server Types:\n") for _, t := range allServerTypeStatus { cmd.Printf(" - ID: %-8d Name: %-8s Supported: %-8s Available: %s\n", t.ID, @@ -71,6 +71,8 @@ var DescribeCmd = base.DescribeCmd{ strconv.FormatBool(t.Available), ) } + } else { + 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 ffda45b0..c8fc375e 100644 --- a/internal/cmd/datacenter/describe_test.go +++ b/internal/cmd/datacenter/describe_test.go @@ -39,6 +39,8 @@ Location: City: Latitude: 0.000000 Longitude: 0.000000 +Server Types: + No server types ` assert.NoError(t, err) From d06bff9445a7ebb31d6514146fe34727206a13c7 Mon Sep 17 00:00:00 2001 From: jo Date: Fri, 30 Aug 2024 12:45:14 +0200 Subject: [PATCH 3/3] remove pointer --- internal/cmd/datacenter/describe.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/cmd/datacenter/describe.go b/internal/cmd/datacenter/describe.go index 462525f3..16e3e213 100644 --- a/internal/cmd/datacenter/describe.go +++ b/internal/cmd/datacenter/describe.go @@ -45,21 +45,21 @@ var DescribeCmd = base.DescribeCmd{ Supported bool } - allServerTypeStatus := make([]*ServerTypeStatus, 0, len(datacenter.ServerTypes.Supported)) + allServerTypeStatus := make([]ServerTypeStatus, 0, len(datacenter.ServerTypes.Supported)) for _, serverType := range datacenter.ServerTypes.Supported { - allServerTypeStatus = append(allServerTypeStatus, &ServerTypeStatus{ID: serverType.ID, Supported: true}) + allServerTypeStatus = append(allServerTypeStatus, ServerTypeStatus{ID: serverType.ID, Supported: true}) } for _, serverType := range datacenter.ServerTypes.Available { - index := slices.IndexFunc(allServerTypeStatus, func(i *ServerTypeStatus) bool { return serverType.ID == i.ID }) + 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}) + allServerTypeStatus = append(allServerTypeStatus, ServerTypeStatus{ID: serverType.ID, Available: true}) } } - slices.SortFunc(allServerTypeStatus, func(a, b *ServerTypeStatus) int { return int(a.ID - b.ID) }) + slices.SortFunc(allServerTypeStatus, func(a, b ServerTypeStatus) int { return int(a.ID - b.ID) }) cmd.Printf("Server Types:\n") if len(allServerTypeStatus) > 0 {