Skip to content

Commit f785bfa

Browse files
authored
fix: completion improvements (#491)
1 parent 3d6a6fa commit f785bfa

30 files changed

Lines changed: 322 additions & 20 deletions

File tree

internal/commands/database/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func (s *createCommand) InitCommand() {
202202
}
203203

204204
func (s *createCommand) InitCommandWithConfig(cfg *config.Config) {
205+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("type", namedargs.CompletionFunc(completion.DatabaseType{}, cfg)))
205206
commands.Must(s.Cobra().RegisterFlagCompletionFunc("zone", namedargs.CompletionFunc(completion.Zone{}, cfg)))
206207
}
207208

internal/commands/ipaddress/ip_address.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010

1111
const maxIPAddressActions = 10
1212

13+
var Families = []string{
14+
upcloud.IPAddressFamilyIPv4,
15+
upcloud.IPAddressFamilyIPv6,
16+
}
17+
1318
// BaseIPAddressCommand creates the base 'ip-address' command
1419
func BaseIPAddressCommand() commands.Command {
1520
return &ipAddressCommand{

internal/commands/kubernetes/nodegroup/create.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
1010
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
1111
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
12+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/namedargs"
1213
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
1314
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1415

@@ -46,7 +47,6 @@ func GetCreateNodeGroupFlagSet(p *CreateNodeGroupParams) *pflag.FlagSet {
4647
commands.Must(fs.SetAnnotation("kubelet-arg", commands.FlagAnnotationNoFileCompletions, nil))
4748
commands.Must(fs.SetAnnotation("label", commands.FlagAnnotationNoFileCompletions, nil))
4849
commands.Must(fs.SetAnnotation("name", commands.FlagAnnotationNoFileCompletions, nil))
49-
commands.Must(fs.SetAnnotation("plan", commands.FlagAnnotationNoFileCompletions, nil))
5050
commands.Must(fs.SetAnnotation("storage", commands.FlagAnnotationNoFileCompletions, nil))
5151
commands.Must(fs.SetAnnotation("taint", commands.FlagAnnotationNoFileCompletions, nil))
5252

@@ -155,6 +155,10 @@ func (s *createCommand) InitCommand() {
155155
commands.Must(s.Cobra().MarkFlagRequired("plan"))
156156
}
157157

158+
func (s *createCommand) InitCommandWithConfig(cfg *config.Config) {
159+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("plan", namedargs.CompletionFunc(completion.KubernetesPlan{}, cfg)))
160+
}
161+
158162
// ExecuteSingleArgument implements commands.SingleArgumentCommand
159163
func (s *createCommand) ExecuteSingleArgument(exec commands.Executor, arg string) (output.Output, error) {
160164
msg := fmt.Sprintf("Creating node group %s into cluster %v", s.p.Name, arg)

internal/commands/network/create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (s *createCommand) InitCommand() {
6161
}
6262

6363
func (s *createCommand) InitCommandWithConfig(cfg *config.Config) {
64+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("router", namedargs.CompletionFunc(completion.Router{}, cfg)))
6465
commands.Must(s.Cobra().RegisterFlagCompletionFunc("zone", namedargs.CompletionFunc(completion.Zone{}, cfg)))
6566
}
6667

internal/commands/network/modify.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
1111
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
1212
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
13+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/namedargs"
1314
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
1415
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1516
)
@@ -54,6 +55,10 @@ func (s *modifyCommand) InitCommand() {
5455
s.AddFlags(fs)
5556
}
5657

58+
func (s *modifyCommand) InitCommandWithConfig(cfg *config.Config) {
59+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("router", namedargs.CompletionFunc(completion.Router{}, cfg)))
60+
}
61+
5762
// ExecuteSingleArgument implements commands.SingleArgumentCommand
5863
func (s *modifyCommand) ExecuteSingleArgument(exec commands.Executor, arg string) (output.Output, error) {
5964
if s.attachRouter != "" && s.detachRouter == config.True {

internal/commands/network/network.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212

1313
const maxNetworkActions = 10
1414

15+
var Types = []string{
16+
upcloud.NetworkTypePublic,
17+
upcloud.NetworkTypeUtility,
18+
upcloud.NetworkTypePrivate,
19+
}
20+
1521
// BaseNetworkCommand creates the base "network" command
1622
func BaseNetworkCommand() commands.Command {
1723
return &networkCommand{

internal/commands/server/create.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1818
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
1919
"github.com/jedib0t/go-pretty/v6/text"
20+
"github.com/spf13/cobra"
2021
"github.com/spf13/pflag"
2122
)
2223

@@ -257,6 +258,8 @@ type createCommand struct {
257258

258259
// InitCommand implements Command.InitCommand
259260
func (s *createCommand) InitCommand() {
261+
passwordDeliveries := []string{request.PasswordDeliveryNone, request.PasswordDeliveryEmail, request.PasswordDeliverySMS}
262+
260263
s.Cobra().Long = commands.WrapLongDescription(`Create a new server
261264
262265
Note that the default template, Ubuntu Server 24.04 LTS (Noble Numbat), only supports SSH key based authentication. Use ` + "`" + `--ssh-keys` + "`" + ` option to provide the keys when creating a server with the default template. The examples below use public key from the ` + "`" + `~/.ssh` + "`" + ` directory. If you want to use different authentication method, use ` + "`" + `--os` + "`" + ` parameter to specify a different template.`)
@@ -279,10 +282,10 @@ Note that the default template, Ubuntu Server 24.04 LTS (Noble Numbat), only sup
279282
fs.StringVar(&s.params.os, "os", def.os, "Server OS to use (will be the first storage device). The value should be title or UUID of an either public or private template. Set to empty to fully customise the storages.")
280283
fs.IntVar(&s.params.osStorageSize, "os-storage-size", def.osStorageSize, "OS storage size in GiB. This is only applicable if `os` is also set. Zero value makes the disk equal to the minimum size of the template.")
281284
config.AddToggleFlag(fs, &s.params.osStorageEncrypted, "os-storage-encrypt", false, "Encrypt the OS storage. This is only applicable if `os` is also set.")
282-
fs.StringVar(&s.params.PasswordDelivery, "password-delivery", def.PasswordDelivery, "Defines how password is delivered. Available: email, sms")
285+
fs.StringVar(&s.params.PasswordDelivery, "password-delivery", def.PasswordDelivery, "Defines how password is delivered. Available: "+strings.Join(passwordDeliveries, ", "))
283286
fs.StringVar(&s.params.Plan, "plan", def.Plan, "Server plan name. See \"server plans\" command for valid plans. Set to \"custom\" and use `cores` and `memory` options for flexible plan.")
284287
fs.StringVar(&s.params.RemoteAccessPassword, "remote-access-password", def.RemoteAccessPassword, "Defines the remote access password.")
285-
fs.StringVar(&s.params.RemoteAccessType, "remote-access-type", def.RemoteAccessType, "Set a remote access type. Available: vnc, spice")
288+
fs.StringVar(&s.params.RemoteAccessType, "remote-access-type", def.RemoteAccessType, "Set a remote access type. Available: "+strings.Join(remoteAccessTypes, ", "))
286289
fs.StringVar(&s.params.ServerGroup, "server-group", def.ServerGroup, "UUID of a server group for the server. To remove the server from the group, see `servergroup modify")
287290
fs.StringVar(&s.params.SimpleBackup, "simple-backup", def.SimpleBackup, simpleBackupDescription)
288291
fs.StringSliceVar(&s.params.sshKeys, "ssh-keys", def.sshKeys, "Add one or more SSH keys to the admin account. Accepted values are SSH public keys or filenames from where to read the keys.")
@@ -291,7 +294,7 @@ Note that the default template, Ubuntu Server 24.04 LTS (Noble Numbat), only sup
291294
fs.StringVar(&s.params.Title, "title", def.Title, "A short, informational description.")
292295
fs.StringVar(&s.params.UserData, "user-data", def.UserData, "Defines URL for a server setup script, or the script body itself.")
293296
fs.StringVar(&s.params.username, "username", def.username, "Admin account username.")
294-
fs.StringVar(&s.params.VideoModel, "video-model", def.VideoModel, "Video interface model of the server. Available: vga, cirrus")
297+
fs.StringVar(&s.params.VideoModel, "video-model", def.VideoModel, "Video interface model of the server. Available: "+strings.Join(videoModels, ", "))
295298
config.AddToggleFlag(fs, &s.wait, "wait", false, "Wait for server to be in started state before returning.")
296299
fs.StringVar(&s.params.Zone, "zone", def.Zone, namedargs.ZoneDescription("server"))
297300
// fs.BoolVar(&s.params.firewall, "firewall", def.firewall, "Enables the firewall. You can manage firewall rules with the firewall command.")
@@ -301,9 +304,17 @@ Note that the default template, Ubuntu Server 24.04 LTS (Noble Numbat), only sup
301304

302305
commands.Must(s.Cobra().MarkFlagRequired("hostname"))
303306
commands.Must(s.Cobra().MarkFlagRequired("zone"))
307+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("password-delivery", cobra.FixedCompletions(passwordDeliveries, cobra.ShellCompDirectiveNoFileComp)))
308+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("remote-access-type", cobra.FixedCompletions(remoteAccessTypes, cobra.ShellCompDirectiveNoFileComp)))
309+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("video-model", cobra.FixedCompletions(videoModels, cobra.ShellCompDirectiveNoFileComp)))
304310
}
305311

306312
func (s *createCommand) InitCommandWithConfig(cfg *config.Config) {
313+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("avoid-host", namedargs.CompletionFunc(completion.HostID{}, cfg)))
314+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("host", namedargs.CompletionFunc(completion.HostID{}, cfg)))
315+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("plan", namedargs.CompletionFunc(completion.ServerPlan{}, cfg)))
316+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("server-group", namedargs.CompletionFunc(completion.ServerGroupUUID{}, cfg)))
317+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("time-zone", namedargs.CompletionFunc(completion.TimeZone{}, cfg)))
307318
commands.Must(s.Cobra().RegisterFlagCompletionFunc("zone", namedargs.CompletionFunc(completion.Zone{}, cfg)))
308319
}
309320

