Skip to content

Commit

Permalink
Merge pull request #243 from rsteube/alias-completion
Browse files Browse the repository at this point in the history
command: moved alias completion from carapace-bin
  • Loading branch information
rsteube authored Dec 23, 2023
2 parents f81db94 + 0183381 commit 2768626
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package spec

import (
"os"
"path/filepath"
"regexp"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-spec/pkg/command"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

type Command command.Command
Expand Down Expand Up @@ -56,6 +61,7 @@ func (c Command) ToCobraE() (*cobra.Command, error) {
c.addDashAnyCompletion,
c.addSubcommands,
c.disableFlagParsing,
c.addAliasCompletion,
} {
if err := f(cmd); err != nil {
return nil, err
Expand Down Expand Up @@ -216,3 +222,48 @@ func (c Command) disableFlagParsing(cmd *cobra.Command) error {
}
return nil
}

func (c Command) addAliasCompletion(cmd *cobra.Command) error {
if c.Run != "" &&
len(c.Flags) == 0 &&
len(c.PersistentFlags) == 0 &&
len(c.Completion.Positional) == 0 &&
len(c.Completion.PositionalAny) == 0 &&
len(c.Completion.Dash) == 0 &&
len(c.Completion.DashAny) == 0 {

cmd.DisableFlagParsing = true
carapace.Gen(cmd).PositionalAnyCompletion(
carapace.ActionCallback(func(context carapace.Context) carapace.Action {
switch {
case regexp.MustCompile(`^\[.*\]$`).MatchString(string(c.Run)):
var mArgs []string
if err := yaml.Unmarshal([]byte(c.Run), &mArgs); err != nil {
return carapace.ActionMessage(err.Error())
}
if len(mArgs) == 0 {
return carapace.ActionMessage("empty alias: %#v", c.Run)
}

// TODO keep in sync with ActionCarapaceBin in carapace-bridge
carapaceCmd := "carapace"
if executable, err := os.Executable(); err == nil && filepath.Base(executable) == "carapace" {
carapaceCmd = executable // workaround for sandbox tests: directly call executable which was built with "go run"
}

execArgs := []string{mArgs[0], "export", mArgs[0]}
execArgs = append(execArgs, mArgs[1:]...)
execArgs = append(execArgs, context.Args...)
execArgs = append(execArgs, context.Value)
return carapace.ActionExecCommand(carapaceCmd, execArgs...)(func(output []byte) carapace.Action {
return carapace.ActionImport(output)
})

default:
return carapace.ActionValues()
}
}),
)
}
return nil
}

0 comments on commit 2768626

Please sign in to comment.