Skip to content

Commit 0211273

Browse files
authored
fix: prevent filename completion of flags that don't take filename args (#366)
1 parent 3f03c6a commit 0211273

File tree

22 files changed

+75
-0
lines changed

22 files changed

+75
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- List cloud native plans in their own section in human readable `server plans` output.
1818

19+
### Fixed
20+
21+
- Prevent filename completion of flags that don't take filename args.
22+
1923
## [3.14.0] - 2025-01-08
2024

2125
### Added

internal/commands/command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ func (s *BaseCommand) AddFlags(flags *pflag.FlagSet) {
141141

142142
flags.VisitAll(func(flag *pflag.Flag) {
143143
s.Cobra().Flags().AddFlag(flag)
144+
if _, ok := flag.Annotations[FlagAnnotationNoFileCompletions]; ok {
145+
Must(s.Cobra().RegisterFlagCompletionFunc(flag.Name, cobra.NoFileCompletions))
146+
}
144147
})
145148
}
146149

internal/commands/database/index/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1010

1111
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
12+
"github.com/spf13/cobra"
1213
"github.com/spf13/pflag"
1314
)
1415

@@ -37,6 +38,7 @@ func (s *deleteCommand) InitCommand() {
3738
s.AddFlags(flagSet)
3839

3940
commands.Must(s.Cobra().MarkFlagRequired("name"))
41+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("name", cobra.NoFileCompletions))
4042
}
4143

4244
// Execute implements commands.MultipleArgumentCommand

internal/commands/database/session/cancel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (s *cancelCommand) InitCommand() {
4242

4343
s.AddFlags(flagSet)
4444
commands.Must(s.Cobra().MarkFlagRequired("pid"))
45+
commands.Must(flagSet.SetAnnotation("pid", commands.FlagAnnotationNoFileCompletions, nil))
4546
}
4647

4748
// Execute implements commands.MultipleArgumentCommand

internal/commands/database/session/list.go

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

1313
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1414
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
15+
"github.com/spf13/cobra"
1516
"github.com/spf13/pflag"
1617
)
1718

@@ -44,6 +45,9 @@ func (s *listCommand) InitCommand() {
4445
flagSet.StringVar(&s.order, "order", "query_duration:desc", "Key and direction for sorting.")
4546

4647
s.AddFlags(flagSet)
48+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("limit", cobra.NoFileCompletions))
49+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("offset", cobra.NoFileCompletions))
50+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("order", cobra.NoFileCompletions))
4751
}
4852

4953
// Execute implements commands.MultipleArgumentCommand

internal/commands/ipaddress/assign.go

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

1313
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1414
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
15+
"github.com/spf13/cobra"
1516
"github.com/spf13/pflag"
1617
)
1718

@@ -53,7 +54,9 @@ func (s *assignCommand) InitCommand() {
5354
fs.StringVar(&s.mac, "mac", "", "MAC address of server interface to assign address to. Required for detached floating IP address if zone is not specified.")
5455
fs.StringVar(&s.zone, "zone", "", (namedargs.ZoneDescription("IP address") + " Required when creating a detached floating IP address, i.e. when MAC address is not specified."))
5556
config.AddToggleFlag(fs, &s.floating, "floating", false, "Whether the address to be assigned is a floating one.")
57+
5658
s.AddFlags(fs)
59+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("mac", cobra.NoFileCompletions))
5760
}
5861

5962
func (s *assignCommand) InitCommandWithConfig(cfg *config.Config) {

internal/commands/kubernetes/create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1616
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
17+
"github.com/spf13/cobra"
1718
"github.com/spf13/pflag"
1819
)
1920

@@ -150,6 +151,7 @@ func (c *createCommand) InitCommand() {
150151
commands.Must(c.Cobra().MarkFlagRequired("name"))
151152
commands.Must(c.Cobra().MarkFlagRequired("network"))
152153
commands.Must(c.Cobra().MarkFlagRequired("zone"))
154+
commands.Must(c.Cobra().RegisterFlagCompletionFunc("name", cobra.NoFileCompletions))
153155
}
154156

155157
func (c *createCommand) InitCommandWithConfig(cfg *config.Config) {

internal/commands/kubernetes/modify.go

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

1313
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
1414
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
15+
"github.com/spf13/cobra"
1516
"github.com/spf13/pflag"
1617
)
1718

@@ -50,6 +51,7 @@ func (c *modifyCommand) InitCommand() {
5051

5152
c.AddFlags(fs)
5253
c.Cobra().MarkFlagsMutuallyExclusive("label", "clear-labels")
54+
commands.Must(c.Cobra().RegisterFlagCompletionFunc("label", cobra.NoFileCompletions))
5355
}
5456

5557
// Execute implements commands.MultipleArgumentCommand

internal/commands/kubernetes/nodegroup/create.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func GetCreateNodeGroupFlagSet(p *CreateNodeGroupParams) *pflag.FlagSet {
4242
fs.StringArrayVar(&p.Taints, "taint", []string{}, "Taints to be configured to the nodes in `key=value:effect` format")
4343
config.AddEnableOrDisableFlag(fs, &p.UtilityNetworkAccess, true, "utility-network-access", "utility network access. If disabled, nodes in this group will not have access to utility network")
4444

45+
commands.Must(fs.SetAnnotation("count", commands.FlagAnnotationNoFileCompletions, nil))
46+
commands.Must(fs.SetAnnotation("kubelet-arg", commands.FlagAnnotationNoFileCompletions, nil))
47+
commands.Must(fs.SetAnnotation("label", commands.FlagAnnotationNoFileCompletions, nil))
48+
commands.Must(fs.SetAnnotation("name", commands.FlagAnnotationNoFileCompletions, nil))
49+
commands.Must(fs.SetAnnotation("plan", commands.FlagAnnotationNoFileCompletions, nil))
50+
commands.Must(fs.SetAnnotation("storage", commands.FlagAnnotationNoFileCompletions, nil))
51+
commands.Must(fs.SetAnnotation("taint", commands.FlagAnnotationNoFileCompletions, nil))
52+
4553
return fs
4654
}
4755

internal/commands/kubernetes/nodegroup/delete.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
99
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
1010
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
11+
"github.com/spf13/cobra"
1112
"github.com/spf13/pflag"
1213
)
1314

@@ -36,6 +37,7 @@ func (s *deleteCommand) InitCommand() {
3637
s.AddFlags(flagSet)
3738

3839
commands.Must(s.Cobra().MarkFlagRequired("name"))
40+
commands.Must(s.Cobra().RegisterFlagCompletionFunc("name", cobra.NoFileCompletions))
3941
}
4042

4143
// ExecuteSingleArgument implements commands.SingleArgumentCommand

0 commit comments

Comments
 (0)