From 7596982282e39629e97314d3373c4111d13d5133 Mon Sep 17 00:00:00 2001 From: p4u1 Date: Mon, 8 Jul 2024 17:55:40 +0200 Subject: [PATCH] feat: update new version in env file --- cli/app/deploy.go | 7 +++++++ cli/app/rollback.go | 7 +++++++ cli/app/upgrade.go | 7 +++++++ pkg/app/app.go | 27 +++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/cli/app/deploy.go b/cli/app/deploy.go index 9a9524f1..0f2c0ed5 100644 --- a/cli/app/deploy.go +++ b/cli/app/deploy.go @@ -240,6 +240,13 @@ EXAMPLE: log.Fatalf("attempting to run post deploy commands, saw: %s", err) } } + + if app.Recipe.Version != "" && specificVersion != "" && specificVersion != app.Recipe.Version { + err := app.WriteRecipeVersion(specificVersion) + if err != nil { + log.Fatalf("writing new recipe version in env file: %s", err) + } + } return nil }, } diff --git a/cli/app/rollback.go b/cli/app/rollback.go index 8ec65d06..79a35359 100644 --- a/cli/app/rollback.go +++ b/cli/app/rollback.go @@ -211,6 +211,13 @@ EXAMPLE: log.Fatal(err) } + if app.Recipe.Version != "" { + err := app.WriteRecipeVersion(chosenDowngrade) + if err != nil { + log.Fatalf("writing new recipe version in env file: %s", err) + } + } + return nil }, } diff --git a/cli/app/upgrade.go b/cli/app/upgrade.go index 69198151..26366aef 100644 --- a/cli/app/upgrade.go +++ b/cli/app/upgrade.go @@ -267,6 +267,13 @@ EXAMPLE: } } + if app.Recipe.Version != "" { + err := app.WriteRecipeVersion(chosenUpgrade) + if err != nil { + log.Fatalf("writing new recipe version in env file: %s", err) + } + } + return nil }, } diff --git a/pkg/app/app.go b/pkg/app/app.go index 9b09dcd9..95b6d191 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -568,3 +568,30 @@ func ReadAbraShCmdNames(abraSh string) ([]string, error) { return cmdNames, nil } + +func (a App) WriteRecipeVersion(version string) error { + file, err := os.Open(a.Path) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + lines := []string{} + for scanner.Scan() { + line := scanner.Text() + if !strings.Contains(line, "RECIPE=") && !strings.Contains(line, "TYPE") { + lines = append(lines, line) + continue + } + splitted := strings.Split(line, ":") + line = fmt.Sprintf("%s:%s", splitted[0], version) + lines = append(lines, line) + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } + + return os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm) +}