From 3cd03ab546080928fc2408dff6f007a6dd0dd6f4 Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 23 Dec 2023 17:30:50 +0100 Subject: [PATCH] moved alias completion to carapace-spec --- cmd/carapace/cmd/spec.go | 49 ---------------------------------------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 3 insertions(+), 52 deletions(-) diff --git a/cmd/carapace/cmd/spec.go b/cmd/carapace/cmd/spec.go index 9fb5870237..a17f532ac5 100644 --- a/cmd/carapace/cmd/spec.go +++ b/cmd/carapace/cmd/spec.go @@ -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" ) @@ -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 { @@ -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...) diff --git a/go.mod b/go.mod index 00cfcc7bee..c84a71ce1c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ceaa93c609..a3306ba28e 100644 --- a/go.sum +++ b/go.sum @@ -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=