fix: don't export from within function
Also, don't explode on command function which has "export" in the name! See coop-cloud/organising#498
This commit is contained in:
parent
970ae0fc4e
commit
379915587c
|
@ -8,6 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Autonomic-Cooperative/godotenv"
|
"github.com/Autonomic-Cooperative/godotenv"
|
||||||
|
@ -149,22 +150,32 @@ func ReadAbraShEnvVars(abraSh string) (map[string]string, error) {
|
||||||
}
|
}
|
||||||
return envVars, err
|
return envVars, err
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
exportRegex, err := regexp.Compile(`^export\s+(\w+=\w+)`)
|
||||||
|
if err != nil {
|
||||||
|
return envVars, err
|
||||||
|
}
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
txt := scanner.Text()
|
||||||
if strings.Contains(line, "export") {
|
if exportRegex.MatchString(txt) {
|
||||||
splitVals := strings.Split(line, "export ")
|
splitVals := strings.Split(txt, "export ")
|
||||||
envVarDef := splitVals[len(splitVals)-1]
|
envVarDef := splitVals[len(splitVals)-1]
|
||||||
keyVal := strings.Split(envVarDef, "=")
|
keyVal := strings.Split(envVarDef, "=")
|
||||||
if len(keyVal) != 2 {
|
if len(keyVal) != 2 {
|
||||||
return envVars, fmt.Errorf("couldn't parse %s", line)
|
return envVars, fmt.Errorf("couldn't parse %s", txt)
|
||||||
}
|
}
|
||||||
envVars[keyVal[0]] = keyVal[1]
|
envVars[keyVal[0]] = keyVal[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Debugf("read %s from %s", envVars, abraSh)
|
if len(envVars) > 0 {
|
||||||
|
logrus.Debugf("read %s from %s", envVars, abraSh)
|
||||||
|
} else {
|
||||||
|
logrus.Debugf("read 0 env var exports from %s", abraSh)
|
||||||
|
}
|
||||||
|
|
||||||
return envVars, nil
|
return envVars, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,8 +46,6 @@ var ExpectedAppFiles = map[string]config.AppFile{
|
||||||
AppName: ExpectedAppFile,
|
AppName: ExpectedAppFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
// var expectedServerNames = []string{"evil.corp"}
|
|
||||||
|
|
||||||
func TestGetAllFoldersInDirectory(t *testing.T) {
|
func TestGetAllFoldersInDirectory(t *testing.T) {
|
||||||
folders, err := config.GetAllFoldersInDirectory(TestFolder)
|
folders, err := config.GetAllFoldersInDirectory(TestFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -87,3 +85,37 @@ func TestReadEnv(t *testing.T) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadAbraShEnvVars(t *testing.T) {
|
||||||
|
if err := catalogue.EnsureCatalogue(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
offline := true
|
||||||
|
r, err := recipe.Get("abra-integration-test-recipe", offline)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
abraShPath := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, r.Name, "abra.sh")
|
||||||
|
abraShEnv, err := config.ReadAbraShEnvVars(abraShPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(abraShEnv) == 0 {
|
||||||
|
t.Error("at least one env var should be exported")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := abraShEnv["INNER_FOO"]; ok {
|
||||||
|
t.Error("INNER_FOO should not be exported")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := abraShEnv["INNER_BAZ"]; ok {
|
||||||
|
t.Error("INNER_BAZ should not be exported")
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := abraShEnv["OUTER_FOO"]; !ok {
|
||||||
|
t.Error("OUTER_FOO should be exported")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue