Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/reference/manual/hcloud_server-type_list.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion internal/cmd/servertype/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/kit/sliceutil"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var ListCmd = &base.ListCmd[*hcloud.ServerType, schema.ServerType]{
ResourceNamePlural: "Server Types",
JSONKeyGetByName: "server_types",
DefaultColumns: []string{"id", "name", "cores", "cpu_type", "architecture", "memory", "disk", "storage_type"},
DefaultColumns: []string{"id", "name", "cores", "cpu_type", "architecture", "memory", "disk", "location"},
SortOption: nil, // Server Types do not support sorting

Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]*hcloud.ServerType, error) {
Expand All @@ -32,6 +33,13 @@ var ListCmd = &base.ListCmd[*hcloud.ServerType, schema.ServerType]{
OutputTable: func(t *output.Table[*hcloud.ServerType], _ hcapi2.Client) {
t.
AddAllowedFields(&hcloud.ServerType{}).
AddFieldFn("location", func(serverType *hcloud.ServerType) string {
locationNames := sliceutil.Transform(
serverType.Locations,
func(l hcloud.ServerTypeLocation) string { return l.Location.Name },
)
return strings.Join(locationNames, ",")
}).
AddFieldAlias("storagetype", "storage type").
AddFieldFn("memory", func(serverType *hcloud.ServerType) string {
return fmt.Sprintf("%.1f GB", serverType.Memory)
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/servertype/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ func TestList(t *testing.T) {
Memory: 8.0,
Disk: 80,
StorageType: hcloud.StorageTypeLocal,
Locations: []hcloud.ServerTypeLocation{
{Location: &hcloud.Location{ID: 1, Name: "fsn1"}},
{Location: &hcloud.Location{ID: 2, Name: "nbg1"}},
{Location: &hcloud.Location{ID: 3, Name: "hel1"}},
},
},
}, nil)

out, errOut, err := fx.Run(cmd, []string{})

expOut := `ID NAME CORES CPU TYPE ARCHITECTURE MEMORY DISK STORAGE TYPE
123 test 2 shared arm 8.0 GB 80 GB local
expOut := `ID NAME CORES CPU TYPE ARCHITECTURE MEMORY DISK LOCATION
123 test 2 shared arm 8.0 GB 80 GB fsn1,nbg1,hel1
`

require.NoError(t, err)
Expand Down
Loading