-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-yolo.go
153 lines (130 loc) · 3.19 KB
/
git-yolo.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/mitchellh/go-homedir"
"github.com/radovskyb/watcher"
)
func logErrror(err error) {
if err != nil {
log.Println(err)
}
}
func ePrint(err error, msg string) {
logErrror(err)
if err != nil {
log.Println("ERROR START >>> ")
log.Println(fmt.Sprintf("%s", msg))
log.Println("ERROR END <<<")
}
}
func exitFail(msg string) {
// currently a wrapper, may change functionality later
log.Fatalln(msg)
}
func exitOnError(err error, msg string) {
logErrror(err)
exitFail(msg)
}
func readFile(filepath string) []string {
fileLines := []string{}
file, err := os.Open(filepath)
if err != nil {
log.Fatalln(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fileLines = append(fileLines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return fileLines
}
func getMessages() []string {
messageList := []string{}
msgFileList := []string{}
yoloDir, err := homedir.Dir()
logErrror(err)
yoloDir = filepath.Join(yoloDir, ".gityolo")
pathInfo, err := os.Stat(yoloDir)
// exitOnError(err, fmt.Sprintf("FATAL: Directory %s does not exist!", yoloDir))
if !os.FileMode.IsDir(pathInfo.Mode()) {
exitFail(fmt.Sprintf("FATAL: Expected %s to be a directory", yoloDir))
}
// get a list of all text files in yoloDir
err = filepath.Walk(yoloDir,
func(path string, info os.FileInfo, err error) error {
if err == nil {
if strings.HasSuffix(path, ".txt") {
pathInfo, err = os.Stat(path)
if os.FileMode.IsRegular(pathInfo.Mode()) {
msgFileList = append(msgFileList, path)
}
}
}
return err
})
logErrror(err)
for _, theFile := range msgFileList {
theContents := readFile(theFile)
for _, theLine := range theContents {
messageList = append(messageList, theLine)
}
}
return messageList
}
func pickMessage(messageList *[]string, r *rand.Rand) string {
return (*messageList)[r.Intn(len(*messageList))]
}
func runCmd(cmd *exec.Cmd) error {
output, err := cmd.CombinedOutput()
ePrint(err, string(output))
return err
}
func GitYolo(messageList *[]string, r *rand.Rand) {
// add gitignore ignored files
gitAdd := exec.Command("git", "add", ".", "--force")
// commit with random messag
gitCommit := exec.Command("git", "commit", "-m", pickMessage(messageList, r))
// (force) push to master. what could possibly go wrong?
gitPushForce := exec.Command("git", "push", "--force", "origin", "master")
gitPush := exec.Command("git", "push", "--force")
runCmd(gitAdd)
runCmd(gitCommit)
runCmd(gitPushForce)
runCmd(gitPush)
}
func runWatcher(messageList *[]string, r *rand.Rand) {
theWatcher := watcher.New()
defer theWatcher.Close()
go func() {
for {
select {
case event := <-theWatcher.Event:
log.Println(event)
GitYolo(messageList, r)
case err := <-theWatcher.Error:
log.Println("error:", err)
}
}
}()
theWatcher.AddRecursive(".")
theWatcher.Ignore(".git")
if err := theWatcher.Start(time.Millisecond); err != nil {
log.Fatalln(err)
}
}
func main() {
messageList := getMessages()
r := rand.New(rand.NewSource(time.Now().Unix()))
runWatcher(&messageList, r)
}