Skip to content

Commit

Permalink
add fish support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Olshanskiy authored and SimonTheLeg committed Jul 2, 2024
1 parent 355ad75 commit e02e8cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newCompletionCmd() *completionCmd {
cc := completionCmd{}

cc.cmd = &cobra.Command{
Use: "completion [bash|zsh]",
Use: "completion [bash|zsh|fish]",
Short: "Generate completion script",
Long: `To load completions:
Expand All @@ -43,7 +43,7 @@ Zsh:
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.ExactValidArgs(1),
RunE: cc.completion,
}
Expand Down Expand Up @@ -88,6 +88,19 @@ func (c *completionCmd) completion(cmd *cobra.Command, args []string) error {

os.Stdout.WriteString(genBash)

case "fish":
const name = "konf-go"
var b bytes.Buffer
rootCmd.Use = name
err := rootCmd.GenFishCompletion(&b, true)
if err != nil {
return err
}
anchor := "complete -c " + name
genFish := strings.Replace(b.String(), anchor, anchor+" -w konf", 2) // this is a bit different as we have to replace two occurrences of the anchor

os.Stdout.WriteString(genFish)

default:
return fmt.Errorf("konf currently does not support autocompletions for %s", args[0])
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/shellwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ konf_cleanup() {
konf-go cleanup
}
trap konf_cleanup EXIT
`

var fish = `
function konf -w konf-go
set -f res (konf-go $argv)
# only change $KUBECONFIG if instructed by konf-go
if string match -q 'KUBECONFIGCHANGE:*' $res
# this basically takes the line and cuts out the KUBECONFIGCHANGE Part
set -gx KUBECONFIG (string replace -r '^KUBECONFIGCHANGE:' '' $res)
else
# this makes --help work
printf "%s\n" $res
end
end
function konf_cleanup
konf-go cleanup
end
trap konf_cleanup EXIT
`

Expand All @@ -75,6 +95,8 @@ trap konf_cleanup EXIT
wrapper = zsh
case "bash":
wrapper = bash
case "fish":
wrapper = fish
default:
return fmt.Errorf("konf currently does not support %s", args[0])
}
Expand Down

0 comments on commit e02e8cd

Please sign in to comment.