-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (70 loc) · 2.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/common-nighthawk/go-figure"
)
func executeGitAdd() {
fmt.Println("\n")
// git add .
out, err := exec.Command("git", "add", ".").CombinedOutput()
fmt.Println(string(out))
if err != nil {
log.Fatalf("executeGitAdd() failed: %s", err)
}
}
func executeGitCommit(commitMessage string) {
// git commit -m [commit message]
out, err := exec.Command("git", "commit", "-m", commitMessage).CombinedOutput()
// Check if we push a commit where nothing is going to be commited to the branch
// This is returned as an error - but we want to clean up the output of this and present this in less of a critical manner
// Therefor we return from those messages as exit(0) - which is fine and mimics normal Git behavior
if strings.Contains(string(out), "nothing to commit, working tree clean") {
// If the bool is true, log out the message in full to stdout still
fmt.Println(string(out))
// Call exit(0)
os.Exit(0)
}
// We put this after the first if statement so we don't duplicate stdout
fmt.Println(string(out))
if err != nil {
log.Fatalf("executeGitCommit() failed: %s", err)
}
}
func executeGitPush(gitRemote string, gitBranch string) {
// git push [remote] [branch]
gitPush := exec.Command("git", "push", gitRemote, gitBranch)
// Reassign command output to os output streams
gitPush.Stdout = os.Stdout
gitPush.Stderr = os.Stderr
// Runs the command
// Redirecting command stdout to os stdout captures all output for "git push" whereas CombinedOut() only grabs the last line ran
err := gitPush.Run()
if err != nil {
log.Fatalf("executeGitPush() failed: %s", err)
}
}
func main() {
// Documentation reference: https://github.com/common-nighthawk/go-figure
myFigure := figure.NewColorFigure("gutils", "", "blue", true)
myFigure.Print()
// Define a flag for the commit message to git
commitMessage := flag.String("c", "initial commit", "A commit message - defaults to 'initial commit'")
// Define a flag for the git remote
gitRemote := flag.String("r", "origin", "The specified upstream git remote to push to - defaults to origin")
// Define a flag for the branch to be pushed to
gitBranch := flag.String("b", "main", "The branch to push to - defaults to main")
// Parse all input
flag.Parse()
// flags package expects pointers back to the original values
// git add .
executeGitAdd()
// git commit [commit message]
executeGitCommit(*commitMessage)
// git push [remote] [branch]
executeGitPush(*gitRemote, *gitBranch)
}