Skip to content

Commit

Permalink
Merge pull request #31 from progrium/support-bash-func
Browse files Browse the repository at this point in the history
feat: add support for exported bash functions
  • Loading branch information
josegonzalez authored Jan 10, 2021
2 parents fc6696b + c5e779a commit 9ffba79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions basher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package basher

import (
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -223,6 +224,14 @@ func (c *Context) buildEnvfile() (string, error) {
continue
}

if isBashFunc(pair[0], pair[1]) {
bash_function_name := strings.TrimPrefix(pair[0], "BASH_FUNC_")
bash_function_name = strings.TrimSuffix(bash_function_name, "%%")
file.Write([]byte(fmt.Sprintf("%s%s\n", bash_function_name, pair[1])))
file.Write([]byte(fmt.Sprintf("export -f %s\n", bash_function_name)))
continue
}

file.Write([]byte("export " + strings.Replace(
strings.Replace(kvp, "'", "\\'", -1), "=", "=$'", 1) + "'\n"))
}
Expand All @@ -237,6 +246,10 @@ func (c *Context) buildEnvfile() (string, error) {
return file.Name(), nil
}

func isBashFunc(key string, value string) bool {
return strings.HasPrefix(key, "BASH_FUNC_") && strings.HasPrefix(value, "()")
}

// Runs a command in Bash from this Context. With each call, a temporary file
// is generated used as BASH_ENV when calling Bash that includes all variables,
// sourced scripts, and exported functions from the Context. Standard I/O by
Expand Down
21 changes: 21 additions & 0 deletions basher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,24 @@ func TestOddArgs(t *testing.T) {
t.Fatal("unexpected stdout:", stdout.String())
}
}

func TestIsBashFunc(t *testing.T) {
if isBashFunc("", "") {
t.Fatal("empty string is not a bash func")
}

if isBashFunc("key", "value") {
t.Fatal("key=value is not a bash func")
}

if isBashFunc("BASH_FUNC_readlinkf", "value") {
t.Fatal("key does not end with %%")
}

if isBashFunc("BASH_FUNC_readlinkf%%", "value") {
t.Fatal("value does not begin with ()")
}
if !isBashFunc("BASH_FUNC_readlinkf%%", "() { true }") {
t.Fatal("bash func should be detected")
}
}

0 comments on commit 9ffba79

Please sign in to comment.