Skip to content

Commit

Permalink
added alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 15, 2022
1 parent 63d99e6 commit 65cb7de
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
2 changes: 1 addition & 1 deletion action.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action {
vals := make([]string, 0)
for _, elem := range arr {
if elemSubst, err := c.Envsubst(elem); err != nil {
batch = append(batch, carapace.ActionMessage(fmt.Sprintf("%v: %v", err.Error(), elem)))
batch = append(batch, carapace.ActionMessage(%v: %v", err.Error(), elem)
} else if strings.HasPrefix(elemSubst, "$") { // macro
batch = append(batch, ActionMacro(elemSubst))
} else {
Expand Down
8 changes: 5 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type Command struct {
Name string
Aliases []string
Description string
Flags map[string]string
PersistentFlags map[string]string
Expand All @@ -22,9 +23,10 @@ type Command struct {

func (c *Command) ToCobra() *cobra.Command {
cmd := &cobra.Command{
Use: c.Name,
Short: c.Description,
Run: func(cmd *cobra.Command, args []string) {},
Use: c.Name,
Aliases: c.Aliases,
Short: c.Description,
Run: func(cmd *cobra.Command, args []string) {},
}
carapace.Gen(cmd).Standalone()

Expand Down
56 changes: 42 additions & 14 deletions core.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
package spec

import (
"runtime"
"strings"

"github.com/rsteube/carapace"
)

func init() {
addCoreMacro("directories", MacroN(carapace.ActionDirectories))
addCoreMacro("noflag", MacroN(func() carapace.Action { return carapace.ActionValues() }).NoFlag())
addCoreMacro("files", MacroV(carapace.ActionFiles))
addCoreMacro("message", MacroI(carapace.ActionMessage))
addCoreMacro("message", MacroI(func(s string) carapace.Action { return carapace.ActionMessage(s) }))
addCoreMacro("noflag", MacroN(func() carapace.Action { return carapace.ActionValues() }).NoFlag())
addCoreMacro("spec", MacroI(ActionSpec).NoFlag())

addCoreMacro("", MacroI(func(s string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("sh", "-c", s)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)
for _, line := range lines {
if line != "" {
vals = append(vals, parseValue(line)...)
}
}
return carapace.ActionStyledValuesDescribed(vals...)
}).Invoke(c).ToA()
})
if runtime.GOOS == "windows" {
return shell("pwsh", s)
}
return shell("sh", s)
}))
addCoreMacro("bash", MacroI(func(s string) carapace.Action { return shell("bash", s) }))
addCoreMacro("elvish", MacroI(func(s string) carapace.Action { return shell("elvish", s) }))
addCoreMacro("fish", MacroI(func(s string) carapace.Action { return shell("fish", s) }))
addCoreMacro("ion", MacroI(func(s string) carapace.Action { return shell("ion", s) }))
addCoreMacro("nu", MacroI(func(s string) carapace.Action { return shell("nu", s) }))
addCoreMacro("osh", MacroI(func(s string) carapace.Action { return shell("osh", s) }))
addCoreMacro("pwsh", MacroI(func(s string) carapace.Action { return shell("pwsh", s) }))
addCoreMacro("sh", MacroI(func(s string) carapace.Action { return shell("sh", s) }))
addCoreMacro("xonsh", MacroI(func(s string) carapace.Action { return shell("xonsh", s) }))
addCoreMacro("zsh", MacroI(func(s string) carapace.Action { return shell("zsh", s) }))
}

func shell(shell, command string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if runtime.GOOS == "windows" &&
shell != "elvish" &&
shell != "nu" &&
shell != "pwsh" &&
shell != "xonsh" {
return carapace.ActionMessage("unsupported shell [%v]: %v", runtime.GOOS, shell)
}

return carapace.ActionExecCommand(shell, "-c", command)(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)
for _, line := range lines {
if line != "" {
vals = append(vals, parseValue(line)...)
}
}
return carapace.ActionStyledValuesDescribed(vals...)
}).Invoke(c).ToA()
})

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/rsteube/carapace-spec
go 1.18

require (
github.com/rsteube/carapace v0.25.0
github.com/rsteube/carapace v0.25.1
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/rsteube/carapace v0.25.0 h1:8tqNZE9ftHMFOl54dBmvPSNbce3+bgvlynhcRPmdrAw=
github.com/rsteube/carapace v0.25.0/go.mod h1:3N1bLcmfoBOn0WwCniEhVF52AqvN//zyV1JrtT52ECw=
github.com/rsteube/carapace v0.25.1 h1:zF6gJqrQ82fPv15A8APh2giAjj/IXX1ENri/svNm7VY=
github.com/rsteube/carapace v0.25.1/go.mod h1:3N1bLcmfoBOn0WwCniEhVF52AqvN//zyV1JrtT52ECw=
github.com/rsteube/carapace-pflag v0.0.4 h1:Onb0cLNLxg1xJr2EsMlBldAI5KkybrvZ89b5cRElZXI=
github.com/rsteube/carapace-pflag v0.0.4/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
6 changes: 3 additions & 3 deletions macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func AddMacro(s string, m Macro) {
func ActionMacro(s string) carapace.Action {
r := regexp.MustCompile(`^\$(?P<macro>[^(]*)(\((?P<arg>.*)\))?$`)
if !r.MatchString(s) {
return carapace.ActionMessage(fmt.Sprintf("malformed macro: '%v'", s))
return carapace.ActionMessage(malformed macro: '%v'", s)
}

matches := findNamedMatches(r, s)
if m, ok := macros[matches["macro"]]; !ok {
return carapace.ActionMessage(fmt.Sprintf("unknown macro: '%v'", s))
return carapace.ActionMessage("unknown macro: '%v'", s)
} else {
return m.f(matches["arg"])
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func MacroV[T any](f func(s ...T) carapace.Action) Macro {

var t []T
if err := yaml.Unmarshal([]byte(s), &t); err != nil {
return carapace.ActionMessage(fmt.Sprintf("malformed macro arg: '%v', expected '%v'", s, reflect.TypeOf(t)))
return carapace.ActionMessage("malformed macro arg: '%v', expected '%v'", s, reflect.TypeOf(t))
}
return f(t...)
},
Expand Down

0 comments on commit 65cb7de

Please sign in to comment.