-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgit.go
195 lines (188 loc) · 4.61 KB
/
mgit.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/jonaz/mgit/pkg/models"
"github.com/jonaz/mgit/pkg/providers"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func main() {
logrus.SetOutput(os.Stderr)
logrus.SetFormatter(&logrus.TextFormatter{TimestampFormat: time.RFC3339Nano, FullTimestamp: true})
app := &cli.App{
Name: "mgit",
Usage: "manage multiple git repos at the same time",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bitbucket-url",
Value: "",
Usage: "url to bitbucket server",
Aliases: []string{"c"},
},
&cli.StringFlag{
Name: "dir",
Value: ".",
Usage: "temporary working directory for all the git repos",
},
&cli.StringFlag{
Name: "loglevel",
Value: "info",
},
},
Commands: []*cli.Command{
{
Name: "clone",
Usage: "clone all repos and make sure they are up to date",
Action: func(c *cli.Context) error {
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
whitelist := strings.FieldsFunc(c.String("whitelist"), func(c rune) bool {
return c == ','
})
return provider.Clone(whitelist, c.String("has-file"), c.String("content-regexp"))
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "has-file",
Usage: "only clone repo which has file",
},
&cli.StringFlag{
Name: "whitelist",
Usage: "only clone repos in comma separated list",
},
&cli.StringFlag{
Name: "content-regexp",
Usage: "only clone repos where has-file contains data that matches the regexp",
},
},
},
{
Name: "pull-request",
Aliases: []string{"pr"},
Usage: "multip PR open. Each PR will be opened in a new browser tab",
Action: func(c *cli.Context) error {
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
return provider.PR(nil, c.String("mode"))
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mode",
Usage: "open PR with 'api' or 'browser'",
Value: "browser",
},
},
},
{
Name: "git",
Usage: "run git commands in multiple repos",
Action: func(c *cli.Context) error {
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
return provider.Git(c.Args().Slice())
},
},
{
Name: "replace",
Usage: "replace text in multiple repos",
Action: func(c *cli.Context) error {
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
return provider.Replace(c.String("regexp"), c.String("with"), c.String("file-regexp"), c.String("path-regexp"), nil)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "with",
Usage: "content to replace that line with",
},
&cli.StringFlag{
Name: "regexp",
Usage: "regexp to find a line in file",
},
&cli.StringFlag{
Name: "file-regexp",
Usage: "regexp to filter files",
},
&cli.StringFlag{
Name: "path-regexp",
Usage: "regexp to filter files. includes full path from repo root",
},
},
},
{
Name: "command",
Usage: "run command in multiple repos",
Action: func(c *cli.Context) error {
provider, err := providers.GetProvider(c)
if err != nil {
return err
}
return runAction(provider, models.Action{Command: strings.Join(c.Args().Slice(), " ")})
},
},
{
Name: "playbook",
Usage: "run a playbook with combined commands",
Subcommands: []*cli.Command{
{
Name: "run",
Usage: "run a playbook with combined commands",
Action: runPlaybook,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Usage: "force push new git branch",
},
},
},
{
Name: "pull-request",
Aliases: []string{"pr"},
Usage: "open PR in a new browser tab for each repo in the playbook",
Action: openPR,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mode",
Usage: "open PR with 'api' or 'browser'",
Value: "browser",
},
},
},
{
Name: "generate",
Usage: "generate a playbook of currently cloned repos",
Action: generatePlaybook,
},
},
},
},
}
app.Before = globalBefore
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func globalBefore(c *cli.Context) error {
lvl, err := logrus.ParseLevel(c.String("loglevel"))
if err != nil {
return err
}
if lvl != logrus.InfoLevel {
fmt.Fprintf(os.Stderr, "using loglevel: %s\n", lvl.String())
}
logrus.SetLevel(lvl)
return nil
}