internal/commands/server/firewall/create.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package serverfirewall
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
8+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/ipaddress"
79
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
810
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
911
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1012
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1113
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
1214
"github.com/m7shapan/cidr"
15+
"github.com/spf13/cobra"
1316
"github.com/spf13/pflag"
1417
)
1518

@@ -52,17 +55,21 @@ func (s *createCommand) MaximumExecutions() int {
5255

5356
// InitCommand implements Command.InitCommand
5457
func (s *createCommand) InitCommand() {
58+
directions := []string{upcloud.FirewallRuleDirectionIn, upcloud.FirewallRuleDirectionOut}
59+
actions := []string{upcloud.FirewallRuleActionAccept, upcloud.FirewallRuleActionDrop}
60+
protocols := []string{upcloud.FirewallRuleProtocolTCP, upcloud.FirewallRuleProtocolUDP, upcloud.FirewallRuleProtocolICMP}
61+
5562
s.Cobra().Long = commands.WrapLongDescription(`Create a new firewall rule
5663
5764
To edit the default rule of the firewall, set only ` + "`" + `--direction` + "`" + ` and ` + "`" + `--action` + "`" + ` parameters. This creates catch-all rule that will take effect when no other rule matches. Note that the default rule must be positioned after all other rules. Use ` + "`" + `--position` + "`" + ` parameter or create default rule after other rules.`)
5865

5966
flagSet := &pflag.FlagSet{}
6067

61-
flagSet.StringVar(&s.direction, "direction", "", "Rule direction. Available: in, out")
62-
flagSet.StringVar(&s.action, "action", "", "Rule action. Available: accept, drop")
63-
flagSet.StringVar(&s.family, "family", "", "IP family. Available: IPv4, IPv6")
68+
flagSet.StringVar(&s.direction, "direction", "", "Rule direction. Available: "+strings.Join(directions, ", "))
69+
flagSet.StringVar(&s.action, "action", "", "Rule action. Available: "+strings.Join(actions, ", "))
70+
flagSet.StringVar(&s.family, "family", "", "IP family. Available: "+strings.Join(ipaddress.Families, ", "))
6471
flagSet.IntVar(&s.position, "position", 0, "Position in relation to other rules. Available: 1-1000")
65-
flagSet.StringVar(&s.protocol, "protocol", "", "Protocol. Available: tcp, udp, icmp")
72+
flagSet.StringVar(&s.protocol, "protocol", "", "Protocol. Available: "+strings.Join(protocols, ", "))
6673
flagSet.StringVar(&s.icmpType, "icmp-type", "", "ICMP type. Available: 0-255")
6774
flagSet.StringVar(&s.destinationIPBlock, "dest-ipaddress-block", "", "Destination IP address block.")
6875
flagSet.StringVar(&s.destinationPortStart, "destination-port-start", "", "Destination port range start. Available: 1-65535")
@@ -77,6 +84,10 @@ To edit the default rule of the firewall, set only ` + "`" + `--direction` + "`"
7784
commands.Must(s.Cobra().MarkFlagRequired("action"))
7885
s.Cobra().MarkFlagsRequiredTogether("destination-port-start", "destination-port-end")
7986
s.Cobra().MarkFlagsRequiredTogether("source-port-start", "source-port-end")
87+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("direction", cobra.FixedCompletions(directions, cobra.ShellCompDirectiveNoFileComp)))
88+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("action", cobra.FixedCompletions(actions, cobra.ShellCompDirectiveNoFileComp)))
89+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("family", cobra.FixedCompletions(ipaddress.Families, cobra.ShellCompDirectiveNoFileComp)))
90+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("protocol", cobra.FixedCompletions(protocols, cobra.ShellCompDirectiveNoFileComp)))
8091
}
8192

8293
// Execute implements commands.MultipleArgumentCommand

internal/commands/server/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"sync"
88

99
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
10+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/network"
1011
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
1112
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
1213
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
1314
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1415
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
1516
"github.com/jedib0t/go-pretty/v6/text"
17+
"github.com/spf13/cobra"
1618
"github.com/spf13/pflag"
1719
)
1820

@@ -52,10 +54,12 @@ type listCommand struct {
5254

5355
// InitCommand implements Command.InitCommand
5456
func (ls *listCommand) InitCommand() {
57+
accessTypes := append(append(make([]string, 0, len(network.Types)), network.Types...), "all")
5558
flags := &pflag.FlagSet{}
5659
flags.StringVar(&ls.showIPAddresses, "show-ip-addresses", "none", "Show servers IP addresses of specified access type in the output or all ip addresses if argument value is \"all\" or no argument is specified.")
5760
flags.Lookup("show-ip-addresses").NoOptDefVal = "all"
5861
ls.AddFlags(flags)
62+
commands.Must(ls.Cobra().RegisterFlagCompletionFunc("show-ip-addresses", cobra.FixedCompletions(accessTypes, cobra.ShellCompDirectiveNoFileComp)))
5963
}
6064

6165
// ExecuteWithoutArguments implements commands.NoArgumentCommand

internal/commands/server/load.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
77
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands/storage"
88
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
9+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
10+
"github.com/UpCloudLtd/upcloud-cli/v3/internal/namedargs"
911
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
1012
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1113

@@ -51,6 +53,10 @@ func (s *loadCommand) InitCommand() {
5153
commands.Must(s.Cobra().MarkFlagRequired("storage"))
5254
}
5355

56+
func (s *loadCommand) InitCommandWithConfig(cfg *config.Config) {
57+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("storage", namedargs.CompletionFunc(completion.StorageCDROMUUID{}, cfg)))
58+
}
59+
5460
// Execute implements commands.MultipleArgumentCommand
5561
func (s *loadCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
5662
svc := exec.Storage()

0 commit comments

Comments
 (0)