Skip to content

Commit

Permalink
chore(cmd): code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Raj Nandan Sharma authored and Raj Nandan Sharma committed Apr 15, 2024
1 parent 545a77b commit 10c7da8
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 195 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
17 changes: 6 additions & 11 deletions cmd/add.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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")

},
}

Expand Down
30 changes: 21 additions & 9 deletions cmd/branch.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

},
}
Expand Down
16 changes: 5 additions & 11 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

},
}
Expand Down
45 changes: 15 additions & 30 deletions cmd/commit.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cmd

import (
"fmt"
"os"
"os/exec"
"errors"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

},
}
Expand Down
40 changes: 15 additions & 25 deletions cmd/pull.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

},
}

Expand Down
17 changes: 6 additions & 11 deletions cmd/push.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

},
}

Expand Down
17 changes: 6 additions & 11 deletions cmd/reset.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)

},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 10c7da8

Please sign in to comment.