Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve datacenter describe server types readability #854

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions internal/cmd/datacenter/describe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package datacenter

import (
"slices"
"strconv"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
Expand Down Expand Up @@ -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))
jooola marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
50 changes: 10 additions & 40 deletions internal/cmd/datacenter/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"})
Expand All @@ -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)
Expand Down