Skip to content

feat(fish): implement ShellCompDirectiveFilterDirs#2372

Open
thaJeztah wants to merge 1 commit intospf13:mainfrom
thaJeztah:fish_ShellCompDirectiveFilterDir
Open

feat(fish): implement ShellCompDirectiveFilterDirs#2372
thaJeztah wants to merge 1 commit intospf13:mainfrom
thaJeztah:fish_ShellCompDirectiveFilterDir

Conversation

@thaJeztah
Copy link
Copy Markdown
Contributor

Fish supports directory-only completions through __fish_complete_directories, but Cobra's generated completion script treated this directive as unsupported.

Use Fish's native directory completion when ShellCompDirectiveFilterDirs is set.

Fish supports directory-only completions through `__fish_complete_directories`,
but Cobra's generated completion script treated this directive as unsupported.

Use Fish's native directory completion when `ShellCompDirectiveFilterDirs` is set.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Copy Markdown
Contributor Author

Failure is unrelated to my changes, but perhaps GolangCi-lint was updated;

  Error: completions.go:958:24: G703: Path traversal via taint analysis (gosec)
  		f, err := os.OpenFile(path,
  		                     ^

* `BashCompSubdirsInDir` (filtering by directory)
* The functions corresponding to the above annotations are consequently not supported and will be ignored for `fish`:
* `MarkFlagFilename()` and `MarkPersistentFlagFilename()` (filtering by file extension)
* `MarkFlagDirname()` and `MarkPersistentFlagDirname()` (filtering by directory)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure why this line was removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to double check when I'm back at my computer, but I think the flow was; these options set a "bash" annotation on the flag, but the code further down converts that to setting autocompletion for the flag, using the ShellCompDirectiveFilterDirs directive.

So they were ignored because that directive wasn't functional 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, so MarkFlagDirname sets the BashCompSubdirsInDir annotation;

func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
return flags.SetAnnotation(name, BashCompSubdirsInDir, []string{})
}

Which then gets translated to ShellCompDirectiveFilterDirs here;

cobra/completions.go

Lines 416 to 423 in 61968e8

if subDir, present := flag.Annotations[BashCompSubdirsInDir]; present {
if len(subDir) == 1 {
// Directory completion from within a directory
return finalCmd, subDir, ShellCompDirectiveFilterDirs, nil
}
// Directory completion
return finalCmd, []Completion{}, ShellCompDirectiveFilterDirs, nil
}

The ShellCompDirectiveFilterDirs previously didn't take effect on Fish, but with this change does

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants