From b2485cc122f84f11caffc15714e5d0fd1bb65aae Mon Sep 17 00:00:00 2001
From: p4u1 <p4u1_f4u1@riseup.net>
Date: Thu, 7 Mar 2024 17:46:59 +0100
Subject: [PATCH] feat: add git-user and git-email flags to recipe new

---
 cli/internal/cli.go               | 16 ++++++++++++++++
 cli/recipe/new.go                 |  7 ++++---
 pkg/git/init.go                   | 24 +++++++++++++++---------
 tests/integration/recipe_new.bats |  4 ++--
 4 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/cli/internal/cli.go b/cli/internal/cli.go
index 0ed2af57c3..e66ab00770 100644
--- a/cli/internal/cli.go
+++ b/cli/internal/cli.go
@@ -248,6 +248,22 @@ var RemoteUserFlag = &cli.StringFlag{
 	Destination: &RemoteUser,
 }
 
+var GitName string
+var GitNameFlag = &cli.StringFlag{
+	Name:        "git-name, gn",
+	Value:       "",
+	Usage:       "Git (user) name to do commits with",
+	Destination: &GitName,
+}
+
+var GitEmail string
+var GitEmailFlag = &cli.StringFlag{
+	Name:        "git-email, ge",
+	Value:       "",
+	Usage:       "Git email name to do commits with",
+	Destination: &GitEmail,
+}
+
 // SubCommandBefore wires up pre-action machinery (e.g. --debug handling).
 func SubCommandBefore(c *cli.Context) error {
 	if Debug {
diff --git a/cli/recipe/new.go b/cli/recipe/new.go
index 87e84d87de..efbcc3b1d6 100644
--- a/cli/recipe/new.go
+++ b/cli/recipe/new.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path"
 	"text/template"
@@ -37,6 +36,8 @@ var recipeNewCommand = cli.Command{
 		internal.DebugFlag,
 		internal.NoInputFlag,
 		internal.OfflineFlag,
+		internal.GitNameFlag,
+		internal.GitEmailFlag,
 	},
 	Before:    internal.SubCommandBefore,
 	Usage:     "Create a new recipe",
@@ -92,14 +93,14 @@ recipe and domain in the sample environment config).
 				logrus.Fatal(err)
 			}
 
-			if err := ioutil.WriteFile(path, templated.Bytes(), 0644); err != nil {
+			if err := os.WriteFile(path, templated.Bytes(), 0644); err != nil {
 				logrus.Fatal(err)
 			}
 
 		}
 
 		newGitRepo := path.Join(config.RECIPES_DIR, recipeName)
-		if err := git.Init(newGitRepo, true); err != nil {
+		if err := git.Init(newGitRepo, true, internal.GitName, internal.GitEmail); err != nil {
 			logrus.Fatal(err)
 		}
 
diff --git a/pkg/git/init.go b/pkg/git/init.go
index d0af92b035..00d3c2e35a 100644
--- a/pkg/git/init.go
+++ b/pkg/git/init.go
@@ -1,35 +1,41 @@
 package git
 
 import (
+	"fmt"
+
 	"github.com/go-git/go-git/v5"
-	gitPkg "github.com/go-git/go-git/v5"
+	"github.com/go-git/go-git/v5/plumbing/object"
 	"github.com/sirupsen/logrus"
 )
 
 // Init inits a new repo and commits all the stuff if you want
-func Init(repoPath string, commit bool) error {
-	if _, err := gitPkg.PlainInit(repoPath, false); err != nil {
-		logrus.Fatal(err)
+func Init(repoPath string, commit bool, gitName, gitEmail string) error {
+	if _, err := git.PlainInit(repoPath, false); err != nil {
+		return fmt.Errorf("git init: %s", err)
 	}
 	logrus.Debugf("initialised new git repo in %s", repoPath)
 
 	if commit {
 		commitRepo, err := git.PlainOpen(repoPath)
 		if err != nil {
-			logrus.Fatal(err)
+			return fmt.Errorf("git open: %s", err)
 		}
 
 		commitWorktree, err := commitRepo.Worktree()
 		if err != nil {
-			logrus.Fatal(err)
+			return fmt.Errorf("git worktree: %s", err)
 		}
 
 		if err := commitWorktree.AddWithOptions(&git.AddOptions{All: true}); err != nil {
-			return err
+			return fmt.Errorf("git add: %s", err)
 		}
 
-		if _, err = commitWorktree.Commit("init", &git.CommitOptions{}); err != nil {
-			return err
+		var author *object.Signature
+		if gitName != "" && gitEmail != "" {
+			author = &object.Signature{Name: gitName, Email: gitEmail}
+		}
+		if _, err = commitWorktree.Commit("init", &git.CommitOptions{Author: author}); err != nil {
+			return fmt.Errorf("git commit: %s", err)
 		}
 		logrus.Debugf("init committed all files for new git repo in %s", repoPath)
 	}
diff --git a/tests/integration/recipe_new.bats b/tests/integration/recipe_new.bats
index 6149f053bd..b1f8e15053 100644
--- a/tests/integration/recipe_new.bats
+++ b/tests/integration/recipe_new.bats
@@ -23,14 +23,14 @@ teardown(){
 }
 
 @test "create new recipe" {
-  run $ABRA recipe new foobar
+  run $ABRA recipe new foobar --git-name foo --git-email foo@example.com
   assert_success
   assert_output --partial 'Your new foobar recipe has been created'
   assert_exists "$ABRA_DIR/recipes/foobar"
 }
 
 @test "create new app from new recipe" {
-  run $ABRA recipe new foobar
+  run $ABRA recipe new foobar --git-name foo --git-email foo@example.com
   assert_success
 
   run $ABRA app new foobar \