fix: avoid overwriting non version env vars

See coop-cloud/organising#630
This commit is contained in:
decentral1se 2024-07-24 16:07:08 +02:00
parent 827edcb0da
commit 518c5795f4
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
8 changed files with 36 additions and 17 deletions

View File

@ -258,7 +258,7 @@ EXAMPLE:
app.Recipe.Version = chaosVersion
}
log.Debugf("choosing %s as version to save to env file", app.Recipe.Version)
if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil {
if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil {
log.Fatalf("writing new recipe version in env file: %s", err)
}

View File

@ -219,7 +219,7 @@ var appNewCommand = cli.Command{
}
log.Debugf("choosing %s as version to save to env file", version)
if err := app.WriteRecipeVersion(version); err != nil {
if err := app.WriteRecipeVersion(version, false); err != nil {
log.Fatalf("writing new recipe version in env file: %s", err)
}

View File

@ -225,7 +225,7 @@ EXAMPLE:
app.Recipe.Version = chosenDowngrade
log.Debugf("choosing %s as version to save to env file", app.Recipe.Version)
if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil {
if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil {
log.Fatalf("writing new recipe version in env file: %s", err)
}

View File

@ -286,7 +286,7 @@ EXAMPLE:
app.Recipe.Version = chosenUpgrade
log.Debugf("choosing %s as version to save to env file", app.Recipe.Version)
if err := app.WriteRecipeVersion(app.Recipe.Version); err != nil {
if err := app.WriteRecipeVersion(app.Recipe.Version, false); err != nil {
log.Fatalf("writing new recipe version in env file: %s", err)
}

View File

@ -569,7 +569,7 @@ func ReadAbraShCmdNames(abraSh string) ([]string, error) {
return cmdNames, nil
}
func (a App) WriteRecipeVersion(version string) error {
func (a App) WriteRecipeVersion(version string, dryRun bool) error {
file, err := os.Open(a.Path)
if err != nil {
return err
@ -581,7 +581,7 @@ func (a App) WriteRecipeVersion(version string) error {
lines := []string{}
for scanner.Scan() {
line := scanner.Text()
if !strings.Contains(line, "RECIPE=") && !strings.Contains(line, "TYPE") {
if !strings.HasPrefix(line, "RECIPE=") && !strings.HasPrefix(line, "TYPE=") {
lines = append(lines, line)
continue
}
@ -606,8 +606,12 @@ func (a App) WriteRecipeVersion(version string) error {
log.Fatal(err)
}
if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil {
log.Fatal(err)
if !dryRun {
if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil {
log.Fatal(err)
}
} else {
log.Debugf("skipping writing version %s because dry run", version)
}
if !skipped {

View File

@ -11,6 +11,7 @@ import (
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/recipe"
testPkg "coopcloud.tech/abra/pkg/test"
"github.com/stretchr/testify/assert"
)
func TestGetAllFoldersInDirectory(t *testing.T) {
@ -43,13 +44,7 @@ func TestReadEnv(t *testing.T) {
t.Fatal(err)
}
if !reflect.DeepEqual(env, testPkg.ExpectedAppEnv) {
t.Fatalf(
"did not get expected application settings. Expected: DOMAIN=%s RECIPE=%s; Got: DOMAIN=%s RECIPE=%s",
testPkg.ExpectedAppEnv["DOMAIN"],
testPkg.ExpectedAppEnv["RECIPE"],
env["DOMAIN"],
env["RECIPE"],
)
t.Fatal("did not get expected application settings")
}
}
@ -228,3 +223,21 @@ func TestEnvVarModifiersIncluded(t *testing.T) {
}
}
}
func TestNoOverwriteNonVersionEnvVars(t *testing.T) {
app, err := appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
if err != nil {
t.Fatal(err)
}
if err := app.WriteRecipeVersion("1.3.12", true); err != nil {
t.Fatal(err)
}
app, err = appPkg.GetApp(testPkg.ExpectedAppFiles, testPkg.AppName)
if err != nil {
t.Fatal(err)
}
assert.NotEqual(t, app.Env["SMTP_AUTHTYPE"], "login:1.3.12")
}

View File

@ -27,8 +27,9 @@ var (
)
var ExpectedAppEnv = envfile.AppEnv{
"DOMAIN": "ecloud.evil.corp",
"RECIPE": "ecloud",
"DOMAIN": "ecloud.evil.corp",
"RECIPE": "ecloud",
"SMTP_AUTHTYPE": "login",
}
var ExpectedApp = appPkg.App{

View File

@ -1,2 +1,3 @@
RECIPE=ecloud
DOMAIN=ecloud.evil.corp
SMTP_AUTHTYPE=login