Skip to content

Commit 10c7da8

Browse files
Raj Nandan SharmaRaj Nandan Sharma
Raj Nandan Sharma
authored and
Raj Nandan Sharma
committed
chore(cmd): code refactor
1 parent 545a77b commit 10c7da8

File tree

14 files changed

+191
-195
lines changed

14 files changed

+191
-195
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ go install -v github.com/rajnandan1/okgit@latest
1717

1818
## Documentation
1919

20+
21+
| Command | Description |
22+
|-------------|-------------------------------------------------------------------------|
23+
| ad | Stage files for commit. Similar to `git add` |
24+
| bn | Get current branch name. Similar to `git branch` |
25+
| ch | Switch branches or restore working tree files. Similar to `git checkout` |
26+
| cm | Create a conventional commit. Similar to `git commit` |
27+
| completion | Generate the autocompletion script for the specified shell |
28+
| done | Do add commit and push at one go |
29+
| help | Help about any command |
30+
| pl | Pull remote branch changes. Similar to `git pull` |
31+
| ps | Push local branch changes to remote. Similar to `git push` |
32+
| rs | Reset changes in the working directory. Similar to `git reset` |
33+
| sn | Sync local branch with remote from -> to |
34+
| st | Check the status of the repository. Similar to `git status` |
35+
| start | Start working on new or existing branch |
36+
37+
38+
2039
```bash
2140
okgit --help
2241
```

cmd/add.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package cmd
22

33
import (
4-
"os"
5-
"os/exec"
6-
7-
"github.com/fatih/color"
84
"github.com/rajnandan1/okgit/models"
5+
"github.com/rajnandan1/okgit/utils"
96
"github.com/spf13/cobra"
107
)
118

@@ -20,14 +17,12 @@ var addCmd = &cobra.Command{
2017
}
2118
gitAdd := models.AllCommands["gitAdd"]
2219
gitAdd.Arguments = append(gitAdd.Arguments, args...)
23-
xmd := exec.Command(gitAdd.Name, gitAdd.Arguments...)
24-
xmd.Stdout = os.Stdout
25-
xmd.Stderr = os.Stderr
26-
if xmd.Run() == nil {
27-
color.Green("✔ Staged files successfully")
28-
} else {
29-
color.Red("⨯ Error staging files")
20+
_, cmdErr := utils.RunCommand(gitAdd.Name, gitAdd.Arguments, "")
21+
if cmdErr != nil {
22+
utils.LogFatal(cmdErr)
3023
}
24+
utils.LogOutput("Staged files successfully")
25+
3126
},
3227
}
3328

cmd/branch.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cmd
22

33
import (
4-
"os/exec"
5-
6-
"github.com/fatih/color"
74
"github.com/rajnandan1/okgit/models"
5+
"github.com/rajnandan1/okgit/utils"
86
"github.com/spf13/cobra"
97
)
108

@@ -15,13 +13,27 @@ var branchCmd = &cobra.Command{
1513
Run: func(cmd *cobra.Command, args []string) {
1614

1715
gitBranch := models.AllCommands["gitBranch"]
18-
branch, err := exec.Command(gitBranch.Name, gitBranch.Arguments...).Output()
19-
if err != nil {
20-
color.Red("Is it a git repo? Error getting current branch")
21-
return
16+
17+
cmdOut, cmdErr := utils.RunCommand(gitBranch.Name, gitBranch.Arguments, "")
18+
if cmdErr != nil {
19+
utils.LogFatal(cmdErr)
20+
}
21+
22+
output := cmdOut
23+
24+
lastCommitData := models.AllCommands["lastCommitData"]
25+
cmdOut, cmdErr = utils.RunCommand(lastCommitData.Name, lastCommitData.Arguments, "")
26+
if cmdErr == nil {
27+
output += "\n" + cmdOut
28+
}
29+
30+
lastCommitAuthor := models.AllCommands["lastCommitAuthor"]
31+
cmdOut, cmdErr = utils.RunCommand(lastCommitAuthor.Name, lastCommitAuthor.Arguments, "")
32+
if cmdErr == nil {
33+
output += "\n" + cmdOut
2234
}
23-
branch = branch[:len(branch)-1]
24-
color.Green("Current branch: %s", branch)
35+
36+
utils.LogOutput(output)
2537

2638
},
2739
}

cmd/checkout.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package cmd
22

33
import (
4-
"os"
5-
"os/exec"
6-
7-
"github.com/fatih/color"
84
"github.com/rajnandan1/okgit/models"
5+
"github.com/rajnandan1/okgit/utils"
96
"github.com/spf13/cobra"
107
)
118

@@ -22,14 +19,11 @@ var checkoutCmd = &cobra.Command{
2219

2320
gitCheckout := models.AllCommands["gitCheckout"]
2421
gitCheckout.Arguments = append(gitCheckout.Arguments, args...)
25-
xmd := exec.Command(gitCheckout.Name, gitCheckout.Arguments...)
26-
xmd.Stdout = os.Stdout
27-
xmd.Stderr = os.Stderr
28-
if xmd.Run() == nil {
29-
color.Green("✔ Switched branches or restored files successfully")
30-
} else {
31-
color.Red("⨯ Error switching branches or restoring files")
22+
cmdOut, cmdErr := utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "")
23+
if cmdErr != nil {
24+
utils.LogFatal(cmdErr)
3225
}
26+
utils.LogOutput(cmdOut)
3327

3428
},
3529
}

