Skip to content

Commit

Permalink
Merge pull request #34 from rsteube/default-value
Browse files Browse the repository at this point in the history
variable expansion default value
  • Loading branch information
rsteube authored May 6, 2022
2 parents b2ff3c2 + 86b4f81 commit f4951e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
replace github.com/spf13/pflag => github.com/cornfeedhobo/pflag v1.1.0

require (
github.com/rsteube/carapace v0.20.1
github.com/rsteube/carapace v0.20.2
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/cornfeedhobo/pflag v1.1.0/go.mod h1:ROo/cqBpAh84jplPcXiI5HTjyLmtp04Wa
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/rsteube/carapace v0.20.1 h1:ZnPtEZQGi4vSoco7yLexTvmx9X82jUaOcDScpl3v14M=
github.com/rsteube/carapace v0.20.1/go.mod h1:GgiwpPVhucHNOv0AmtIkxhiEFkCMP5BBRauyQLP0mFY=
github.com/rsteube/carapace v0.20.2 h1:i6QSQZ5vZKmXcwupsYch8XdJ/GBbZU+H8XtfXEs8Z4E=
github.com/rsteube/carapace v0.20.2/go.mod h1:GgiwpPVhucHNOv0AmtIkxhiEFkCMP5BBRauyQLP0mFY=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
Expand Down
23 changes: 5 additions & 18 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,15 @@ func parseAction(cmd *cobra.Command, arr []string) carapace.Action {
})
}))

rEnv := regexp.MustCompile(`\${(?P<name>[^}]+)}`)

batch := carapace.Batch()
vals := make([]string, 0)
for _, elem := range arr {
// TODO yuck
if matches := rEnv.FindAllStringSubmatch(elem, -1); matches != nil {
for _, submatches := range matches {
name := submatches[1]
for _, env := range c.Env { // TODO add Context.Getenv
splitted := strings.SplitN(env, "=", 2)
if splitted[0] == name { // TODO must be the last matching (solved with Getenv)
elem = strings.Replace(elem, submatches[0], splitted[1], 1)
}
}
}
}

if strings.HasPrefix(elem, "$") { // macro
batch = append(batch, parseMacro(elem))
if elemSubst, err := c.Envsubst(elem); err != nil {
batch = append(batch, carapace.ActionMessage(fmt.Sprintf("%v: %v", err.Error(), elem)))
} else if strings.HasPrefix(elemSubst, "$") { // macro
batch = append(batch, parseMacro(elemSubst))
} else {
vals = append(vals, parseValue(elem)...)
vals = append(vals, parseValue(elemSubst)...)
}
}
batch = append(batch, carapace.ActionStyledValuesDescribed(vals...))
Expand Down

0 comments on commit f4951e1

Please sign in to comment.