-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
prm.go
357 lines (297 loc) · 8.14 KB
/
prm.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"errors"
"fmt"
"os"
"strconv"
"github.com/ldez/prm/v3/choose"
"github.com/ldez/prm/v3/cmd"
"github.com/ldez/prm/v3/config"
"github.com/ldez/prm/v3/local"
"github.com/ldez/prm/v3/meta"
"github.com/ldez/prm/v3/types"
"github.com/spf13/cobra"
)
func main() {
rootCmd := createRootCmd()
rootCmd.AddCommand(createVersionCmd())
rootCmd.AddCommand(createCheckoutCmd())
rootCmd.AddCommand(createRemoveCmd())
rootCmd.AddCommand(createPullCmd())
rootCmd.AddCommand(createPushCmd())
rootCmd.AddCommand(createPushForceCmd())
rootCmd.AddCommand(createListCmd())
rootCmd.AddCommand(createCloneCmd())
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func createRootCmd() *cobra.Command {
return &cobra.Command{
Use: "prm",
Short: "PRM - The Pull Request Manager.",
Long: `PRM - The Pull Request Manager.`,
Version: meta.GetVersion(),
PreRunE: safe,
RunE: func(_ *cobra.Command, _ []string) error {
return rootRun()
},
}
}
func createVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Display version information.",
Long: `Display version information.`,
Run: func(_ *cobra.Command, _ []string) {
meta.DisplayVersion()
},
}
}
func createCheckoutCmd() *cobra.Command {
checkoutCfg := types.CheckoutOptions{}
return &cobra.Command{
Use: "checkout [PR number]",
Aliases: []string{"c"},
Short: "Checkout a PR (create a local branch and add remote).",
Long: "Checkout a PR (create a local branch and add remote).",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
if err := cobra.MaximumNArgs(1)(cmd, args); err != nil {
return err
}
if len(args) == 1 {
val, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("argument must be a valid number: %w", err)
}
checkoutCfg.Number = val
}
return nil
},
PreRunE: safe,
RunE: func(_ *cobra.Command, _ []string) error {
if checkoutCfg.Number != 0 {
return cmd.Checkout(&checkoutCfg)
}
conf, err := config.Get()
if err != nil {
return err
}
return cmd.InteractiveCheckout(conf)
},
Example: ` $ prm checkout
$ prm checkout 1234
$ prm c
$ prm c 1234`,
}
}
func createRemoveCmd() *cobra.Command {
removeCfg := types.RemoveOptions{}
removeCmd := &cobra.Command{
Use: "rm [PR numbers]",
Aliases: []string{"remove"},
Short: "Remove one or more PRs from the current local repository.",
Long: "Remove one or more PRs from the current local repository.",
Args: func(_ *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
var values []int
for i, arg := range args {
val, err := strconv.Atoi(arg)
if err != nil {
return fmt.Errorf("argument %d must be a valid number: %w", i, err)
}
values = append(values, val)
}
removeCfg.Numbers = values
return nil
},
PreRunE: safe,
RunE: func(_ *cobra.Command, _ []string) error {
return removeRun(&removeCfg)
},
Example: ` $ prm rm
$ prm rm 1234
$ prm rm 1234 4567
$ prm remove
$ prm remove 1234
$ prm remove 1234 4567`,
}
removeCmd.Flags().BoolVarP(&removeCfg.All, "all", "a", false, "All pull requests.")
return removeCmd
}
func createPullCmd() *cobra.Command {
pullCfg := types.PullOptions{}
pullCmd := &cobra.Command{
Use: "pull",
Short: "Pull to the PR branch.",
Long: "Pull to the PR branch.",
RunE: func(_ *cobra.Command, _ []string) error {
return cmd.Pull(&pullCfg)
},
}
pullCmd.Flags().BoolVarP(&pullCfg.Force, "force", "f", false, "Force the pull.")
return pullCmd
}
func createPushCmd() *cobra.Command {
pushCfg := types.PushOptions{}
pushCmd := &cobra.Command{
Use: "push",
Short: "Push to the PR branch.",
Long: "Push to the PR branch.",
RunE: func(_ *cobra.Command, _ []string) error {
return cmd.Push(&pushCfg)
},
}
pushCmd.Flags().BoolVarP(&pushCfg.Force, "force", "f", false, "Force the push.")
return pushCmd
}
func createPushForceCmd() *cobra.Command {
pushForceCfg := types.PushOptions{Force: true}
return &cobra.Command{
Use: "pf",
Short: "Push force to the PR branch.",
Long: "Push force to the PR branch.",
RunE: func(_ *cobra.Command, _ []string) error {
return cmd.Push(&pushForceCfg)
},
}
}
func createListCmd() *cobra.Command {
listCfg := types.ListOptions{}
listCmd := &cobra.Command{
Use: "list",
Short: "Display all current PRs.",
Long: "Display all current PRs.",
RunE: func(_ *cobra.Command, _ []string) error {
return cmd.List(&listCfg)
},
Example: `$ prm list
$ prm list --all
$ prm list --all --skip-empty=false`,
}
listCmd.Flags().BoolVarP(&listCfg.All, "all", "a", false, "All pull requests.")
listCmd.Flags().BoolVarP(&listCfg.SkipEmpty, "skip-empty", "s", true, "Skip project with no PR. (only when --all option is used)")
return listCmd
}
func createCloneCmd() *cobra.Command {
cloneCfg := types.CloneOptions{}
cloneCmd := &cobra.Command{
Use: "clone [URL]",
Short: "Clone a repository and create a fork if needed.",
Long: "Clone a repository and create a fork if needed.",
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
cloneCfg.Repo = args[0]
return nil
},
RunE: func(_ *cobra.Command, _ []string) error {
return cmd.Clone(cloneCfg)
},
Example: `$ prm clone git@github.com:user/repo.git
$ prm clone https://github.com/user/repo.git
$ prm clone -n git@github.com:user/repo.git
$ prm clone -r git@github.com:user/repo.git
$ prm clone -o myorg git@github.com:user/repo.git`,
}
cloneCmd.Flags().BoolVarP(&cloneCfg.NoFork, "no-fork", "n", false, "Don't create fork on GitHub.")
cloneCmd.Flags().BoolVarP(&cloneCfg.UserAsRootDir, "user-as-root-dir", "r", false, "Username as root directory.")
cloneCmd.Flags().StringVarP(&cloneCfg.Organization, "org", "o", "", "The organization in which to create the fork instead of the user account.")
return cloneCmd
}
func rootRun() error {
conf, err := config.Get()
if err != nil {
return fmt.Errorf("failed to get configuration: %w", err)
}
action, err := choose.Action()
if err != nil {
return err
}
switch action {
case choose.ActionList:
return cmd.Switch(&types.ListOptions{})
case choose.ActionCheckout:
return cmd.InteractiveCheckout(conf)
case choose.ActionRemove:
return cmd.InteractiveRemove(conf)
case choose.ActionProjects:
return cmd.Switch(&types.ListOptions{All: true})
}
return nil
}
func removeRun(removeOptions *types.RemoveOptions) error {
if removeOptions.All {
return cmd.Remove(removeOptions)
}
if len(removeOptions.Numbers) == 0 {
conf, err := config.Get()
if err != nil {
return fmt.Errorf("failed to get configuration: %w", err)
}
return cmd.InteractiveRemove(conf)
}
return cmd.Remove(removeOptions)
}
func safe(_ *cobra.Command, _ []string) error {
_, err := config.Get()
if err == nil {
return nil
}
err = initProject()
if err != nil {
return fmt.Errorf("failed to init projet: %w", err)
}
return nil
}
func initProject() error {
// Get all remotes
remotes, err := local.GetRemotes()
if err != nil {
return fmt.Errorf("failed to get remotes: %w", err)
}
// get global configuration
confs, err := config.ReadFile()
if err != nil {
return fmt.Errorf("failed to read configuration file: %w", err)
}
repoDir, err := local.GetGitRepoRoot()
if err != nil {
return fmt.Errorf("failed to get git root directory: %w", err)
}
branches, err := local.GetBranches()
if err != nil {
return fmt.Errorf("failed to get git branch list: %w", err)
}
branch, err := choose.MainBranch(branches)
if err != nil {
return err
}
if branch == "" || branch == choose.ExitLabel {
return errors.New("no main branch chosen: exit")
}
var remoteName string
if len(remotes) == 1 {
remoteName = remotes[0].Name
} else {
remoteName, err = choose.GitRemote(remotes)
if err != nil {
return err
}
if remoteName == "" || remoteName == choose.ExitLabel {
return errors.New("no remote chosen: exit")
}
}
confs = append(confs, config.Configuration{
Directory: repoDir,
BaseRemote: remoteName,
MainBranch: branch,
})
return config.Save(confs)
}