diff --git a/cli/catalogue/catalogue.go b/cli/catalogue/catalogue.go index 9e6dfcbe..50cb8d2d 100644 --- a/cli/catalogue/catalogue.go +++ b/cli/catalogue/catalogue.go @@ -15,7 +15,6 @@ import ( gitPkg "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/limit" "github.com/AlecAivazis/survey/v2" - "github.com/go-git/go-git/v5" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -224,43 +223,19 @@ A new catalogue copy can be published to the recipes repository by passing the logrus.Infof("generated new recipe catalogue in %s", config.APPS_JSON) if internal.Commit { - repoPath := path.Join(config.ABRA_DIR, "catalogue") - commitRepo, err := git.PlainOpen(repoPath) - if err != nil { - logrus.Fatal(err) - } - - commitWorktree, err := commitRepo.Worktree() - if err != nil { - logrus.Fatal(err) - } - - if internal.CommitMessage == "" { + if internal.CommitMessage == "" && !internal.NoInput { prompt := &survey.Input{ Message: "commit message", - Default: "chore: publish new catalogue changes", + Default: fmt.Sprintf("chore: publish catalogue changes"), } if err := survey.AskOne(prompt, &internal.CommitMessage); err != nil { logrus.Fatal(err) } } - err = commitWorktree.AddGlob("**.json") - if err != nil { + if err := gitPkg.Commit("**.json", internal.CommitMessage, false); err != nil { logrus.Fatal(err) } - logrus.Debug("staged **.json for commit") - - _, err = commitWorktree.Commit(internal.CommitMessage, &git.CommitOptions{}) - if err != nil { - logrus.Fatal(err) - } - logrus.Info("changes commited") - - if err := commitRepo.Push(&git.PushOptions{}); err != nil { - logrus.Fatal(err) - } - logrus.Info("changes pushed") } return nil diff --git a/cli/recipe/release.go b/cli/recipe/release.go index 0a9b2be2..517f9c58 100644 --- a/cli/recipe/release.go +++ b/cli/recipe/release.go @@ -9,6 +9,7 @@ import ( abraFormatter "coopcloud.tech/abra/cli/formatter" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/pkg/config" + gitPkg "coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/recipe" recipePkg "coopcloud.tech/abra/pkg/recipe" "coopcloud.tech/tagcmp" @@ -140,15 +141,6 @@ You may invoke this command in "wizard" mode and be prompted for input: } if internal.Commit || internal.CommitMessage != "" { - commitRepo, err := git.PlainOpen(directory) - if err != nil { - logrus.Fatal(err) - } - commitWorktree, err := commitRepo.Worktree() - if err != nil { - logrus.Fatal(err) - } - if internal.CommitMessage == "" && !internal.NoInput { prompt := &survey.Input{ Message: "commit message", @@ -159,21 +151,14 @@ You may invoke this command in "wizard" mode and be prompted for input: } } - err = commitWorktree.AddGlob("compose.**yml") - if err != nil { + if internal.CommitMessage == "" { + logrus.Fatal("no commit message specified?") + } + + if err := gitPkg.Commit("compose.**yml", internal.CommitMessage, internal.Dry); err != nil { logrus.Fatal(err) } - logrus.Debug("staged compose.**yml for commit") - if !internal.Dry { - _, err = commitWorktree.Commit(internal.CommitMessage, &git.CommitOptions{}) - if err != nil { - logrus.Fatal(err) - } - logrus.Info("changes commited") - } else { - logrus.Info("dry run only: NOT committing changes") - } } repo, err := git.PlainOpen(directory) diff --git a/pkg/git/commit.go b/pkg/git/commit.go new file mode 100644 index 00000000..68e114db --- /dev/null +++ b/pkg/git/commit.go @@ -0,0 +1,68 @@ +package git + +import ( + "fmt" + "path" + + "coopcloud.tech/abra/pkg/config" + "github.com/go-git/go-git/v5" + "github.com/sirupsen/logrus" +) + +// Commit runs a git commit +func Commit(glob, commitMessage string, dryRun bool) error { + if commitMessage == "" { + return fmt.Errorf("no commit message specified?") + } + + repoPath := path.Join(config.ABRA_DIR, "catalogue") + commitRepo, err := git.PlainOpen(repoPath) + if err != nil { + return err + } + + commitWorktree, err := commitRepo.Worktree() + if err != nil { + return err + } + + patterns, err := GetExcludesFiles() + if err != nil { + return err + } + + if len(patterns) > 0 { + commitWorktree.Excludes = append(patterns, commitWorktree.Excludes...) + } + + if !dryRun { + err = commitWorktree.AddGlob(glob) + if err != nil { + return err + } + logrus.Debugf("staged %s for commit", glob) + } else { + logrus.Debugf("dry run: did not stage %s for commit", glob) + } + + if !dryRun { + _, err = commitWorktree.Commit(commitMessage, &git.CommitOptions{}) + if err != nil { + return err + } + logrus.Info("changes commited") + } else { + logrus.Info("dry run: no changes commited") + } + + if !dryRun { + if err := commitRepo.Push(&git.PushOptions{}); err != nil { + return err + } + logrus.Info("changes pushed") + } else { + logrus.Info("dry run: no changes pushed") + } + + return nil +}