cmd/commit.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"os"
6-
"os/exec"
4+
"errors"
75
"strings"
86

97
"github.com/fatih/color"
@@ -44,17 +42,15 @@ var commitCmd = &cobra.Command{
4442

4543
gitBranch := models.AllCommands["gitBranch"]
4644

47-
branch, err := exec.Command(gitBranch.Name, gitBranch.Arguments...).Output()
48-
if err != nil {
49-
color.Red("Is it a git repo? Error getting current branch")
50-
return
45+
branch, cmdErr := utils.RunCommand(gitBranch.Name, gitBranch.Arguments, "")
46+
if cmdErr != nil {
47+
utils.LogFatal(cmdErr)
5148
}
52-
branch = branch[:len(branch)-1]
53-
if storedCommit, err := utils.GetLastCommitForBranchFromFile(string(branch)); err == nil {
49+
if storedCommit, err := utils.GetLastCommitForBranchFromFile(branch); err == nil {
5450
myCommit = *storedCommit
5551
}
5652
if myCommit.Type == "" {
57-
myCommit.Type = getCommitTypeFromBranchName(string(branch))
53+
myCommit.Type = getCommitTypeFromBranchName(branch)
5854
}
5955

6056
// Ask for the commit type
@@ -65,16 +61,14 @@ var commitCmd = &cobra.Command{
6561

6662
commitTypeInput := utils.ReadInput(false)
6763
if commitTypeInput == "" && myCommit.Type == "" {
68-
color.Red("Commit type is required.")
69-
return
64+
utils.LogFatal(errors.New("Commit type is required."))
7065
}
7166
if commitTypeInput != "" {
7267
myCommit.Type = commitTypeInput
7368
}
7469

7570
if !contains(commitTypes, myCommit.Type) {
76-
color.Red("Invalid commit type. Please provide a valid commit type.")
77-
return
71+
utils.LogFatal(errors.New("Invalid commit type. Please provide a valid commit type."))
7872
}
7973

8074
// Ask for the commit scope
@@ -102,8 +96,7 @@ var commitCmd = &cobra.Command{
10296
}
10397

10498
if myCommit.Summary == "" {
105-
color.Red("Commit summary is required.")
106-
return
99+
utils.LogFatal(errors.New("Commit summary is required."))
107100
}
108101

109102
// Ask for the commit message
@@ -162,26 +155,18 @@ var commitCmd = &cobra.Command{
162155

163156
commit := generateCommit(myCommit)
164157

165-
fmt.Println("Generated commit message:")
166-
fmt.Println(commit)
167158
utils.CreateDirectoryAndFileIfNotExist()
168-
err = utils.AddCommitToBranchFile(string(branch), myCommit)
159+
err := utils.AddCommitToBranchFile(string(branch), myCommit)
169160
if err != nil {
170-
color.Red("Error adding commit to branch file:", err)
171-
return
161+
utils.LogFatal(errors.New("Error adding commit to branch file:"))
172162
}
173163

174164
gitCommit := models.AllCommands["gitCommit"]
175-
xmd := exec.Command(gitCommit.Name, gitCommit.Arguments...)
176-
xmd.Stdout = os.Stdout
177-
xmd.Stderr = os.Stderr
178-
xmd.Stdin = strings.NewReader(commit)
179-
xmderr := xmd.Run()
180-
if xmderr == nil {
181-
color.Green("✔ Committed changes successfully")
182-
} else {
183-
color.Red("⨯ Error committing changes")
165+
cmdOutCommit, cmdErr := utils.RunCommand(gitCommit.Name, gitCommit.Arguments, commit)
166+
if cmdErr != nil {
167+
utils.LogFatal(cmdErr)
184168
}
169+
utils.LogOutput(cmdOutCommit)
185170

186171
},
187172
}

cmd/pull.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package cmd
22

33
import (
4-
"os"
5-
"os/exec"
4+
"errors"
65

7-
"github.com/fatih/color"
86
"github.com/rajnandan1/okgit/models"
7+
"github.com/rajnandan1/okgit/utils"
98
"github.com/spf13/cobra"
109
)
1110

@@ -17,46 +16,37 @@ var pullCmd = &cobra.Command{
1716

1817
//get current branch
1918
gitBracnh := models.AllCommands["gitBranch"]
20-
branch, err := exec.Command(gitBracnh.Name, gitBracnh.Arguments...).Output()
19+
branch, err := utils.RunCommand(gitBracnh.Name, gitBracnh.Arguments, "")
2120
if err != nil {
22-
branch = []byte("")
23-
} else {
24-
branch = branch[:len(branch)-1]
21+
branch = ""
2522
}
2623

2724
//expect the args[0] to be a branch name
2825
if len(args) > 0 {
29-
branch = []byte(args[0])
26+
branch = args[0]
3027
}
3128

3229
if len(branch) == 0 {
33-
color.Red("Error getting branch name")
34-
return
30+
utils.LogFatal(errors.New("Please provide the branch name to pull changes"))
3531
}
3632

3733
//checkout the branch
3834
gitCheckout := models.AllCommands["gitCheckout"]
3935
gitCheckout.Arguments = append(gitCheckout.Arguments, string(branch))
40-
xmd := exec.Command(gitCheckout.Name, gitCheckout.Arguments...)
41-
xmd.Stdout = os.Stdout
42-
xmd.Stderr = os.Stderr
43-
if xmd.Run() == nil {
44-
color.Green("✔ Checked out branch successfully")
45-
} else {
46-
color.Red("⨯ Error checking out branch")
47-
return
36+
cmdOut, cmdErr := utils.RunCommand(gitCheckout.Name, gitCheckout.Arguments, "")
37+
if cmdErr != nil {
38+
utils.LogFatal(cmdErr)
4839
}
40+
utils.LogOutput(cmdOut)
4941

5042
gitPull := models.AllCommands["gitPull"]
5143
gitPull.Arguments = append(gitPull.Arguments, string(branch))
52-
xmd = exec.Command(gitPull.Name, gitPull.Arguments...)
53-
xmd.Stdout = os.Stdout
54-
xmd.Stderr = os.Stderr
55-
if xmd.Run() == nil {
56-
color.Green("✔ Pulled changes successfully")
57-
} else {
58-
color.Red("⨯ Error pulling changes")
44+
cmdOut, cmdErr = utils.RunCommand(gitPull.Name, gitPull.Arguments, "")
45+
if cmdErr != nil {
46+
utils.LogFatal(cmdErr)
5947
}
48+
utils.LogOutput(cmdOut)
49+
6050
},
6151
}
6252

cmd/push.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package cmd
22

33
import (
4-
"os"
5-
"os/exec"
6-
7-
"github.com/fatih/color"
84
"github.com/rajnandan1/okgit/models"
5+
"github.com/rajnandan1/okgit/utils"
96
"github.com/spf13/cobra"
107
)
118

@@ -16,14 +13,12 @@ var pushCmd = &cobra.Command{
1613
Run: func(cmd *cobra.Command, args []string) {
1714

1815
gitPush := models.AllCommands["gitPush"]
19-
xmd := exec.Command(gitPush.Name, gitPush.Arguments...)
20-
xmd.Stdout = os.Stdout
21-
xmd.Stderr = os.Stderr
22-
if xmd.Run() == nil {
23-
color.Green("✔ Pushed changes successfully")
24-
} else {
25-
color.Red("⨯ Error pushing changes")
16+
cmdOut, cmdErr := utils.RunCommand(gitPush.Name, gitPush.Arguments, "")
17+
if cmdErr != nil {
18+
utils.LogFatal(cmdErr)
2619
}
20+
utils.LogOutput(cmdOut)
21+
2722
},
2823
}
2924

cmd/reset.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package cmd
22

33
import (
4-
"os"
5-
"os/exec"
6-
7-
"github.com/fatih/color"
84
"github.com/rajnandan1/okgit/models"
5+
"github.com/rajnandan1/okgit/utils"
96
"github.com/spf13/cobra"
107
)
118

@@ -16,14 +13,12 @@ var resetCmd = &cobra.Command{
1613
Run: func(cmd *cobra.Command, args []string) {
1714

1815
gitReset := models.AllCommands["gitReset"]
19-
xmd := exec.Command(gitReset.Name, gitReset.Arguments...)
20-
xmd.Stdout = os.Stdout
21-
xmd.Stderr = os.Stderr
22-
if xmd.Run() == nil {
23-
color.Green("✔ Reset changes successfully")
24-
} else {
25-
color.Red("⨯ Error resetting changes")
16+
cmdOut, cmdErr := utils.RunCommand(gitReset.Name, gitReset.Arguments, "")
17+
if cmdErr != nil {
18+
utils.LogFatal(cmdErr)
2619
}
20+
utils.LogOutput(cmdOut)
21+
2722
},
2823
}
2924

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var rootCmd = &cobra.Command{
1717
// Uncomment the following line if your bare application
1818
// has an action associated with it:
1919
// Run: func(cmd *cobra.Command, args []string) { },
20-
Version: "1.0.7",
20+
Version: "1.0.8",
2121
}
2222

2323
// Execute adds all child commands to the root command and sets flags appropriately.

0 commit comments

Comments
 (0)