Skip to content

Commit

Permalink
moved alias completion to carapace-spec
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 23, 2023
1 parent c600b9d commit 3cd03ab
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 52 deletions.
49 changes: 0 additions & 49 deletions cmd/carapace/cmd/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"

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

Expand All @@ -34,50 +30,6 @@ func loadSpec(path string) (string, *spec.Command, error) {
return abs, &specCmd, nil
}

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

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

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

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

for _, subCommand := range specCommand.Commands {
if subCmd, _, err := cmd.Find([]string{subCommand.Name}); err == nil {
addAliasCompletion(subCmd, subCommand)
}
}
}

func scrape(path string) {
// TODO yuck - all this needs some cleanup
if _, spec, err := loadSpec(path); err == nil {
Expand All @@ -104,7 +56,6 @@ func specCompletion(path string, args ...string) (string, error) {
}

cmd := spec.ToCobra()
addAliasCompletion(cmd, command.Command(*spec)) // TODO put this somewhere else

a := []string{"_carapace"}
a = append(a, args...)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/rsteube/carapace v0.47.4
github.com/rsteube/carapace-bridge v0.1.7
github.com/rsteube/carapace-shlex v0.1.1
github.com/rsteube/carapace-spec v0.12.0
github.com/rsteube/carapace-spec v0.12.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
golang.org/x/mod v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/rsteube/carapace-pflag v0.2.0 h1:EYqFO9Haib3NDCPqKu0VxOGi9YQBkXk1IzlH
github.com/rsteube/carapace-pflag v0.2.0/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/rsteube/carapace-shlex v0.1.1 h1:fRQEBBKyYKm4TXUabm4tzH904iFWSmXJl3UZhMfQNYU=
github.com/rsteube/carapace-shlex v0.1.1/go.mod h1:zPw1dOFwvLPKStUy9g2BYKanI6bsQMATzDMYQQybo3o=
github.com/rsteube/carapace-spec v0.12.0 h1:ydsHsc0uku6K7DT18TAw4kpooyhbnATc9EIFO6zwrEY=
github.com/rsteube/carapace-spec v0.12.0/go.mod h1:kxUqKja4vvbc7h/8L8gpeDxzGGNL3pD4bu5ipAso/zM=
github.com/rsteube/carapace-spec v0.12.1 h1:vptE5gmcDq4dSagXei+lUrJSj37mQ6k4eAfPT9T2ZjI=
github.com/rsteube/carapace-spec v0.12.1/go.mod h1:kxUqKja4vvbc7h/8L8gpeDxzGGNL3pD4bu5ipAso/zM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
Expand Down

0 comments on commit 3cd03ab

Please sign in to comment.