forked from toolshed/abra
fix: fix: trim comments that are not modifers
See coop-cloud/organising#505
This commit is contained in:
@ -150,7 +150,7 @@ func (a ByName) Less(i, j int) bool {
|
||||
}
|
||||
|
||||
func ReadAppEnvFile(appFile AppFile, name AppName) (App, error) {
|
||||
env, err := ReadEnv(appFile.Path)
|
||||
env, err := ReadEnv(appFile.Path, ReadEnvOptions{})
|
||||
if err != nil {
|
||||
return App{}, fmt.Errorf("env file for %s couldn't be read: %s", name, err.Error())
|
||||
}
|
||||
|
@ -55,6 +55,11 @@ func GetServers() ([]string, error) {
|
||||
return servers, nil
|
||||
}
|
||||
|
||||
// ReadEnvOptions modifies the ReadEnv processing of env vars.
|
||||
type ReadEnvOptions struct {
|
||||
IncludeModifiers bool
|
||||
}
|
||||
|
||||
// ContainsEnvVarModifier determines if an env var contains a modifier.
|
||||
func ContainsEnvVarModifier(envVar string) bool {
|
||||
for _, mod := range envVarModifiers {
|
||||
@ -66,7 +71,7 @@ func ContainsEnvVarModifier(envVar string) bool {
|
||||
}
|
||||
|
||||
// ReadEnv loads an app envivornment into a map.
|
||||
func ReadEnv(filePath string) (AppEnv, error) {
|
||||
func ReadEnv(filePath string, opts ReadEnvOptions) (AppEnv, error) {
|
||||
var envVars AppEnv
|
||||
|
||||
envVars, err := godotenv.Read(filePath)
|
||||
@ -76,7 +81,7 @@ func ReadEnv(filePath string) (AppEnv, error) {
|
||||
|
||||
for idx, envVar := range envVars {
|
||||
if strings.Contains(envVar, "#") {
|
||||
if ContainsEnvVarModifier(envVar) {
|
||||
if opts.IncludeModifiers && ContainsEnvVarModifier(envVar) {
|
||||
continue
|
||||
}
|
||||
vals := strings.Split(envVar, "#")
|
||||
@ -222,7 +227,7 @@ func CheckEnv(app App) ([]EnvVar, error) {
|
||||
return envVars, err
|
||||
}
|
||||
|
||||
envSample, err := ReadEnv(envSamplePath)
|
||||
envSample, err := ReadEnv(envSamplePath, ReadEnvOptions{})
|
||||
if err != nil {
|
||||
return envVars, err
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func TestGetAllFilesInDirectory(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadEnv(t *testing.T) {
|
||||
env, err := config.ReadEnv(ExpectedAppFile.Path)
|
||||
env, err := config.ReadEnv(ExpectedAppFile.Path, config.ReadEnvOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -123,7 +123,7 @@ func TestCheckEnv(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -157,7 +157,7 @@ func TestCheckEnvError(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -203,7 +203,7 @@ func TestEnvVarCommentsRemoved(t *testing.T) {
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath)
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -216,4 +216,31 @@ func TestEnvVarCommentsRemoved(t *testing.T) {
|
||||
if strings.Contains(envVar, "should be removed") {
|
||||
t.Fatalf("comment from '%s' should be removed", envVar)
|
||||
}
|
||||
|
||||
envVar, exists = envSample["SECRET_TEST_PASS_TWO_VERSION"]
|
||||
if !exists {
|
||||
t.Fatal("WITH_COMMENT env var should be present in .env.sample")
|
||||
}
|
||||
|
||||
if strings.Contains(envVar, "length") {
|
||||
t.Fatal("comment from env var SECRET_TEST_PASS_TWO_VERSION should have been removed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvVarModifiersIncluded(t *testing.T) {
|
||||
offline := true
|
||||
r, err := recipe.Get("abra-test-recipe", offline)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
|
||||
envSample, err := config.ReadEnv(envSamplePath, config.ReadEnvOptions{IncludeModifiers: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !strings.Contains(envSample["SECRET_TEST_PASS_TWO_VERSION"], "length") {
|
||||
t.Fatal("comment from env var SECRET_TEST_PASS_TWO_VERSION should not be removed")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user