Added a whitelist to add cobra apps to to prevent non-cobra apps from

being excuted
This commit is contained in:
Jack Wright 2022-11-27 15:06:59 -08:00
parent 1324e0c316
commit 37ac7454fb
3 changed files with 81 additions and 51 deletions

View File

@ -847,7 +847,7 @@ to your powershell profile.
Short: fmt.Sprintf(shortDesc, "nushell"),
Long: fmt.Sprintf(`Generate the autocompletion script for nushell.
To configure completions:
To configure the Nushell cobra external configurator for the first time:
# 1. Copy the output of the command below:
> %[1]s completion nushell
@ -860,7 +860,17 @@ To configure completions:
# 4. Change the config block's external_completer line to be
external_completer: $cobra_completer
# 5. You will need to start a new shell for this setup to take effect.
# 5. You will need to start a new shell or for this setup to take effect.
If you have already setup the cobra external configurator:
# 1. Edit the nushell config file:
> config nu
# 2. Modify the cobra_apps varible to contain this application:
> let cobra_apps = [ "othercobraapp", "%[1]s" ]
# 3. You will need to start a new shell or for this setup to take effect.
`, c.Root().Name()),
Args: NoArgs,

View File

@ -24,17 +24,23 @@ import (
func (c *Command) GenNushellCompletion(w io.Writer) error {
buf := new(bytes.Buffer)
WriteStringAndCheck(buf, fmt.Sprintf(`
# A list of cobra that completion will be attempted for.
# Add new apps to this list to enable completion for them.
let cobra_apps = ["%[1]s"]
# An external configurator that works with any cobra based
# command line application (e.g. kubectl, minikube)
let cobra_completer = {|spans|
let ShellCompDirectiveError = %[1]d
let ShellCompDirectiveNoSpace = %[2]d
let ShellCompDirectiveNoFileComp = %[3]d
let ShellCompDirectiveFilterFileExt = %[4]d
let ShellCompDirectiveFilterDirs = %[5]d
let cmd = $spans.0
if not ($cobra_apps | where $it == $cmd | is-empty) {
let ShellCompDirectiveError = %[2]d
let ShellCompDirectiveNoSpace = %[3]d
let ShellCompDirectiveNoFileComp = %[4]d
let ShellCompDirectiveFilterFileExt = %[5]d
let ShellCompDirectiveFilterDirs = %[6]d
let last_span = ($spans | last | str trim)
# skip the first entry in the span (the command) and join the rest of the span to create __complete args
@ -79,9 +85,11 @@ let cobra_completer = {|spans|
} else {
$completions
}
} else {
null
}
}
`, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
`, c.Name(), ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
_, err := buf.WriteTo(w)

View File

@ -72,6 +72,8 @@ PowerShell:
Nushell:
# To configure the Nushell cobra external configurator for the first time:
# 1. Copy the output of the command below:
> %[1]s completion nushell
@ -83,7 +85,17 @@ Nushell:
# 4. Change the config block's external_completer line to be
external_completer: $cobra_completer
# 5. You will need to start a new shell for this setup to take effect.
# 5. You will need to start a new shell or for this setup to take effect.
# If you have already setup the cobra external configurator:
# 1. Edit the nushell config file:
> config nu
# 2. Modify the cobra_apps varible to contain this application:
> let cobra_apps = [ "othercobraapp", "%[1]s" ]
# 3. You will need to start a new shell for this setup to take effect.
`,cmd.Root().Name()),
DisableFlagsInUseLine: true,