-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
179 lines (161 loc) · 4.34 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v2"
)
// Command represents a command to run.
type Command struct {
Path string `yaml:"path"`
Args []string `yaml:"args"`
}
// Validate the command is usable.
func (c Command) Validate() error {
if c.Path == "" {
return fmt.Errorf("command must specify 'path'")
}
return nil
}
// Config for gitbot.
type Config struct {
Repos []string `yaml:"repos"`
BasePath string `yaml:"base_path"`
ChangeCmds []Command `yaml:"change_cmds"`
PostCmds []Command `yaml:"post_cmds"`
}
// NormalizePath will return an absolute path from a relative one, with
// the assumption that is is relative to the location of the configuration file.
func NormalizePath(configPath, commandPath string) string {
if !strings.HasPrefix(commandPath, ".") {
return commandPath
}
return filepath.Join(filepath.Dir(configPath), commandPath)
}
// Validate that the config is usable.
func (c Config) Validate() error {
if len(c.Repos) == 0 {
return fmt.Errorf("config must contain a non-empty 'repos' list")
}
for _, changecmd := range c.ChangeCmds {
if err := changecmd.Validate(); err != nil {
return err
}
}
if len(c.PostCmds) == 0 {
return fmt.Errorf("config must contain a non-empty 'post_cmds' list")
}
for _, postcmd := range c.PostCmds {
if err := postcmd.Validate(); err != nil {
return err
}
}
return nil
}
func main() {
version := flag.Bool("version", false, "Shows version and exits")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [config]:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Flags: \n")
flag.PrintDefaults()
}
flag.Parse()
if *version {
fmt.Printf("gitbot %s\n", Version)
os.Exit(0)
}
if len(flag.Args()) != 1 {
flag.Usage()
os.Exit(1)
}
cfgfilePath := flag.Args()[0]
cfgfile, err := os.Open(cfgfilePath)
if err != nil {
log.Fatal(err)
}
defer cfgfile.Close()
cfgfiledata, err := ioutil.ReadAll(cfgfile)
if err != nil {
log.Fatal(err)
}
var cfg Config
if err := yaml.Unmarshal(cfgfiledata, &cfg); err != nil {
log.Fatal(err)
}
if err := cfg.Validate(); err != nil {
log.Fatal(err)
}
basePath := cfg.BasePath
for _, repo := range cfg.Repos {
// clone repo to temp directory
tempdir, err := ioutil.TempDir(basePath+os.TempDir(), "")
if err != nil {
log.Fatal(err)
}
log.Printf("%s: cloning to %s", repo, tempdir)
if os.Getenv("GITBOT_LEAVE_TEMPDIRS") == "" {
defer os.RemoveAll(tempdir)
}
clonecmd := exec.Command("git", "clone", "--depth", "1", repo, tempdir)
clonecmd.Dir = tempdir
clonecmd.Stdout = os.Stdout
clonecmd.Stderr = os.Stderr
if err := clonecmd.Run(); err != nil {
log.Fatalf("%s: error cloning: %s", repo, err)
}
// make changes
log.Printf("%s: making changes", repo)
noChangesMade := true
for _, changecmd := range cfg.ChangeCmds {
commandPath := NormalizePath(cfgfilePath, changecmd.Path)
changecmd := exec.Command(commandPath, append(changecmd.Args, tempdir)...)
var changecmdstdout bytes.Buffer
changecmd.Stdout = io.MultiWriter(os.Stdout, &changecmdstdout)
changecmd.Stderr = os.Stderr
if err := changecmd.Run(); err != nil {
log.Printf("%s: no changes to make", repo)
continue
}
noChangesMade = false
// commit changes
log.Printf("%s: committing changes", repo)
gitaddcmd := exec.Command("git", "add", "-A")
gitaddcmd.Dir = tempdir
gitaddcmd.Stdout = os.Stdout
gitaddcmd.Stderr = os.Stderr
if err := gitaddcmd.Run(); err != nil {
log.Fatalf("%s: error adding: %s", repo, err)
}
commitcmd := exec.Command("git", "commit", "-m", changecmdstdout.String())
commitcmd.Dir = tempdir
commitcmd.Stdout = os.Stdout
commitcmd.Stderr = os.Stderr
if err := commitcmd.Run(); err != nil {
log.Fatalf("%s: error committing: %s", repo, err)
}
}
// don't run post commands if none of the change commands made a change
if noChangesMade {
continue
}
// run post commands
log.Printf("%s: running post commands", repo)
for _, postcmd := range cfg.PostCmds {
postcmdPath := NormalizePath(cfgfilePath, postcmd.Path)
cmd := exec.Command(postcmdPath, postcmd.Args...)
cmd.Dir = tempdir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("%s: error running post command: %s", repo, err)
}
}
}
}