Skip to content

Commit

Permalink
ActionCarapaceBin: support spec completion
Browse files Browse the repository at this point in the history
- specs override embedded completers with the same name
  • Loading branch information
rsteube committed Sep 24, 2022
1 parent 271b550 commit 92fa7ad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
15 changes: 2 additions & 13 deletions completers/sudo_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/bridge"
"github.com/rsteube/carapace-bin/pkg/actions/os"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -102,26 +103,14 @@ func sudoCmd() *cobra.Command {
}

func subCmd(name string) *cobra.Command {
// TODO invokes carapace but should use system wide completions longterm (rsteube/invoke-completion)
cmd := &cobra.Command{
Use: name,
Run: func(cmd *cobra.Command, args []string) {},
DisableFlagParsing: true,
}

carapace.Gen(cmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{name, "export", ""}
args = append(args, c.Args...)
args = append(args, c.CallbackValue)
return carapace.ActionExecCommand("carapace", args...)(func(output []byte) carapace.Action {
// TODO carapace needs exit code on error
if string(output) == "" {
return carapace.ActionValues()
}
return carapace.ActionImport(output)
})
}),
bridge.ActionCarapaceBin(name),
)
return cmd
}
31 changes: 30 additions & 1 deletion pkg/actions/bridge/carapace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package bridge

import "github.com/rsteube/carapace"
import (
"fmt"
"os"

"github.com/rsteube/carapace"
spec "github.com/rsteube/carapace-spec"
)

// ActionCarapace bridges rsteube/carapace completion
func ActionCarapace(cmd string) carapace.Action {
Expand All @@ -19,6 +25,17 @@ func ActionCarapace(cmd string) carapace.Action {

// ActionCarapaceBin bridges a completer provided by carapace-bin
func ActionCarapaceBin(completer string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if a, err := actionSpec(completer); err == nil {
return a
} else if !os.IsNotExist(err) {
return carapace.ActionMessage(err.Error())
}
return actionEmbedded(completer)
})
}

func actionEmbedded(completer string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{completer, "export", ""}
args = append(args, c.Args...)
Expand All @@ -31,3 +48,15 @@ func ActionCarapaceBin(completer string) carapace.Action {
})
})
}

func actionSpec(completer string) (carapace.Action, error) {
configDir, err := os.UserConfigDir()
if err != nil {
return carapace.ActionValues(), err
}
path := fmt.Sprintf("%v/carapace/specs/%v.yaml", configDir, completer)
if _, err := os.Stat(path); err != nil {
return carapace.ActionValues(), err
}
return spec.ActionSpec(path), nil
}

0 comments on commit 92fa7ad

Please sign in to comment.