-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrb.go
114 lines (95 loc) · 2.37 KB
/
grb.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
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/cryptojuice/grb/repositories"
"github.com/urfave/cli"
)
func Filter(branches []string, searchString string) []string {
filtered := branches[:0]
for _, b := range branches {
if strings.Contains(b, searchString) {
filtered = append(filtered, b)
}
}
return filtered
}
func main() {
app := cli.NewApp()
app.Name = "grb"
app.Version = "0.1.2"
app.Usage = "grb [global options] \"search terms\""
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "delete, d",
Usage: "Deletes remote branches return by search result.",
},
cli.BoolFlag{
Name: "local, l",
Usage: "Used with -d or --delete to remove local branch along with remote branches.",
},
cli.BoolFlag{
Name: "no-prompt, f",
Usage: "Attempt to delete without confirmation.",
},
cli.StringFlag{
Name: "remote, r",
Usage: "Alters git remote searched by grb. defaults to origin if flag is not provided.",
},
}
app.Action = func(c *cli.Context) {
var promptFlag = true
var deleteRemoteFlag = false
var deleteLocalFlag = false
var searchString string
var remoteRepository = repositories.Remote{
Name: "origin",
}
var localRepository = repositories.Local{}
if c.String("no-prompt") == "true" {
promptFlag = false
}
if len(c.String("remote")) > 0 {
remoteRepository.Name = c.String("remote")
}
if c.String("delete") == "true" {
deleteRemoteFlag = true
}
if c.String("local") == "true" {
deleteLocalFlag = true
}
branches := remoteRepository.Fetch()
localBranches := localRepository.Fetch()
if len(c.Args()) > 0 {
searchString = c.Args()[0]
}
if deleteRemoteFlag == true {
if len(c.Args()) > 0 && len(c.Args()[0]) > 0 {
for _, b := range Filter(branches, searchString) {
remoteRepository.DeleteBranch(b, promptFlag)
}
if deleteLocalFlag == true {
for _, b := range Filter(localBranches, searchString) {
localRepository.DeleteBranch(b, promptFlag)
}
}
} else {
log.Println("Please provide search terms.")
}
}
if len(c.Args()) == 0 {
for _, b := range branches {
fmt.Println(string(b[11:]))
}
}
if len(c.Args()) > 0 && deleteLocalFlag == false && deleteRemoteFlag == false {
searchString = c.Args()[0]
for _, b := range Filter(branches, searchString) {
fmt.Println(string(b[11:]))
}
}
}
app.Run(os.Args)
}