From 8313203b06b8b538ff5b5c14cc8b06795e1f762d Mon Sep 17 00:00:00 2001 From: iton0 Date: Thu, 5 Dec 2024 13:43:22 -0500 Subject: [PATCH] feat: add root git-wrapper functionality - added logic for running git-related (git & gh) cloning commands with HkUp. --- cmd/root.go | 5 ++- internal/logic/root.go | 93 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 internal/logic/root.go diff --git a/cmd/root.go b/cmd/root.go index 9f05a57..5320292 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "github.com/iton0/hkup-cli/cmd/config" "github.com/iton0/hkup-cli/cmd/template" + "github.com/iton0/hkup-cli/internal/logic" "github.com/spf13/cobra" ) @@ -15,10 +16,12 @@ var ( version = "dev" rootCmd = &cobra.Command{ - Use: "hkup", + Use: "hkup [-- ]", Short: "hkup CLI", Long: `hkup is a management tool for git hooks`, + Args: cobra.MinimumNArgs(1), Version: version, + RunE: logic.Root, } ) diff --git a/internal/logic/root.go b/internal/logic/root.go new file mode 100644 index 0000000..07fd31d --- /dev/null +++ b/internal/logic/root.go @@ -0,0 +1,93 @@ +package logic + +import ( + "os" + "os/exec" + "strings" + + "github.com/iton0/hkup-cli/internal/util" + "github.com/spf13/cobra" +) + +// Root wraps git-related clone commands for easier initialization of HkUp. +// +// Returns error if issue with cloning repository or initializing HkUp. +func Root(cmd *cobra.Command, args []string) error { + root := args[0] + + // Tries to run git command in the terminal + err := util.RunCommandInTerminal(root, args[1:]...) + if err != nil { + return err + } + + secondLast := args[len(args)-2] + dir := args[len(args)-1] + var isBare bool + + // Checks if the cloned repository is bare + for _, v := range args[1:] { + if v == "--bare" { + isBare = true + } + } + + if err = cdLogic(root, secondLast, dir, isBare); err != nil { + return err + } + + // Current working directory is a bare repository and + // prompts the user to create a worktree & cd into the worktree + if isBare { + // Asks for the worktree path + worktree, err := util.UserInputPrompt("Create Worktree Path:") + if err != nil { + return err + } + + // Asks for the branch to checkout worktree into + branch, err := util.UserInputPrompt("Branch:") + if err != nil { + return err + } + + // Tries to create worktree and cd into it + err = exec.Command("git", "worktree", "add", worktree, branch).Run() + if err != nil { + return err + } + if err = os.Chdir(worktree); err != nil { + return err + } + } + + // Tries to initialize HkUp + return Init(cmd, nil) +} + +// cdLogic implements the HkUp wrapper logic around git-related clone command. +// Returns error if issue with changing directory. +func cdLogic(root, secondLast, dir string, isBare bool) error { + // Gets the remote repository name if no custom clone name is used + // ex). Not Custom: git clone + // Custom: git clone foo + switch root { + case "git", "/usr/bin/git": + if isBare && strings.HasSuffix(dir, ".git") { + start := strings.LastIndex(dir, "/") + 1 + dir = dir[start:] + } else if strings.HasSuffix(dir, ".git") { + start := strings.LastIndex(dir, "/") + 1 + end := strings.LastIndex(dir, ".git") + dir = dir[start:end] + } + case "gh", "usr/bin/gh": + if strings.Count(secondLast, "/") != 1 { + start := strings.LastIndex(dir, "/") + 1 + dir = dir[start:] + } + } + + // Either successful or returns error if issue with changing directory + return os.Chdir(dir) +}