-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (111 loc) · 4.06 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
package main
import (
"brcha/command"
"brcha/common"
"brcha/log"
"brcha/network"
"flag"
"net/http"
"os"
)
const (
emptyCommandArguments string = `Use "brcha -h" or "brcha -help" for more information.`
helpCommandOutput string = `
Usage:
brcha [arguments]
The arguments are:
-i <issue-key>
-t <branch-type>
-clean
-r <remote>
-assignee <username>
Available branch types:
build, b: Changes that affect the build system or external dependencies (example scopes: gradle, npm)
chore, ch: Routine tasks that don't affect the functionality or user-facing aspects of a project
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs, d: Documentation only changes
feat, ft: A new feature
fix, fx: A bug fix
perf, p: A code change that improves performance
refactor, rf: A code change that neither fixes a bug nor adds a feature
revert, rv: A code that restors to a previous or default condition
style, s: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
test, t: Adding missing tests or correcting existing tests
Examples:
~% brcha -i XX-111
~% branch created: task/XX-111_jira-issue-name
~%
~% brcha -i XX-111 -t fx
~% branch created: fix/XX-111_jira-issue-name
~%
~% brcha -clean
~% branch deleted: fix/XX-111_jira-issue-name
~%
~% brcha -clean -r origin
~% branch deleted: fix/XX-111_jira-issue-name
~% branch deleted: origin/fix/XX-111_jira-issue-name
~%
~% brcha -clean -r origin -assignee example.user
~% branch deleted: fix/XX-111_jira-issue-name
~% branch deleted: origin/fix/XX-111_jira-issue-name`
)
func main() {
input := readUserInput()
if input.HasFlag(common.HelpFlag) {
log.Info().Println(helpCommandOutput)
os.Exit(0)
}
if err := command.ReadEnvVariables(); err != nil {
log.Error().Println(err)
os.Exit(1)
}
httpClient := &http.Client{}
client := network.NewClient(httpClient)
var cmd command.BrchaCommand
if input.HasFlag(common.CleanFlag) {
cmd = command.NewDeleteLocalBranchCommand(client, input)
} else {
cmd = command.NewCreateLocalBranchCommand(client, input)
}
if err := cmd.Execute(); err != nil {
log.Error().Println(err)
os.Exit(1)
}
}
func readUserInput() *common.Input {
var input = &common.Input{
Flags: common.EmptyFlag,
Arguments: make(map[common.InputType]string),
}
help := flag.Bool("help", false, "displays all available commands")
issue := flag.String("i", "", "issue key")
branchType := flag.String("t", "", "(optional) overrides the type of branch")
clean := flag.Bool("clean", false, "deletes all local branches with Jira status Done")
remote := flag.String("r", "", "(optional) provides remote to delete branch in origin")
assignee := flag.String("assignee", "", "(optional) provides assignee to delete remote branch")
flag.Parse()
if help != nil && *help {
input.AddFlag(common.HelpFlag)
}
if issue != nil && *issue != "" {
input.Arguments[common.Issue] = *issue
if branchType != nil && *branchType != "" {
input.Arguments[common.BranchType] = *branchType
}
}
if clean != nil && *clean {
input.AddFlag(common.CleanFlag)
if remote != nil && *remote != "" {
input.Arguments[common.Remote] = *remote
}
if assignee != nil && *assignee != "" {
input.Arguments[common.Assignee] = *assignee
}
}
if (len(os.Args) == 1) || input.Flags == common.EmptyFlag && (len(input.Arguments) == 0) {
log.Error().Println(emptyCommandArguments)
os.Exit(0)
}
log.Debug().Printf("user input: flags=%d args=%+v", input.Flags, input.Arguments)
return input
}