forked from derekparker/go-pear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
293 lines (232 loc) · 5.23 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
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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"sort"
"strings"
"github.com/jessevdk/go-flags"
"github.com/libgit2/git2go"
"gopkg.in/v1/yaml"
)
const version = "1.3.2"
type Config struct {
Email string
Devs map[string]string
}
type options struct {
File string `short:"f" long:"file" description:"Optional alternative git config file"`
Email string `short:"e" long:"email" description:"Base author email"`
Global bool `short:"g" long:"global" description:"Modify global git settings"`
Unset bool `short:"u" long:"unset" description:"Unset local pear information"`
Version bool `short:"v" long:"version" description:"Print version string"`
}
func pearrcpath() string {
return path.Join(os.Getenv("HOME"), ".pearrc")
}
func parseFlags() ([]string, *options, error) {
opts := &options{}
devs, err := flags.ParseArgs(opts, os.Args[1:])
if err != nil {
return nil, nil, err
}
return devs, opts, nil
}
func initGitConfig(opts *options) (*git.Config, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
usr, err := user.Current()
if err != nil {
return nil, err
}
repopath, err := git.Discover(wd, false, []string{usr.HomeDir})
if err != nil {
return nil, err
}
repo, err := git.OpenRepository(repopath)
if err != nil {
return nil, err
}
gitconf, err := repo.Config()
if err != nil {
return nil, err
}
if opts.File != "" {
err = gitconf.AddFile(opts.File, git.ConfigLevelApp, false)
if err != nil {
return nil, err
}
}
if opts.Global {
glconfpath := path.Join(usr.HomeDir, ".gitconfig")
err = gitconf.AddFile(glconfpath, git.ConfigLevelGlobal, false)
if err != nil {
return nil, err
}
}
return gitconf, nil
}
func printStderrAndDie(err error) {
os.Stderr.WriteString(err.Error())
os.Exit(1)
}
func main() {
devs, opts, err := parseFlags()
if err != nil {
return
}
if opts.Version {
fmt.Printf("Pear version %s\n", version)
os.Exit(0)
}
gitconfig, err := initGitConfig(opts)
if err != nil {
printStderrAndDie(err)
}
defer gitconfig.Free()
if len(os.Args) == 1 {
fmt.Println(username(gitconfig))
os.Exit(0)
}
conf, err := readPearrc(pearrcpath())
if err != nil {
printStderrAndDie(err)
}
sanitizeDevNames(devs)
if opts.Unset {
removePair(gitconfig)
os.Exit(0)
}
var (
fullnames = checkPair(devs, conf)
email = formatEmail(checkEmail(conf), devs)
)
setPair(email, fullnames, gitconfig)
savePearrc(conf, pearrcpath())
}
func username(gitconfig *git.Config) string {
name, err := gitconfig.LookupString("user.name")
if err != nil {
printStderrAndDie(err)
}
return name
}
func email(gitconfig *git.Config) string {
email, err := gitconfig.LookupString("user.email")
if err != nil {
printStderrAndDie(err)
}
return email
}
func setPair(email string, pairs []string, gitconfig *git.Config) {
pair := strings.Join(pairs, " and ")
err := gitconfig.SetString("user.name", pair)
if err != nil {
printStderrAndDie(err)
}
err = gitconfig.SetString("user.email", email)
if err != nil {
printStderrAndDie(err)
}
}
func removePair(gitconfig *git.Config) {
err := gitconfig.Delete("user.name")
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
}
err = gitconfig.Delete("user.email")
if err != nil {
os.Stderr.WriteString(err.Error())
}
}
func checkEmail(conf *Config) string {
if conf.Email == "" {
conf.Email = getEmail()
}
return conf.Email
}
func checkPair(pair []string, conf *Config) []string {
var fullnames []string
for _, dev := range pair {
if _, ok := conf.Devs[dev]; !ok {
conf.Devs[dev] = getName(dev)
}
fullnames = append(fullnames, conf.Devs[dev])
}
return fullnames
}
func getName(devName string) string {
prompt := fmt.Sprintf("Please enter a full name for %s:", devName)
return promptForInput(prompt)
}
func getEmail() string {
return promptForInput("Please provide base author email:")
}
func promptForInput(prompt string) string {
_, err := fmt.Println(prompt)
if err != nil {
log.Fatal(err)
}
return readInput()
}
func readInput() string {
buf := bufio.NewReader(os.Stdin)
inputString, err := buf.ReadString('\n')
if err != nil {
log.Fatal("Could not read from stdin: ", err)
}
return trimNewline(inputString)
}
func savePearrc(conf *Config, path string) error {
contents, err := yaml.Marshal(conf)
if err != nil {
return err
}
err = ioutil.WriteFile(path, contents, os.ModeExclusive)
if err != nil {
return err
}
return nil
}
func readPearrc(path string) (*Config, error) {
conf := &Config{
Devs: make(map[string]string),
}
file, err := os.Open(path)
if err != nil {
file, err = os.Create(path)
if err != nil {
return nil, err
}
}
defer file.Close()
contents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(contents, conf)
if err != nil {
return nil, err
}
return conf, nil
}
func formatEmail(email string, devs []string) string {
parts := strings.Split(email, "@")
devlist := strings.Join(devs, "+")
return fmt.Sprintf("%s+%s@%s", parts[0], devlist, parts[1])
}
func trimNewline(s string) string {
return strings.TrimSuffix(s, "\n")
}
func sanitizeDevNames(devs []string) {
for i, dev := range devs {
devs[i] = strings.ToLower(dev)
}
sort.Strings(devs)
}