Skip to content

Commit

Permalink
Add auto completion suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Dec 12, 2023
1 parent f88a7b1 commit 2742de8
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions internal/cmd/server/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -12,13 +13,26 @@ import (
"github.com/spf13/pflag"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

var serverStatusStringSlice = []string{
string(hcloud.ServerStatusInitializing),
string(hcloud.ServerStatusOff),
string(hcloud.ServerStatusRunning),
string(hcloud.ServerStatusStarting),
string(hcloud.ServerStatusStopping),
string(hcloud.ServerStatusMigrating),
string(hcloud.ServerStatusRebuilding),
string(hcloud.ServerStatusDeleting),
string(hcloud.ServerStatusUnknown),
}

var ListCmd = base.ListCmd{
ResourceNamePlural: "Servers",
JSONKeyGetByName: "servers",
Expand All @@ -27,6 +41,7 @@ var ListCmd = base.ListCmd{

AdditionalFlags: func(cmd *cobra.Command) {
cmd.Flags().StringSlice("status", nil, "Only servers with one of these statuses are displayed")
_ = cmd.RegisterFlagCompletionFunc("status", cmpl.SuggestCandidates(serverStatusStringSlice...))
},

Fetch: func(ctx context.Context, client hcapi2.Client, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) {
Expand All @@ -38,18 +53,9 @@ var ListCmd = base.ListCmd{
}
if len(statuses) > 0 {
for _, status := range statuses {
switch status {
case string(hcloud.ServerStatusInitializing),
string(hcloud.ServerStatusOff),
string(hcloud.ServerStatusRunning),
string(hcloud.ServerStatusStarting),
string(hcloud.ServerStatusStopping),
string(hcloud.ServerStatusMigrating),
string(hcloud.ServerStatusRebuilding),
string(hcloud.ServerStatusDeleting),
string(hcloud.ServerStatusUnknown):
if slices.Contains(serverStatusStringSlice, status) {
opts.Status = append(opts.Status, hcloud.ServerStatus(status))
default:
} else {
return nil, fmt.Errorf("invalid status: %s", status)
}
}
Expand Down

0 comments on commit 2742de8

Please sign in to comment.