Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionCarapaceBin: support spec completion #1310

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}