Skip to content

Commit

Permalink
Examples fix (#58)
Browse files Browse the repository at this point in the history
* Fix build example

* Update TODO.md doc

* Update module Go version

* Fix for flaky Vars/Envs methods tests
  • Loading branch information
vladimirvivien authored Nov 8, 2024
1 parent 1c64441 commit e2c675e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Important tasks
# TODOs
The following are high-level tasks that will be considered for upcoming release cycles.

## Top features/tasks
* [ ] Support and test on Windows platform (#27)
* [ ] Ability to map and access program flags (#20)
* [ ] Support for SSH/SCP
* [x] Provide a simple API to submit HTTP requests and retrieve HTTP documents (think of wget/curl)
* [x] Support for scatter/gather exec commands
* [x] Support for concurrent exec of os commands
Expand Down
2 changes: 1 addition & 1 deletion examples/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
for _, opsys := range []string{"darwin", "linux"} {
gexe.SetVar("arch", arch).SetVar("os", opsys)
gexe.SetVar("binpath", fmt.Sprintf("build/%s/%s/mybinary", arch, opsys))
result := gexe.Envs("CGO_ENABLED=0 GOOS=$os GOARCH=$arch").Run("go build -o $binpath .")
result := gexe.Envs("CGO_ENABLED=0", "GOOS=$os", "GOARCH=$arch").Run("go build -o $binpath .")
if result != "" {
fmt.Printf("Build for %s/%s failed: %s\n", arch, opsys, result)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func Variables() *vars.Variables {
//
// Environment vars can be used in string values
// using Eval("building for os=$GOOS")
func Envs(val string) *Echo {
return DefaultEcho.Envs(val)
func Envs(val ...string) *Echo {
return DefaultEcho.Envs(val...)
}

// SetEnv sets a process environment variable.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/vladimirvivien/gexe

go 1.22
go 1.23
28 changes: 17 additions & 11 deletions vars/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ var (
varsRegex = regexp.MustCompile(`([A-Za-z0-9_]+)=["']?([^"']*?\$\{[A-Za-z0-9_\.\s]+\}[^"']*?|[^"']*)["']?`)
)

// varmap is used to store parsed variables
type varmap struct {
key string
value string
}

// Variables stores a variable map that used for variable expansion
// in parsed commands and other parsed strings.
type Variables struct {
Expand Down Expand Up @@ -48,9 +54,9 @@ func (v *Variables) Envs(variables ...string) *Variables {
if len(variables) == 0 {
return v
}
vars := v.parseVars(variables...)
for key, value := range vars {
v.SetEnv(key, value)
varmaps := v.parseVars(variables...)
for _, parsedVar := range varmaps {
v.SetEnv(parsedVar.key, parsedVar.value)
}
return v
}
Expand All @@ -76,11 +82,11 @@ func (v *Variables) Vars(variables ...string) *Variables {
return v
}

vars := v.parseVars(variables...)
varmaps := v.parseVars(variables...)

// set variables
for key, value := range vars {
v.SetVar(key, value)
for _, parsedVar := range varmaps {
v.SetVar(parsedVar.key, parsedVar.value)
}
return v
}
Expand Down Expand Up @@ -120,20 +126,20 @@ func (v *Variables) Eval(str string) string {
return v.ExpandVar(str, v.Val)
}

// parseVars parses each var line and maps each key to value into map[string]string result.
// parseVars parses each var line and maps each key to value into []varmap result.
// This method does not do variable expansion.
func (v *Variables) parseVars(lines ...string) map[string]string {
result := make(map[string]string)
func (v *Variables) parseVars(lines ...string) []varmap {
var result []varmap
if len(lines) == 0 {
return result
return []varmap{}
}

// each line should contain (<key>)=(<val>) pair
// matched with expressino which returns match[1] (key) and match[2] (value)
for _, line := range lines {
matches := varsRegex.FindStringSubmatch(line)
if len(matches) >= 3 {
result[matches[1]] = matches[2]
result = append(result, varmap{key: matches[1], value: matches[2]})
}
}
return result
Expand Down
14 changes: 7 additions & 7 deletions vars/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestParseVars(t *testing.T) {
{
name: "unclosed quote error",
vars: []string{"foo=bar", "bazz='booz", "dazz=jazz"},
expectedVars: map[string]string{"foo": "bar", "dazz": "jazz"},
expectedVars: map[string]string{"foo": "bar", "bazz": "booz", "dazz": "jazz"},
},
{
name: "multiple items",
Expand All @@ -52,10 +52,10 @@ func TestParseVars(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
vars := New()
kvPair := vars.parseVars(test.vars...)
for key, val := range test.expectedVars {
if strings.TrimSpace(kvPair[key]) != strings.TrimSpace(val) {
t.Errorf("unexpected var: %s=%s (want %s=%s)", key, val, key, kvPair[key])
varmaps := vars.parseVars(test.vars...)
for _, parsedVar := range varmaps {
if strings.TrimSpace(test.expectedVars[parsedVar.key]) != strings.TrimSpace(parsedVar.value) {
t.Errorf("unexpected var: %s=%s (want %s=%s)", parsedVar.key, parsedVar.value, parsedVar.key, test.expectedVars[parsedVar.key])
}
}
})
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestVariables_Vars(t *testing.T) {
{
name: "unclosed quote error",
vars: []string{"foo=bar", "bazz='booz", "dazz=jazz"},
expectedVars: map[string]string{"foo": "bar", "dazz": "jazz"},
expectedVars: map[string]string{"foo": "bar", "bazz": "booz", "dazz": "jazz"},
shouldFail: true,
},
{
Expand All @@ -116,7 +116,7 @@ func TestVariables_Vars(t *testing.T) {

for key, val := range test.expectedVars {
if v, ok := vars.vars[key]; !ok || v != val {
t.Errorf("unexpected var: %s=%s (needs %s=%s)", key, val, key, v)
t.Errorf("unexpected var: %s=%s (want %s=%s)", key, v, key, val)
}
}

Expand Down

0 comments on commit e2c675e

Please sign in to comment.