Skip to content

Commit

Permalink
macro: fix actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed May 7, 2022
1 parent 5e918cb commit 1e27864
Show file tree
Hide file tree
Showing 35 changed files with 169 additions and 97 deletions.
57 changes: 35 additions & 22 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,11 @@ var rootCmd = &cobra.Command{
specCompletion(args[1], args[2:]...)
}
case "--macros":
sortedMacros := make([]string, 0, len(macros))
for name, m := range macros {
if signature := m.Signature(); signature == "" {
sortedMacros = append(sortedMacros, fmt.Sprintf("$_%v", name))
} else {
sortedMacros = append(sortedMacros, fmt.Sprintf("$_%v(%v)", name, m.Signature()))
}
if len(args) > 1 {
printMacro(args[1])
} else {
printMacros()
}
sort.Strings(sortedMacros)
fmt.Fprintln(cmd.OutOrStdout(), strings.Join(sortedMacros, "\n"))

//if len(args) == 1 {
// sortedMacros := make([]string, 0, len(macros))
// for name := range macros {
// sortedMacros = append(sortedMacros, name)
// }
// sort.Strings(sortedMacros)
// fmt.Fprintln(cmd.OutOrStdout(), strings.Join(sortedMacros, "\n"))
//} else {
// if m, ok := macros[args[1]]; ok {
// fmt.Fprintf(cmd.OutOrStdout(), "$_%v(%v)\n", args[1], m.Signature())
// }
//}
case "-h":
cmd.Help()
case "--help":
Expand Down Expand Up @@ -190,6 +172,37 @@ func printCompleters() {
}
}

func printMacros() {
maxlen := 0
names := make([]string, 0)
for name := range macros {
names = append(names, name)
if len := len(name); len > maxlen {
maxlen = len
}
}

sort.Strings(names)
for _, name := range names {
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, macroDescriptions[name])
}
}

func printMacro(name string) {
if m, ok := macros[name]; ok {
path := strings.Replace(name, ".", "/", -1)
signature := ""
if s := m.Signature(); s != "" {
signature = fmt.Sprintf("(%v)", s)
}

fmt.Printf(`signature: $_%v%v
description: %v
reference: https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/actions/%v#Action%v
`, name, signature, macroDescriptions[name], filepath.Dir(path), filepath.Base(path))
}
}

