-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (90 loc) · 1.97 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
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/tjarratt/babble"
"github.com/urfave/cli/v2"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
func main() {
app := &cli.App{
Name: "git-generate",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "commits",
Aliases: []string{"c"},
Value: 1,
Usage: "generate number of commits",
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Value: "test.txt",
Usage: "name of the generated file",
},
&cli.StringFlag{
Name: "prefix",
Value: "",
Usage: "prefix for commit messages",
},
},
Action: func(c *cli.Context) error {
r, err := git.PlainOpen(".")
if err != nil {
return err
}
w, err := r.Worktree()
if err != nil {
return err
}
f, err := os.OpenFile(c.String("file"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
babbler := babble.NewBabbler()
for i := 0; i < c.Int("commits"); i++ {
if _, err := f.WriteString(fmt.Sprintf("%s\n", babbler.Babble())); err != nil {
return err
}
if _, err := w.Add(c.String("file")); err != nil {
return err
}
commitMsg, err := generateCommitMsg()
if err != nil {
return err
}
commit, err := w.Commit(fmt.Sprintf("%s%s", c.String("prefix"), commitMsg), &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
},
})
if _, err := r.CommitObject(commit); err != nil {
return err
}
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("fatal error: %s", err)
}
}
func generateCommitMsg() (string, error) {
res, err := http.Get("http://whatthecommit.com/index.txt")
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(body), nil
}