Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

condition command #2091

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/conditions.sh
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions cmd/carapace/cmd/condition.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
7 changes: 7 additions & 0 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -249,6 +253,7 @@ func init() {

rootCmd.MarkFlagsMutuallyExclusive(
"codegen",
"condition",
"help",
"list",
"macro",
Expand Down Expand Up @@ -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":
Expand Down