-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
177 lines (146 loc) · 3.9 KB
/
cli.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
package main
import (
"bytes"
"flag"
"fmt"
"github.com/github/hub/cmd"
"github.com/github/hub/git"
"github.com/github/hub/github"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"text/template"
)
// Exit codes are int values that represent an exit code for a particular error.
const (
ExitCodeOK int = 0
ExitCodeError int = 1 + iota
ConfigFileName string = ".ghf-template"
)
// CLI is the command line object
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
func (cli *CLI) getTemplate() string {
gitDir, err := git.Dir()
if err != nil {
fmt.Fprintf(cli.errStream, err.Error())
return ""
}
config_file := filepath.Join(gitDir, "..", ConfigFileName)
if _, err := os.Stat(config_file); err != nil {
current_user, _ := user.Current()
config_file = filepath.Join(current_user.HomeDir, ConfigFileName)
if _, err := os.Stat(config_file); err != nil {
fmt.Fprintf(cli.errStream, err.Error())
return ""
}
}
text, err := ioutil.ReadFile(config_file)
if err != nil {
fmt.Fprintf(cli.errStream, err.Error())
return ""
}
return strings.TrimRight(string(text), "\n")
}
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
var (
noEditor bool
params string
template_file string
force bool
issue string
base string
head string
browse bool
version bool
)
// Define option flag parse
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.BoolVar(&noEditor, "no-editor", false, "Disable opening $EDITOR")
flags.BoolVar(&noEditor, "n", false, "Disable opening $EDITOR(Short)")
flags.StringVar(¶ms, "params", "", "Parameters for template")
flags.StringVar(¶ms, "p", "", "Parameters for template(Short)")
flags.StringVar(&template_file, "template", "", "Template filename")
flags.StringVar(&template_file, "t", "", "Template filename(Short)")
// parameters for hub pull-request command
flags.BoolVar(&force, "force", false, "")
flags.BoolVar(&force, "f", false, "(Short)")
flags.BoolVar(&browse, "browse", false, "")
flags.BoolVar(&browse, "o", false, "(Short)")
flags.StringVar(&issue, "issue", "", "")
flags.StringVar(&issue, "i", "", "(Short)")
flags.StringVar(&base, "base", "", "")
flags.StringVar(&base, "b", "", "(Short)")
flags.StringVar(&head, "head", "", "")
flags.StringVar(&head, "h", "", "(Short)")
flags.BoolVar(&version, "version", false, "Print version information and quit.")
// Parse commandline flag
if err := flags.Parse(args[1:]); err != nil {
return ExitCodeError
}
// Show version
if version {
fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version)
return ExitCodeOK
}
if template_file == "" {
template_file = cli.getTemplate()
if template_file == "" {
return ExitCodeError
}
}
params_map := make(map[string]string)
if params != "" {
for _, p := range strings.Split(params, ",") {
param := strings.Split(p, ":")
params_map[param[0]] = param[1]
}
}
tmpl := template.Must(template.ParseFiles(template_file))
out := new(bytes.Buffer)
tmpl.Execute(out, params_map)
content := out.String()
if !noEditor {
editor, err := github.NewEditor("PULLREQ", "pull request", content)
if err != nil {
return ExitCodeError
}
defer editor.DeleteFile()
editor.CS = "//"
title, body, err := editor.EditTitleAndBody()
if err != nil {
return ExitCodeError
}
content = title + "\n\n" + body
}
hubCmd := cmd.New("hub pull-request")
hubCmd.WithArg("-m " + content)
if force {
hubCmd.WithArg("-f")
}
if browse {
hubCmd.WithArg("-o")
}
if issue != "" {
hubCmd.WithArg("-i " + issue)
}
if base != "" {
hubCmd.WithArg("-b " + base)
}
if head != "" {
hubCmd.WithArg("-h " + head)
}
err := hubCmd.Spawn()
if err != nil {
return ExitCodeError
}
return ExitCodeOK
}