From 10c7da823cd0fb5eccd710067c84cfdee9970b91 Mon Sep 17 00:00:00 2001 From: Raj Nandan Sharma Date: Mon, 15 Apr 2024 21:33:59 +0530 Subject: [PATCH] chore(cmd): code refactor --- README.md | 19 ++++++++++ cmd/add.go | 17 ++++----- cmd/branch.go | 30 +++++++++++----- cmd/checkout.go | 16 +++------ cmd/commit.go | 45 ++++++++---------------- cmd/pull.go | 40 ++++++++------------- cmd/push.go | 17 ++++----- cmd/reset.go | 17 ++++----- cmd/root.go | 2 +- cmd/start.go | 35 ++++++++----------- cmd/status.go | 16 ++++----- cmd/sync.go | 91 ++++++++++++++++++------------------------------ models/common.go | 8 +++++ utils/fs.go | 33 ++++++++++++++++++ 14 files changed, 191 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index 29cc57b..58a4a35 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,25 @@ go install -v github.com/rajnandan1/okgit@latest ## Documentation + +| Command | Description | +|-------------|-------------------------------------------------------------------------| +| ad | Stage files for commit. Similar to `git add` | +| bn | Get current branch name. Similar to `git branch` | +| ch | Switch branches or restore working tree files. Similar to `git checkout` | +| cm | Create a conventional commit. Similar to `git commit` | +| completion | Generate the autocompletion script for the specified shell | +| done | Do add commit and push at one go | +| help | Help about any command | +| pl | Pull remote branch changes. Similar to `git pull` | +| ps | Push local branch changes to remote. Similar to `git push` | +| rs | Reset changes in the working directory. Similar to `git reset` | +| sn | Sync local branch with remote from -> to | +| st | Check the status of the repository. Similar to `git status` | +| start | Start working on new or existing branch | + + + ```bash okgit --help ``` diff --git a/cmd/add.go b/cmd/add.go index 7698a42..f96f82a 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -1,11 +1,8 @@ package cmd import ( - "os" - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -20,14 +17,12 @@ var addCmd = &cobra.Command{ } gitAdd := models.AllCommands["gitAdd"] gitAdd.Arguments = append(gitAdd.Arguments, args...) - xmd := exec.Command(gitAdd.Name, gitAdd.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Staged files successfully") - } else { - color.Red("⨯ Error staging files") + _, cmdErr := utils.RunCommand(gitAdd.Name, gitAdd.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput("Staged files successfully") + }, } diff --git a/cmd/branch.go b/cmd/branch.go index d33a800..7458a7f 100644 --- a/cmd/branch.go +++ b/cmd/branch.go @@ -1,10 +1,8 @@ package cmd import ( - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -15,13 +13,27 @@ var branchCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { gitBranch := models.AllCommands["gitBranch"] - branch, err := exec.Command(gitBranch.Name, gitBranch.Arguments...).Output() - if err != nil { - color.Red("Is it a git repo? Error getting current branch") - return + + cmdOut, cmdErr := utils.RunCommand(gitBranch.Name, gitBranch.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) + } + + output := cmdOut + + lastCommitData := models.AllCommands["lastCommitData"] + cmdOut, cmdErr = utils.RunCommand(lastCommitData.Name, lastCommitData.Arguments, "") + if cmdErr == nil { + output += "\n" + cmdOut + } + + lastCommitAuthor := models.AllCommands["lastCommitAuthor"] + cmdOut, cmdErr = utils.RunCommand(lastCommitAuthor.Name, lastCommitAuthor.Arguments, "") + if cmdErr == nil { + output += "\n" + cmdOut } - branch = branch[:len(branch)-1] - color.Green("Current branch: %s", branch) + + utils.LogOutput(output) }, } diff --git a/cmd/checkout.go b/cmd/checkout.go index 0bbf012..c162ab5 100644 --- a/cmd/checkout.go +++ b/cmd/checkout.go @@ -1,11 +1,8 @@ package cmd import ( - "os" - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -22,14 +19,11 @@ var checkoutCmd = &cobra.Command{ gitCheckout := models.AllCommands["gitCheckout"] gitCheckout.Arguments = append(gitCheckout.Arguments, args...) - xmd := exec.Command(gitCheckout.Name, gitCheckout.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Switched branches or restored files successfully") - } else { - color.Red("⨯ Error switching branches or restoring files") + cmdOut, cmdErr := utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) }, } diff --git a/cmd/commit.go b/cmd/commit.go index 3061e44..488487d 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -1,9 +1,7 @@ package cmd import ( - "fmt" - "os" - "os/exec" + "errors" "strings" "github.com/fatih/color" @@ -44,17 +42,15 @@ var commitCmd = &cobra.Command{ gitBranch := models.AllCommands["gitBranch"] - branch, err := exec.Command(gitBranch.Name, gitBranch.Arguments...).Output() - if err != nil { - color.Red("Is it a git repo? Error getting current branch") - return + branch, cmdErr := utils.RunCommand(gitBranch.Name, gitBranch.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } - branch = branch[:len(branch)-1] - if storedCommit, err := utils.GetLastCommitForBranchFromFile(string(branch)); err == nil { + if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch); err == nil { myCommit = *storedCommit } if myCommit.Type == "" { - myCommit.Type = getCommitTypeFromBranchName(string(branch)) + myCommit.Type = getCommitTypeFromBranchName(branch) } // Ask for the commit type @@ -65,16 +61,14 @@ var commitCmd = &cobra.Command{ commitTypeInput := utils.ReadInput(false) if commitTypeInput == "" && myCommit.Type == "" { - color.Red("Commit type is required.") - return + utils.LogFatal(errors.New("Commit type is required.")) } if commitTypeInput != "" { myCommit.Type = commitTypeInput } if !contains(commitTypes, myCommit.Type) { - color.Red("Invalid commit type. Please provide a valid commit type.") - return + utils.LogFatal(errors.New("Invalid commit type. Please provide a valid commit type.")) } // Ask for the commit scope @@ -102,8 +96,7 @@ var commitCmd = &cobra.Command{ } if myCommit.Summary == "" { - color.Red("Commit summary is required.") - return + utils.LogFatal(errors.New("Commit summary is required.")) } // Ask for the commit message @@ -162,26 +155,18 @@ var commitCmd = &cobra.Command{ commit := generateCommit(myCommit) - fmt.Println("Generated commit message:") - fmt.Println(commit) utils.CreateDirectoryAndFileIfNotExist() - err = utils.AddCommitToBranchFile(string(branch), myCommit) + err := utils.AddCommitToBranchFile(string(branch), myCommit) if err != nil { - color.Red("Error adding commit to branch file:", err) - return + utils.LogFatal(errors.New("Error adding commit to branch file:")) } gitCommit := models.AllCommands["gitCommit"] - xmd := exec.Command(gitCommit.Name, gitCommit.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - xmd.Stdin = strings.NewReader(commit) - xmderr := xmd.Run() - if xmderr == nil { - color.Green("✔ Committed changes successfully") - } else { - color.Red("⨯ Error committing changes") + cmdOutCommit, cmdErr := utils.RunCommand(gitCommit.Name, gitCommit.Arguments, commit) + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOutCommit) }, } diff --git a/cmd/pull.go b/cmd/pull.go index b23e59a..3066a96 100644 --- a/cmd/pull.go +++ b/cmd/pull.go @@ -1,11 +1,10 @@ package cmd import ( - "os" - "os/exec" + "errors" - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -17,46 +16,37 @@ var pullCmd = &cobra.Command{ //get current branch gitBracnh := models.AllCommands["gitBranch"] - branch, err := exec.Command(gitBracnh.Name, gitBracnh.Arguments...).Output() + branch, err := utils.RunCommand(gitBracnh.Name, gitBracnh.Arguments, "") if err != nil { - branch = []byte("") - } else { - branch = branch[:len(branch)-1] + branch = "" } //expect the args[0] to be a branch name if len(args) > 0 { - branch = []byte(args[0]) + branch = args[0] } if len(branch) == 0 { - color.Red("Error getting branch name") - return + utils.LogFatal(errors.New("Please provide the branch name to pull changes")) } //checkout the branch gitCheckout := models.AllCommands["gitCheckout"] gitCheckout.Arguments = append(gitCheckout.Arguments, string(branch)) - xmd := exec.Command(gitCheckout.Name, gitCheckout.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Checked out branch successfully") - } else { - color.Red("⨯ Error checking out branch") - return + cmdOut, cmdErr := utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) gitPull := models.AllCommands["gitPull"] gitPull.Arguments = append(gitPull.Arguments, string(branch)) - xmd = exec.Command(gitPull.Name, gitPull.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Pulled changes successfully") - } else { - color.Red("⨯ Error pulling changes") + cmdOut, cmdErr = utils.RunCommand(gitPull.Name, gitPull.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) + }, } diff --git a/cmd/push.go b/cmd/push.go index ef674cd..de54596 100644 --- a/cmd/push.go +++ b/cmd/push.go @@ -1,11 +1,8 @@ package cmd import ( - "os" - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -16,14 +13,12 @@ var pushCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { gitPush := models.AllCommands["gitPush"] - xmd := exec.Command(gitPush.Name, gitPush.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Pushed changes successfully") - } else { - color.Red("⨯ Error pushing changes") + cmdOut, cmdErr := utils.RunCommand(gitPush.Name, gitPush.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) + }, } diff --git a/cmd/reset.go b/cmd/reset.go index c20a781..daa051e 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -1,11 +1,8 @@ package cmd import ( - "os" - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -16,14 +13,12 @@ var resetCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { gitReset := models.AllCommands["gitReset"] - xmd := exec.Command(gitReset.Name, gitReset.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Reset changes successfully") - } else { - color.Red("⨯ Error resetting changes") + cmdOut, cmdErr := utils.RunCommand(gitReset.Name, gitReset.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) + }, } diff --git a/cmd/root.go b/cmd/root.go index 219a959..1995918 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{ // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, - Version: "1.0.7", + Version: "1.0.8", } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/cmd/start.go b/cmd/start.go index ad902fc..f355796 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -1,12 +1,11 @@ package cmd import ( - "os" - "os/exec" + "errors" "strings" - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -17,40 +16,36 @@ var startCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - color.Red("Please provide a branch name") - return + utils.LogFatal(errors.New("Please provide the branch name to start working on")) } + branch := strings.TrimSpace(args[0]) gitFetchBranch := models.AllCommands["gitFetchBranch"] gitFetchBranch.Arguments = append(gitFetchBranch.Arguments, branch) - res, err := exec.Command(gitFetchBranch.Name, gitFetchBranch.Arguments...).Output() + res, err := utils.RunCommand(gitFetchBranch.Name, gitFetchBranch.Arguments, "") if err != nil { - color.Red("Error fetching branches") - return + utils.LogFatal(err) } if len(res) == 0 { - // checkoutCmd.Run(cmd, args) createBranch := models.AllCommands["createBranch"] createBranch.Arguments = append(createBranch.Arguments, branch) - xmd := exec.Command(createBranch.Name, createBranch.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() == nil { - color.Green("✔ Created new branch %s", branch) - } else { - color.Red("⨯ Error creating new branch %s", branch) + cmdOut, cmdErr := utils.RunCommand(createBranch.Name, createBranch.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) } checkoutCmd.Run(cmd, args) gitPull := models.AllCommands["gitPull"] gitPull.Arguments = append(gitPull.Arguments, branch) - _, err1 := exec.Command(gitPull.Name, gitPull.Arguments...).Output() - if err1 == nil { - color.Green("✔ Pulled changes successfully") - } + msg, err1 := utils.RunCommand(gitPull.Name, gitPull.Arguments, "") + if err1 != nil { + utils.LogFatal(err1) + } + utils.LogOutput(msg) }, } diff --git a/cmd/status.go b/cmd/status.go index c3feb00..4a03f89 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -1,11 +1,8 @@ package cmd import ( - "os" - "os/exec" - - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -16,11 +13,12 @@ var statusCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { gitStatus := models.AllCommands["gitStatus"] - xmd := exec.Command(gitStatus.Name, gitStatus.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - xmd.Run() - color.Yellow("Status command executed successfully") + cmdOut, cmdErr := utils.RunCommand(gitStatus.Name, gitStatus.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) + } + utils.LogOutput(cmdOut) + }, } diff --git a/cmd/sync.go b/cmd/sync.go index 43bb5d3..37eac99 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -1,11 +1,10 @@ package cmd import ( - "os" - "os/exec" + "errors" - "github.com/fatih/color" "github.com/rajnandan1/okgit/models" + "github.com/rajnandan1/okgit/utils" "github.com/spf13/cobra" ) @@ -15,18 +14,16 @@ var syncCmd = &cobra.Command{ Long: "Sync local branch with remote from -> to. if to is not given it will sync the current branch. Example usage: okgit sync fromBranchName toBranchName", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - color.Red("Please provide the branch name to sync with") - return + utils.LogFatal(errors.New("Please provide the branch name to sync with")) } fromBranch := args[0] gitBranch := models.AllCommands["gitBranch"] - data, err := exec.Command(gitBranch.Name, gitBranch.Arguments...).Output() - if err != nil { - color.Red("Is it a git repo?") - return + + toBranch, cmdErr := utils.RunCommand(gitBranch.Name, gitBranch.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } - toBranch := string(data[:len(data)-1]) if len(args) >= 2 { args = append(args, args[0]) toBranch = args[1] @@ -34,76 +31,56 @@ var syncCmd = &cobra.Command{ //check if unstaged or uncommit files are there in toBranch gitStatus := models.AllCommands["gitStatus"] - data, err = exec.Command(gitStatus.Name, gitStatus.Arguments...).Output() - if err != nil { - color.Red("Is it a git repo?") - return + cmdOut, cmdErr := utils.RunCommand(gitStatus.Name, gitStatus.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } - if len(data) > 0 { - color.Red("Please commit or stash the changes in the branch %s", toBranch) - return + if len(cmdOut) > 0 { + utils.LogFatal(errors.New("Please commit or stash the changes in the branch " + toBranch)) } - + utils.LogOutput(cmdOut) //now checkout fromBranch - color.Yellow("Checking out the branch %s", fromBranch) gitCheckout := models.AllCommands["gitCheckout"] gitCheckout.Arguments = append(gitCheckout.Arguments, fromBranch) - xmd := exec.Command(gitCheckout.Name, gitCheckout.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() != nil { - color.Red("Error in checking out the branch %s", fromBranch) - return + cmdOut, cmdErr = utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } - + utils.LogOutput(cmdOut) //now pull changes from remote - color.Yellow("Pulling the branch %s", fromBranch) gitPull := models.AllCommands["gitPull"] gitPull.Arguments = append(gitPull.Arguments, fromBranch) - xmd = exec.Command(gitPull.Name, gitPull.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() != nil { - color.Red("Error in pulling the branch %s", fromBranch) - return + cmdOut, cmdErr = utils.RunCommand(gitPull.Name, gitPull.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } - + utils.LogOutput(cmdOut) //now checkout toBranch - color.Yellow("Checking out the branch %s", toBranch) gitCheckout.Arguments = []string{"checkout"} gitCheckout.Arguments = append(gitCheckout.Arguments, toBranch) - xmd = exec.Command(gitCheckout.Name, gitCheckout.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() != nil { - color.Red("Error in checking out the branch %s", toBranch) - return + cmdOut, cmdErr = utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) //now pull changes from remote - color.Yellow("Pulling the branch %s", toBranch) gitPull.Arguments = []string{"pull", "origin", toBranch} - xmd = exec.Command(gitPull.Name, gitPull.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() != nil { - color.Red("Error in pulling the branch %s", toBranch) - return + cmdOut, cmdErr = utils.RunCommand(gitPull.Name, gitPull.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) //now merge toBranch with fromBranch - color.Yellow("Merging the branch %s with %s", fromBranch, toBranch) gitMerge := models.AllCommands["gitMerge"] gitMerge.Arguments = append(gitMerge.Arguments, fromBranch) - xmd = exec.Command(gitMerge.Name, gitMerge.Arguments...) - xmd.Stdout = os.Stdout - xmd.Stderr = os.Stderr - if xmd.Run() != nil { - color.Red("Error in merging the branch %s with %s", fromBranch, toBranch) - return - } else { - color.Green("✔ Synced the branch %s with %s successfully", fromBranch, toBranch) + cmdOut, cmdErr = utils.RunCommand(gitMerge.Name, gitMerge.Arguments, "") + if cmdErr != nil { + utils.LogFatal(cmdErr) } + utils.LogOutput(cmdOut) + utils.LogOutput("Synced branch " + fromBranch + " with " + toBranch) }, } diff --git a/models/common.go b/models/common.go index 4ed1a18..5709388 100644 --- a/models/common.go +++ b/models/common.go @@ -63,4 +63,12 @@ var AllCommands = map[string]ShellCommands{ Name: "git", Arguments: []string{"branch"}, }, + "lastCommitData": { + Name: "git", + Arguments: []string{"log", "-1", "--pretty=format:%h %s"}, + }, + "lastCommitAuthor": { + Name: "git", + Arguments: []string{"log", "-1", "--pretty=format:%an %ad"}, + }, } diff --git a/utils/fs.go b/utils/fs.go index 5f950b2..af4ae4c 100644 --- a/utils/fs.go +++ b/utils/fs.go @@ -2,13 +2,17 @@ package utils import ( "bufio" + "bytes" "encoding/json" "fmt" "io/ioutil" + "log" "os" + "os/exec" "path/filepath" "strings" + "github.com/fatih/color" "github.com/rajnandan1/okgit/models" ) @@ -161,3 +165,32 @@ func ReadInput(multi bool) string { input = strings.TrimSpace(input) return input } +func RunCommand(name string, args []string, stdin string) (string, error) { + color.HiBlue("✨" + name + " " + strings.Join(args, " ")) + cmd := exec.Command(name, args...) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if stdin != "" { + cmd.Stdin = strings.NewReader(stdin) + } + err := cmd.Run() + if err != nil { + return strings.TrimRight(stderr.String(), "\n"), err + } + return strings.TrimRight(stdout.String(), "\n"), nil +} + +func LogFatal(err error) { + if err != nil { + red := color.New(color.FgRed).SprintFunc() + log.Fatal(red(err.Error())) + } +} + +func LogOutput(output string) { + if output != "" { + green := color.New(color.FgGreen).SprintFunc() + fmt.Println(green(output)) + } +}