Skip to content

Commit

Permalink
Merge pull request #1868 from rsteube/env-fix-conditions
Browse files Browse the repository at this point in the history
env: fix missing conditions
  • Loading branch information
rsteube authored Sep 24, 2023
2 parents 8eb60fc + 280913c commit 17f1589
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 36 deletions.
19 changes: 4 additions & 15 deletions cmd/carapace-generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ func conditions() {
macros = append(macros, "// TODO unsupported signature: "+t)
continue
} else if arg == "" {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroN(%v),`, matches[1], _func))
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroN(%v).WithDescription(%#v),`, matches[1], _func, descriptions[matches[1]]))
} else if strings.Contains(arg, "...") {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroV(%v),`, matches[1], _func))
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroV(%v).WithDescription(%#v),`, matches[1], _func, descriptions[matches[1]]))
} else {
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroI(%v),`, matches[1], _func))
macros = append(macros, fmt.Sprintf(`"%v": condition.MacroI(%v).WithDescription(%#v),`, matches[1], _func, descriptions[matches[1]]))
}
}
} else if strings.HasPrefix(t, "// Condition") {
Expand All @@ -342,12 +342,6 @@ func conditions() {
return nil
})

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 conditions
import (
Expand All @@ -359,13 +353,8 @@ func init() {
MacroMap = macro.MacroMap[condition.Macro]{
%v
}
MacroDescriptions = map[string]string {
%v
}
}
`, strings.Join(macros, "\n"), strings.Join(sortedDescriptions, "\n"))
`, strings.Join(macros, "\n"))

os.WriteFile(root+"/pkg/conditions/conditions_generated.go", []byte(content), 0644)
execabs.Command("go", "fmt", root+"/pkg/conditions/conditions_generated.go").Run()
Expand Down
15 changes: 3 additions & 12 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,9 @@ func printCompletersJson() {
}

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.MacroDescriptions[name])
// TODO marshal ordered using yaml
for name, macro := range conditions.MacroMap {
fmt.Printf("%v: %#v\n", name, macro.Description)
}
}

Expand Down
18 changes: 11 additions & 7 deletions internal/condition/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ import (
)

type Macro struct {
macro macro.Macro[Condition]
macro.Macro[Condition]
Description string
}

func (m Macro) WithDescription(s string) Macro {
m.Description = s
return m
}

func (m Macro) Parse(s string) Condition {
return func(c carapace.Context) bool {
b, err := m.macro.Parse(s)
b, err := m.Macro.Parse(s)
if err != nil {
return false
}
return (*b)(c)
}
}

func (m Macro) Signature() string { return m.macro.Signature() }

func MacroI[A any](f func(arg A) Condition) Macro {
return Macro{
macro: macro.MacroI[A, Condition](func(arg A) (*Condition, error) {
Macro: macro.MacroI[A, Condition](func(arg A) (*Condition, error) {
a := f(arg)
return &a, nil
}),
Expand All @@ -32,7 +36,7 @@ func MacroI[A any](f func(arg A) Condition) Macro {

func MacroN(f func() Condition) Macro {
return Macro{
macro: macro.MacroN[Condition](func() (*Condition, error) {
Macro: macro.MacroN[Condition](func() (*Condition, error) {
a := f()
return &a, nil
}),
Expand All @@ -41,7 +45,7 @@ func MacroN(f func() Condition) Macro {

func MacroV[A any](f func(args ...A) Condition) Macro {
return Macro{
macro: macro.MacroV[A, Condition](func(args ...A) (*Condition, error) {
Macro: macro.MacroV[A, Condition](func(args ...A) (*Condition, error) {
a := f(args...)
return &a, nil
}),
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/env/cargo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package env
import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/net/http"
"github.com/rsteube/carapace-bin/pkg/conditions"
"github.com/rsteube/carapace/pkg/style"
)

func init() {
_bool := carapace.ActionValues("true", "false").StyleF(style.ForKeyword)
knownVariables["cargo"] = variables{
Condition: conditions.ConditionPath("cargo"),
Variables: map[string]string{
"CARGO_BIN_NAME": "The name of the binary that is currently being compiled",
"CARGO_BUILD_DEP_INFO_BASEDIR": "Dep-info relative directory, see build.dep-info-basedir",
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/env/starship.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package env

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/conditions"
"github.com/rsteube/carapace/pkg/style"
)

func init() {
knownVariables["starship"] = variables{
Condition: conditions.ConditionPath("starship"),
Variables: map[string]string{
"STARSHIP_CACHE": "cache location",
"STARSHIP_CONFIG": "config location",
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/env/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package env
import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/terraform"
"github.com/rsteube/carapace-bin/pkg/conditions"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/rsteube/carapace/pkg/style"
)

func init() {
_bool := carapace.ActionValues("true", "false").StyleF(style.ForKeyword)
knownVariables["terraform"] = variables{
Condition: conditions.ConditionPath("terraform"),
Variables: map[string]string{
"TF_LOG": "Enables detailed logs to appear on stderr which is useful for debugging",
"TF_LOG_PATH": "This specifies where the log should persist its output to",
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/env/tofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package env
import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tofu"
"github.com/rsteube/carapace-bin/pkg/conditions"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
"github.com/rsteube/carapace/pkg/style"
)

func init() {
_bool := carapace.ActionValues("true", "false").StyleF(style.ForKeyword)
knownVariables["tofu"] = variables{
Condition: conditions.ConditionPath("tofu"),
Variables: map[string]string{
"TF_LOG": "Enables detailed logs to appear on stderr which is useful for debugging",
"TF_LOG_PATH": "This specifies where the log should persist its output to",
Expand Down
3 changes: 1 addition & 2 deletions pkg/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ import (
)

var (
MacroMap = make(macro.MacroMap[condition.Macro])
MacroDescriptions = make(map[string]string)
MacroMap = make(macro.MacroMap[condition.Macro])
)

0 comments on commit 17f1589

Please sign in to comment.