diff --git a/.github/conditions.sh b/.github/conditions.sh new file mode 100644 index 0000000000..d6a9752903 --- /dev/null +++ b/.github/conditions.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +scriptdir=$(dirname $(readlink -f $0)) + +echo "# Conditions" +echo + +$scriptdir/../cmd/carapace/carapace --condition \ + | sed -r 's_(https://[^ ]+)_[\1](\1)_' \ + | sed 's_^\([^ ]\+\) \+\(.*\)_- [\1]\(https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/conditions/#Condition\1) \2_' \ + | sed -e ':loop' -e 's_\(carapace-bin/pkg/condition/[^#]*\)[.]_\1/_' -e 't loop' diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5f87a6bace..1d22f9255b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -77,6 +77,7 @@ jobs: chmod +x cmd/carapace/carapace sed -i 's/\[output.linkcheck\]/#[output.linkcheck]/' docs/book.toml sh .github/completers.sh > docs/src/completers.md + sh .github/conditions.sh > docs/src/variable/conditions.md sh .github/macros.sh > docs/src/spec/macros.md cmd/carapace/carapace --conditions=markdown > docs/src/variable/conditions.md mdbook build docs diff --git a/cmd/carapace/cmd/condition.go b/cmd/carapace/cmd/condition.go new file mode 100644 index 0000000000..b76bf08452 --- /dev/null +++ b/cmd/carapace/cmd/condition.go @@ -0,0 +1,82 @@ +package cmd + +import ( + "fmt" + "path/filepath" + "sort" + "strconv" + "strings" + + "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/pkg/conditions" + "github.com/spf13/cobra" +) + +var conditionCmd = &cobra.Command{ + Use: "--condition [condition]", + Short: "list or execute condition", + Args: cobra.ArbitraryArgs, + Run: func(cmd *cobra.Command, args []string) { + switch len(args) { + case 0: + printConditions() + case 1: + printCondition(strings.SplitN(args[0], "(", 2)[0]) + } + }, +} + +func init() { + carapace.Gen(conditionCmd).Standalone() + macroCmd.Flags().SetInterspersed(false) + + carapace.Gen(conditionCmd).PositionalCompletion( + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + vals := make([]string, 0) + for name, condition := range conditions.MacroMap { + vals = append(vals, name, condition.Description) + } + return carapace.ActionValuesDescribed(vals...) + }), + carapace.ActionCallback(func(c carapace.Context) carapace.Action { + m, err := conditions.MacroMap.Lookup("$" + c.Args[0]) + if err != nil { + return carapace.ActionMessage(err.Error()) + } + condition := m.Parse("$" + c.Args[0]) + + return carapace.ActionValues(strconv.FormatBool(condition(c))) + }), + ) +} + +func printConditions() { + maxlen := 0 + names := make([]string, 0) + for name := range conditions.MacroMap { + 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, conditions.MacroMap[name].Description) + } +} + +func printCondition(name string) { + if m, ok := conditions.MacroMap[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/conditions/%v#Condition%v +`, name, signature, m.Description, filepath.Dir(path), filepath.Base(path)) + } +} diff --git a/cmd/carapace/cmd/root.go b/cmd/carapace/cmd/root.go index 0960ac75f0..5f12f08ec7 100644 --- a/cmd/carapace/cmd/root.go +++ b/cmd/carapace/cmd/root.go @@ -59,6 +59,9 @@ var rootCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // since flag parsing is disabled do this manually switch args[0] { + case "--condition": + conditionCmd.SetArgs(args[1:]) + conditionCmd.Execute() case "--macro": macroCmd.SetArgs(args[1:]) macroCmd.Execute() @@ -239,6 +242,7 @@ func Execute(version string) error { func init() { rootCmd.Flags().Bool("codegen", false, "generate code for spec file") + rootCmd.Flags().Bool("condition", false, "list or execute condition") rootCmd.Flags().BoolP("help", "h", false, "help for carapace") rootCmd.Flags().Bool("list", false, "list completers") rootCmd.Flags().Bool("macro", false, "list or execute macros") @@ -249,6 +253,7 @@ func init() { rootCmd.MarkFlagsMutuallyExclusive( "codegen", + "condition", "help", "list", "macro", @@ -277,6 +282,8 @@ func init() { switch c.Args[0] { case "--codegen": return carapace.ActionExecute(codegenCmd).Shift(1) + case "--condition": + return carapace.ActionExecute(conditionCmd).Shift(1) case "--help": return carapace.ActionValues() case "--list":