Skip to content

Commit e20574b

Browse files
committed
lxc: Prevent file completion for all commands that haven't explicitly set it.
To be removed when spf13/cobra#2209 is addressed. Signed-off-by: Mark Laing <[email protected]>
1 parent 978070d commit e20574b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lxc/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ For help with any of those, simply call them with --help.`))
324324
}
325325
}
326326

327+
// Prevent file completions by default.
328+
// This is a workaround for https://github.com/spf13/cobra/issues/2209 and should be removed when resolved.
329+
preventFileCompletions(app)
330+
327331
// Run the main command and handle errors
328332
err = app.Execute()
329333
if err != nil {
@@ -546,3 +550,19 @@ func (c *cmdGlobal) CheckArgs(cmd *cobra.Command, args []string, minArgs int, ma
546550

547551
return false, nil
548552
}
553+
554+
// preventFileCompletions recurses the Command tree and sets a ValidArgsFunction on each Command if not already set.
555+
// This prevents file completion when e.g. invalid commands are being completed, or when no completions are available yet.
556+
// This is used because the majority of lxc commands interact with remote resources, and not the local filesystem.
557+
// This should be removed when https://github.com/spf13/cobra/issues/2209 is resolved.
558+
func preventFileCompletions(c *cobra.Command) {
559+
if c.ValidArgsFunction == nil {
560+
c.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
561+
return nil, cobra.ShellCompDirectiveNoFileComp
562+
}
563+
}
564+
565+
for _, cmd := range c.Commands() {
566+
preventFileCompletions(cmd)
567+
}
568+
}

0 commit comments

Comments
 (0)