2022-11-11 19:05:24 -07:00
|
|
|
// Copyright 2013-2022 The Cobra Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cobra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-11-27 14:46:13 -07:00
|
|
|
"fmt"
|
2022-11-11 19:05:24 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-11-26 15:39:29 -07:00
|
|
|
func (c *Command) GenNushellCompletion(w io.Writer) error {
|
2022-11-11 19:05:24 -07:00
|
|
|
buf := new(bytes.Buffer)
|
2022-11-27 14:46:13 -07:00
|
|
|
WriteStringAndCheck(buf, fmt.Sprintf(`
|
2022-11-26 15:39:29 -07:00
|
|
|
# An external configurator that works with any cobra based
|
|
|
|
# command line application (e.g. kubectl, minikube)
|
2022-11-27 14:46:13 -07:00
|
|
|
let cobra_completer = {|spans|
|
|
|
|
|
|
|
|
let ShellCompDirectiveError = %[1]d
|
|
|
|
let ShellCompDirectiveNoSpace = %[2]d
|
|
|
|
let ShellCompDirectiveNoFileComp = %[3]d
|
|
|
|
let ShellCompDirectiveFilterFileExt = %[4]d
|
|
|
|
let ShellCompDirectiveFilterDirs = %[5]d
|
2022-11-26 15:39:29 -07:00
|
|
|
|
|
|
|
let cmd = $spans.0
|
2022-11-27 14:46:13 -07:00
|
|
|
let last_span = ($spans | last | str trim)
|
2022-11-26 15:39:29 -07:00
|
|
|
|
|
|
|
# skip the first entry in the span (the command) and join the rest of the span to create __complete args
|
|
|
|
let cmd_args = ($spans | skip 1 | str join ' ')
|
|
|
|
|
|
|
|
# If the last span entry was empty add "" to the end of the command args
|
2022-11-27 14:46:13 -07:00
|
|
|
let cmd_args = if ($last_span | is-empty) {
|
2022-11-26 15:39:29 -07:00
|
|
|
$'($cmd_args) ""'
|
|
|
|
} else {
|
|
|
|
$cmd_args
|
|
|
|
}
|
|
|
|
|
2022-11-27 14:46:13 -07:00
|
|
|
# The full command to be executed with active help disable (Nushell does not support active help)
|
|
|
|
let full_cmd = $'($cmd)_ACTIVE_HELP=0 ($cmd) __complete ($cmd_args)'
|
2022-11-26 15:39:29 -07:00
|
|
|
|
|
|
|
# Since nushell doesn't have anything like eval, execute in a subshell
|
|
|
|
let result = (do -i { nu -c $"'($full_cmd)'" } | complete)
|
|
|
|
|
|
|
|
# Create a record with all completion related info.
|
|
|
|
# directive and directive_str are for posterity
|
|
|
|
let stdout_lines = ($result.stdout | lines)
|
2022-11-27 14:46:13 -07:00
|
|
|
let directive = ($stdout_lines | last | str trim | str replace ":" "" | into int)
|
|
|
|
let completions = ($stdout_lines | drop | parse -r '([\w\-\.:\+\=]*)\t?(.*)' | rename value description)
|
2022-11-26 15:39:29 -07:00
|
|
|
|
2022-11-27 14:46:13 -07:00
|
|
|
# Add space at the end of each completion
|
|
|
|
let completions = if $directive != $ShellCompDirectiveNoSpace {
|
|
|
|
($completions | each {|it| {value: $"($it.value) ", description: $it.description}})
|
|
|
|
} else {
|
|
|
|
$completions
|
|
|
|
}
|
2022-11-26 15:39:29 -07:00
|
|
|
|
2022-11-27 14:46:13 -07:00
|
|
|
if $last_span =~ '=$' {
|
|
|
|
# return flag as part of the completion so that it doesn't get replaced
|
|
|
|
$completions | each {|it| $"($last_span)($it.value)" }
|
|
|
|
} else if $directive == $ShellCompDirectiveNoFileComp {
|
|
|
|
# Allow empty results as this will stop file completion
|
|
|
|
$completions
|
|
|
|
} else if ($completions | is-empty) or $directive == $ShellCompDirectiveError {
|
|
|
|
# Not returning null causes file completions to break
|
|
|
|
# Return null if there are no completions or ShellCompDirectiveError
|
|
|
|
null
|
|
|
|
} else {
|
|
|
|
$completions
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
`, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
|
|
|
|
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))
|
2022-11-11 19:05:24 -07:00
|
|
|
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-26 15:39:29 -07:00
|
|
|
func (c *Command) GenNushellCompletionFile(filename string) error {
|
2022-11-11 19:05:24 -07:00
|
|
|
outFile, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2022-11-26 15:39:29 -07:00
|
|
|
return c.GenNushellCompletion(outFile)
|
2022-11-11 19:05:24 -07:00
|
|
|
}
|