From 4286d32e2ecafa998d5a388e40d289be5c1e405e Mon Sep 17 00:00:00 2001 From: Cody Bender Date: Mon, 25 Nov 2024 18:13:53 -0500 Subject: [PATCH 1/2] feat: add mix completer --- completers/mix_completer/cmd/root.go | 26 +++++++++++++++++++++++++ completers/mix_completer/main.go | 7 +++++++ pkg/actions/tools/mix/mix.go | 29 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 completers/mix_completer/cmd/root.go create mode 100644 completers/mix_completer/main.go create mode 100644 pkg/actions/tools/mix/mix.go diff --git a/completers/mix_completer/cmd/root.go b/completers/mix_completer/cmd/root.go new file mode 100644 index 0000000000..b07a54bbfa --- /dev/null +++ b/completers/mix_completer/cmd/root.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "github.com/carapace-sh/carapace" + "github.com/carapace-sh/carapace-bin/pkg/actions/tools/mix" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "mix", + Short: "A build automation tool for working with applications written in the Elixir programming language", + Long: "https://hexdocs.pm/mix/Mix.html", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func Execute() error { + return rootCmd.Execute() +} + +func init() { + carapace.Gen(rootCmd).Standalone() + + carapace.Gen(rootCmd).PositionalCompletion( + mix.ActionMixTasks(), + ) +} diff --git a/completers/mix_completer/main.go b/completers/mix_completer/main.go new file mode 100644 index 0000000000..dfb1846a1e --- /dev/null +++ b/completers/mix_completer/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/carapace-sh/carapace-bin/completers/mix_completer/cmd" + +func main() { + cmd.Execute() +} diff --git a/pkg/actions/tools/mix/mix.go b/pkg/actions/tools/mix/mix.go new file mode 100644 index 0000000000..5399d5a837 --- /dev/null +++ b/pkg/actions/tools/mix/mix.go @@ -0,0 +1,29 @@ +// package mix contains mix related actions +package mix + +import ( + "regexp" + "strings" + + "github.com/carapace-sh/carapace" +) + +// ActionMixTasks completes mix tasks +// +// install +// deps.get +func ActionMixTasks() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("mix", "help")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + r := regexp.MustCompile(`^mix (\S+)\s*# (.*)`) + vals := make([]string, 0) + for _, line := range lines { + if matches := r.FindStringSubmatch(line); matches != nil { + vals = append(vals, matches[1], matches[2]) + } + } + return carapace.ActionValuesDescribed(vals...) + }) + }) +} From 2f38d8b2908ce32dceaad329fa830495bb244120 Mon Sep 17 00:00:00 2001 From: rsteube Date: Tue, 26 Nov 2024 19:07:23 +0100 Subject: [PATCH 2/2] mix: added flags and help command also support user specs for tasks with `mix..yaml` --- completers/mix_completer/cmd/help.go | 23 +++++++++++++++++++++++ completers/mix_completer/cmd/root.go | 11 +++++++++++ pkg/actions/tools/mix/mix.go | 22 ++++++++++------------ 3 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 completers/mix_completer/cmd/help.go diff --git a/completers/mix_completer/cmd/help.go b/completers/mix_completer/cmd/help.go new file mode 100644 index 0000000000..69bcc36636 --- /dev/null +++ b/completers/mix_completer/cmd/help.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/carapace-sh/carapace" + "github.com/carapace-sh/carapace-bin/pkg/actions/tools/mix" + "github.com/spf13/cobra" +) + +var helpCmd = &cobra.Command{ + Use: "help", + Short: "prints documentation for a given task", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func init() { + carapace.Gen(helpCmd).Standalone() + + rootCmd.AddCommand(helpCmd) + + carapace.Gen(helpCmd).PositionalCompletion( + mix.ActionMixTasks(), + ) +} diff --git a/completers/mix_completer/cmd/root.go b/completers/mix_completer/cmd/root.go index b07a54bbfa..dbe15a3650 100644 --- a/completers/mix_completer/cmd/root.go +++ b/completers/mix_completer/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/carapace-sh/carapace" "github.com/carapace-sh/carapace-bin/pkg/actions/tools/mix" + "github.com/carapace-sh/carapace-bridge/pkg/actions/bridge" "github.com/spf13/cobra" ) @@ -19,8 +20,18 @@ func Execute() error { func init() { carapace.Gen(rootCmd).Standalone() + rootCmd.Flags().SetInterspersed(false) + + rootCmd.Flags().BoolP("help", "h", false, "show help") + rootCmd.Flags().BoolP("version", "v", false, "show version") carapace.Gen(rootCmd).PositionalCompletion( mix.ActionMixTasks(), ) + + carapace.Gen(rootCmd).PositionalAnyCompletion( + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return bridge.ActionCarapaceBin("mix." + c.Args[0]).Shift(1) + }), + ) } diff --git a/pkg/actions/tools/mix/mix.go b/pkg/actions/tools/mix/mix.go index 5399d5a837..4961ab9c05 100644 --- a/pkg/actions/tools/mix/mix.go +++ b/pkg/actions/tools/mix/mix.go @@ -13,17 +13,15 @@ import ( // install // deps.get func ActionMixTasks() carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionExecCommand("mix", "help")(func(output []byte) carapace.Action { - lines := strings.Split(string(output), "\n") - r := regexp.MustCompile(`^mix (\S+)\s*# (.*)`) - vals := make([]string, 0) - for _, line := range lines { - if matches := r.FindStringSubmatch(line); matches != nil { - vals = append(vals, matches[1], matches[2]) - } + return carapace.ActionExecCommand("mix", "help")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + r := regexp.MustCompile(`^mix (\S+)\s*# (.*)`) + vals := make([]string, 0) + for _, line := range lines { + if matches := r.FindStringSubmatch(line); matches != nil { + vals = append(vals, matches[1], matches[2]) } - return carapace.ActionValuesDescribed(vals...) - }) - }) + } + return carapace.ActionValuesDescribed(vals...) + }).Tag("tasks") }