func invokeCompleter(completer string) {
old := os.Stdout
r, w, _ := os.Pipe()
Expand Down
24 changes: 20 additions & 4 deletions cmd/generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func macros() {

imports := make(map[string]bool)
macros := make([]string, 0)
descriptions := make(map[string]string)

r := regexp.MustCompile(`^func Action(?P<name>[^(]+)\((?P<arg>[^(]*)\) carapace.Action {$`)
filepath.WalkDir(root+"/pkg/actions", func(path string, d fs.DirEntry, err error) error {
Expand All @@ -185,14 +186,15 @@ func macros() {
}
defer file.Close()

pkg := strings.Replace(filepath.Dir(strings.TrimPrefix(path, root+"/pkg/actions/")), "/", ".", -1)
_import := fmt.Sprintf(` %v "github.com/rsteube/carapace-bin/pkg/actions/%v"`, strings.Replace(pkg, ".", "_", -1), strings.Replace(pkg, ".", "/", -1))

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if t := scanner.Text(); strings.HasPrefix(t, "func Action") {
if r.MatchString(t) {
pkg := strings.Replace(filepath.Dir(strings.TrimPrefix(path, root+"/pkg/actions/")), "/", ".", -1)
matches := r.FindStringSubmatch(t)

_import := fmt.Sprintf(` %v "github.com/rsteube/carapace-bin/pkg/actions/%v"`, strings.Replace(pkg, ".", "_", -1), strings.Replace(pkg, ".", "/", -1))
_func := fmt.Sprintf("%v.Action%v", strings.Replace(pkg, ".", "_", -1), matches[1])

if arg := matches[2]; strings.Contains(arg, ",") {
Expand All @@ -201,12 +203,16 @@ func macros() {
} else if arg == "" {
macros = append(macros, fmt.Sprintf(`"%v.%v": spec.MacroN(%v),`, pkg, matches[1], _func))
} else if strings.Contains(arg, "...") {
macros = append(macros, fmt.Sprintf(`"%v.%v": spec.MacroVarI(%v),`, pkg, matches[1], _func))
macros = append(macros, fmt.Sprintf(`"%v.%v": spec.MacroV(%v),`, pkg, matches[1], _func))
} else {
macros = append(macros, fmt.Sprintf(`"%v.%v": spec.MacroI(%v),`, pkg, matches[1], _func))
}
imports[_import] = true
}
} else if strings.HasPrefix(t, "// Action") {
if splitted := strings.SplitN(strings.TrimPrefix(t, "// Action"), " ", 2); len(splitted) > 1 {
descriptions[pkg+"."+splitted[0]] = splitted[1]
}
}
}

Expand All @@ -220,6 +226,12 @@ func macros() {
}
sort.Strings(sortedImports)

sortedDescriptions := make([]string, 0)
for key, value := range descriptions {
sortedDescriptions = append(sortedDescriptions, fmt.Sprintf(`"%v": "%v",`, key, value))
}
sort.Strings(sortedDescriptions)

content := fmt.Sprintf(`package cmd
import (
Expand All @@ -230,7 +242,11 @@ import (
var macros = map[string]spec.Macro{
%v
}
`, strings.Join(sortedImports, "\n"), strings.Join(macros, "\n"))
var macroDescriptions = map[string]string {
%v
}
`, strings.Join(sortedImports, "\n"), strings.Join(macros, "\n"), strings.Join(sortedDescriptions, "\n"))

os.WriteFile(root+"/cmd/carapace/cmd/macros.go", []byte(content), 0644)
exec.Command("go", "fmt", root+"/cmd/carapace/cmd/macros.go").Run()
Expand Down
15 changes: 14 additions & 1 deletion completers/carapace_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func flagCmd() *cobra.Command {
cmd.Flags().String("bridge", "", "generic completion bridge")
cmd.Flags().BoolP("help", "h", false, "help for carapace")
cmd.Flags().Bool("list", false, "list completers")
cmd.Flags().Bool("macros", false, "list spec macros")
cmd.Flags().String("macros", "", "list spec macros")
cmd.Flags().String("spec", "", "spec completion")
cmd.Flags().StringSlice("style", []string{}, "set style")
cmd.Flags().BoolP("version", "v", false, "version for carapace")
Expand All @@ -55,6 +55,19 @@ func flagCmd() *cobra.Command {
return carapace.ActionValues()
}
}),
"macros": carapace.ActionExecCommand("carapace", "--macros")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines[:len(lines)-1] {
if fields := strings.Fields(line); len(fields) > 1 {
vals = append(vals, fields[0], strings.Join(fields[1:], " "))
} else {
vals = append(vals, fields[0], "")
}
}
return carapace.ActionValuesDescribed(vals...).Invoke(carapace.Context{}).ToMultiPartsA(".")
}),
"spec": carapace.ActionFiles(".yaml"),
"style": carapace.ActionStyleConfig(),
})
Expand Down
2 changes: 1 addition & 1 deletion completers/cargo_completer/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
carapace.Gen(addCmd).FlagCompletion(carapace.ActionMap{
"branch": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if f := addCmd.Flag("git"); f.Changed && strings.HasPrefix(f.Value.String(), "https://github.com/") {
return git.ActionLsRemoteRefs(f.Value.String(), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: f.Value.String(), Branches: true, Tags: true})
}
return carapace.ActionValues()
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/circleci_completer/cmd/local_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
carapace.Gen(local_executeCmd).FlagCompletion(carapace.ActionMap{
"branch": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := local_executeCmd.Flag("repo-url"); flag.Changed {
return git.ActionLsRemoteRefs(flag.Value.String(), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: flag.Value.String(), Branches: true, Tags: true})
}
return carapace.ActionValues()
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/dart_completer/cmd/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ActionActivePackageExecutables(pkg string) carapace.Action {
lines := strings.Split(string(output), "\n")
for _, line := range lines[:len(lines)-1] {
if splitted := strings.SplitN(line, " ", 2); splitted[0] == pkg {
return pub.ActionHostedExecutables(pkg, splitted[1])
return pub.ActionHostedExecutables(pub.HostedExecutablesOpts{Name: pkg, Version: splitted[1]})
}
}
return carapace.ActionValues()
Expand Down
2 changes: 1 addition & 1 deletion completers/dart_completer/cmd/pub_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
// TODO "git-path":
"git-ref": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := pub_addCmd.Flag("git-url"); flag.Changed {
return git.ActionLsRemoteRefs(flag.Value.String(), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: flag.Value.String(), Branches: true, Tags: true})
}
return carapace.ActionMessage("git-url not set")
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/flutter_completer/cmd/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ActionActivePackageExecutables(pkg string) carapace.Action {
lines := strings.Split(string(output), "\n")
for _, line := range lines[:len(lines)-1] {
if splitted := strings.SplitN(line, " ", 2); splitted[0] == pkg {
return pub.ActionHostedExecutables(pkg, splitted[1])
return pub.ActionHostedExecutables(pub.HostedExecutablesOpts{Name: pkg, Version: splitted[1]})
}
}
return carapace.ActionValues()
Expand Down
2 changes: 1 addition & 1 deletion completers/flutter_completer/cmd/pub_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
// TODO "git-path":
"git-ref": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := pub_addCmd.Flag("git-url"); flag.Changed {
return git.ActionLsRemoteRefs(flag.Value.String(), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: flag.Value.String(), Branches: true, Tags: true})
}
return carapace.ActionMessage("git-url not set")
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/flutter_completer/cmd/pub_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func init() {
case 0:
return pub.ActionDependencies().Invoke(c).Suffix(":").ToA()
case 1:
return pub.ActionHostedExecutables(c.Parts[0], "")
return pub.ActionHostedExecutables(pub.HostedExecutablesOpts{Name: c.Parts[0], Version: ""})
default:
return carapace.ActionValues()
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/extension_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
if splitted := strings.Split(c.Args[0], "/"); len(splitted) == 2 { // just owner/repo
url = "https://github.com/" + url
}
return git.ActionLsRemoteRefs(url, git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: url, Branches: true, Tags: true})
}),
})

Expand Down
2 changes: 1 addition & 1 deletion completers/git_completer/cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func init() {
carapace.Gen(cloneCmd).FlagCompletion(carapace.ActionMap{
"branch": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(c.Args) > 0 {
return git.ActionLsRemoteRefs(c.Args[0], git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[0], Branches: true, Tags: true})
}
return carapace.ActionValues()
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/git_completer/cmd/lsRemote.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {

carapace.Gen(lsRemoteCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return git.ActionLsRemoteRefs(c.Args[0], git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[0], Branches: true, Tags: true})
}),
)
}
4 changes: 2 additions & 2 deletions completers/git_completer/cmd/remote_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func init() {
carapace.Gen(remote_addCmd).FlagCompletion(carapace.ActionMap{
"master": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(c.Args) > 1 {
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[1], Branches: true})
} else {
return carapace.ActionValues()
}
}),
"mirror": carapace.ActionValues("push", "fetch"),
"track": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(c.Args) > 1 {
return git.ActionLsRemoteRefs(c.Args[1], git.LsRemoteRefOption{Branches: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[1], Branches: true})
} else {
return carapace.ActionValues()
}
Expand Down
2 changes: 1 addition & 1 deletion completers/git_completer/cmd/submodule_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
if len(c.Args) < 1 {
return carapace.ActionValues()
}
return git.ActionLsRemoteRefs(c.Args[0], git.LsRemoteRefOption{Branches: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: c.Args[0], Branches: true})
}),
"reference": carapace.ActionDirectories(),
})
Expand Down
2 changes: 1 addition & 1 deletion completers/kompose_completer/cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
"build": carapace.ActionValues("local", "build-config", "none"),
"build-branch": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := convertCmd.Flag("build-repo"); flag.Changed {
return git.ActionLsRemoteRefs(flag.Value.String(), git.LsRemoteRefOption{Branches: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: flag.Value.String(), Branches: true})
}
return carapace.ActionValues()
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/modinfo_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func init() {

carapace.Gen(rootCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return os.ActionKernelModules(rootCmd.Flag("basedir").Value.String(), rootCmd.Flag("set-version").Value.String())
return os.ActionKernelModules(os.KernelModulesOpts{Basedir: rootCmd.Flag("basedir").Value.String(), Release: rootCmd.Flag("set-version").Value.String()})
}),
)
}
2 changes: 1 addition & 1 deletion completers/modprobe_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func init() {
if rootCmd.Flag("remove").Changed {
return os.ActionKernelModulesLoaded()
}
return os.ActionKernelModules(rootCmd.Flag("dirname").Value.String(), rootCmd.Flag("set-version").Value.String())
return os.ActionKernelModules(os.KernelModulesOpts{Basedir: rootCmd.Flag("dirname").Value.String(), Release: rootCmd.Flag("set-version").Value.String()})
}),
)
}
4 changes: 2 additions & 2 deletions completers/npm_completer/cmd/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func ActionPackageNames(cmd *cobra.Command) carapace.Action {

func ActionPackageVersions(cmd *cobra.Command, pkg string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return npm.ActionPackageVersions(cmd.Flag("registry").Value.String(), pkg)
return npm.ActionPackageVersions(npm.PackageOpts{Registry: cmd.Flag("registry").Value.String(), Package: pkg})
})
}

func ActionPackageTags(cmd *cobra.Command, pkg string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return npm.ActionPackageTags(cmd.Flag("registry").Value.String(), pkg)
return npm.ActionPackageTags(npm.PackageOpts{Registry: cmd.Flag("registry").Value.String(), Package: pkg})
})
}

Expand Down
2 changes: 1 addition & 1 deletion completers/qmk_completer/cmd/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
if len(c.Args) > 0 {
fork = c.Args[0]
}
return git.ActionLsRemoteRefs(fmt.Sprintf("%v/%v", url, fork), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: fmt.Sprintf("%v/%v", url, fork), Branches: true, Tags: true})
}),
})

Expand Down
2 changes: 1 addition & 1 deletion completers/qmk_completer/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func init() {
if len(c.Args) > 0 {
fork = c.Args[0]
}
return git.ActionLsRemoteRefs(fmt.Sprintf("%v/%v", url, fork), git.LsRemoteRefOption{Branches: true, Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: fmt.Sprintf("%v/%v", url, fork), Branches: true, Tags: true})
}),
"home": carapace.ActionDirectories(),
})
Expand Down
2 changes: 1 addition & 1 deletion completers/rmmod_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func init() {
rootCmd.Flags().BoolP("version", "V", false, "show version")

carapace.Gen(rootCmd).PositionalAnyCompletion(
os.ActionKernelModules("", ""),
os.ActionKernelModules(os.KernelModulesOpts{Basedir: "", Release: ""}),
)
}
6 changes: 3 additions & 3 deletions completers/toitpkg_completer/cmd/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func ActionPackages() carapace.Action {
func ActionPackageVersions(id string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if strings.HasPrefix(id, "github.com/") {
return git.ActionLsRemoteRefs("https://"+id, git.LsRemoteRefOption{Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: "https://" + id, Tags: true})
}

if strings.Contains(id, "/") && !strings.Contains(id, ".") { // assume github package
return git.ActionLsRemoteRefs("https://github.com/"+id, git.LsRemoteRefOption{Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: "https://github.com/" + id, Tags: true})
}

return packageAction(func(m map[string]pkg) carapace.Action {
if p, ok := m[id]; ok {
if strings.HasPrefix(p.Url, "github.com/") {
return git.ActionLsRemoteRefs("https://"+p.Url, git.LsRemoteRefOption{Tags: true})
return git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: "https://" + p.Url, Tags: true})
}
}
return carapace.ActionValues()
Expand Down
2 changes: 1 addition & 1 deletion completers/waypoint_completer/cmd/project_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func init() {
"git-private-key-path": carapace.ActionFiles(),
"git-ref": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := project_applyCmd.Flag("git-url"); flag.Changed {
git.ActionLsRemoteRefs(flag.Value.String(), git.LsRemoteRefOption{Branches: true, Tags: true})
git.ActionLsRemoteRefs(git.LsRemoteRefOption{Url: flag.Value.String(), Branches: true, Tags: true})
}
return carapace.ActionValues()
}),
Expand Down
2 changes: 1 addition & 1 deletion completers/xdotool_completer/cmd/action/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func ActionDesktops() carapace.Action {
if err != nil {
return carapace.ActionMessage(err.Error())
}
return number.ActionRangeF("%d", 0, num-1)
return number.ActionRange(number.RangeOpts{Format: "%d", Start: 0, End: num - 1})
})
}
Loading

0 comments on commit 1e27864

Please sign in to comment.