From 47013c63d612cdc68c10eb77b774700e6e0421bd Mon Sep 17 00:00:00 2001 From: p4u1 Date: Mon, 8 Jul 2024 10:32:36 +0200 Subject: [PATCH] refactor(recipe): use template for ssh url --- cli/recipe/release.go | 24 ++++++++++++---------- pkg/recipe/recipe.go | 48 ++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/cli/recipe/release.go b/cli/recipe/release.go index ad403c33..df3c5bd4 100644 --- a/cli/recipe/release.go +++ b/cli/recipe/release.go @@ -20,6 +20,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/distribution/reference" "github.com/go-git/go-git/v5" + "github.com/sirupsen/logrus" "github.com/urfave/cli" ) @@ -62,6 +63,7 @@ your SSH keys configured on your account. BashComplete: autocomplete.RecipeNameComplete, Action: func(c *cli.Context) error { recipe := internal.ValidateRecipe(c) + r := recipePkg.Get2(recipe.Name) imagesTmp, err := getImageVersions(recipe) if err != nil { @@ -90,7 +92,7 @@ your SSH keys configured on your account. } if tagString != "" { - if err := createReleaseFromTag(recipe, tagString, mainAppVersion); err != nil { + if err := createReleaseFromTag(r, tagString, mainAppVersion); err != nil { log.Fatal(err) } } @@ -121,14 +123,14 @@ your SSH keys configured on your account. } if len(tags) > 0 { - log.Warnf("previous git tags detected, assuming this is a new semver release") - if err := createReleaseFromPreviousTag(tagString, mainAppVersion, recipe, tags); err != nil { + logrus.Warnf("previous git tags detected, assuming this is a new semver release") + if err := createReleaseFromPreviousTag(tagString, mainAppVersion, r, tags); err != nil { log.Fatal(err) } } else { log.Warnf("no tag specified and no previous tag available for %s, assuming this is the initial release", recipe.Name) - if err := createReleaseFromTag(recipe, tagString, mainAppVersion); err != nil { + if err := createReleaseFromTag(r, tagString, mainAppVersion); err != nil { if cleanUpErr := cleanUpTag(tagString, recipe.Name); err != nil { log.Fatal(cleanUpErr) } @@ -181,7 +183,7 @@ func getImageVersions(recipe recipe.Recipe) (map[string]string, error) { } // createReleaseFromTag creates a new release based on a supplied recipe version string -func createReleaseFromTag(recipe recipe.Recipe, tagString, mainAppVersion string) error { +func createReleaseFromTag(recipe recipe.Recipe2, tagString, mainAppVersion string) error { var err error directory := path.Join(config.RECIPES_DIR, recipe.Name) @@ -245,7 +247,7 @@ func getTagCreateOptions(tag string) (git.CreateTagOptions, error) { // addReleaseNotes checks if the release/next release note exists and moves the // file to release/. -func addReleaseNotes(recipe recipe.Recipe, tag string) error { +func addReleaseNotes(recipe recipe.Recipe2, tag string) error { repoPath := path.Join(config.RECIPES_DIR, recipe.Name) tagReleaseNotePath := path.Join(repoPath, "release", tag) if _, err := os.Stat(tagReleaseNotePath); err == nil { @@ -319,20 +321,20 @@ func addReleaseNotes(recipe recipe.Recipe, tag string) error { return nil } -func commitRelease(recipe recipe.Recipe, tag string) error { +func commitRelease(recipe recipe.Recipe2, tag string) error { if internal.Dry { log.Debugf("dry run: no changes committed") return nil } - isClean, err := gitPkg.IsClean(recipe.Dir()) + isClean, err := gitPkg.IsClean(recipe.Dir) if err != nil { return err } if isClean { if !internal.Dry { - return fmt.Errorf("no changes discovered in %s, nothing to publish?", recipe.Dir()) + return fmt.Errorf("no changes discovered in %s, nothing to publish?", recipe.Dir) } } @@ -372,7 +374,7 @@ func tagRelease(tagString string, repo *git.Repository) error { return nil } -func pushRelease(recipe recipe.Recipe, tagString string) error { +func pushRelease(recipe recipe.Recipe2, tagString string) error { if internal.Dry { log.Info("dry run: no changes published") return nil @@ -401,7 +403,7 @@ func pushRelease(recipe recipe.Recipe, tagString string) error { return nil } -func createReleaseFromPreviousTag(tagString, mainAppVersion string, recipe recipe.Recipe, tags []string) error { +func createReleaseFromPreviousTag(tagString, mainAppVersion string, recipe recipe.Recipe2, tags []string) error { directory := path.Join(config.RECIPES_DIR, recipe.Name) repo, err := git.PlainOpen(directory) if err != nil { diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index d172a1f5..21b1df00 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -138,25 +138,6 @@ type Recipe struct { Meta RecipeMeta } -// Push pushes the latest changes to a SSH URL remote. You need to have your -// local SSH configuration for git.coopcloud.tech working for this to work -func (r Recipe) Push(dryRun bool) error { - repo, err := git.PlainOpen(r.Dir()) - if err != nil { - return err - } - - if err := gitPkg.CreateRemote(repo, "origin-ssh", r.Meta.SSHURL, dryRun); err != nil { - return err - } - - if err := gitPkg.Push(r.Dir(), "origin-ssh", true, dryRun); err != nil { - return err - } - - return nil -} - // Dir retrieves the recipe repository path func (r Recipe) Dir() string { return path.Join(config.RECIPES_DIR, r.Name) @@ -268,14 +249,16 @@ func (r Recipe) SampleEnv() (map[string]string, error) { func Get2(name string) Recipe2 { return Recipe2{ - Name: name, - Dir: path.Join(config.RECIPES_DIR, name), + Name: name, + Dir: path.Join(config.RECIPES_DIR, name), + SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name), } } type Recipe2 struct { - Name string - Dir string + Name string + Dir string + SSHURL string } // Ensure makes sure the recipe exists, is up to date and has the latest version checked out. @@ -508,6 +491,25 @@ func (r Recipe2) ChaosVersion() (string, error) { return version, nil } +// Push pushes the latest changes to a SSH URL remote. You need to have your +// local SSH configuration for git.coopcloud.tech working for this to work +func (r Recipe2) Push(dryRun bool) error { + repo, err := git.PlainOpen(r.Dir) + if err != nil { + return err + } + + if err := gitPkg.CreateRemote(repo, "origin-ssh", r.SSHURL, dryRun); err != nil { + return err + } + + if err := gitPkg.Push(r.Dir, "origin-ssh", true, dryRun); err != nil { + return err + } + + return nil +} + // GetRecipesLocal retrieves all local recipe directories func GetRecipesLocal() ([]string, error) { var recipes []string