-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
81 lines (69 loc) 路 1.56 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
// Command pair allows to set and manage co-author declarations
// for seamless pairing sessions using Git and Github.
package main
import (
"bytes"
"fmt"
"io"
"os"
"github.com/gonzalo-bulnes/pair"
)
type config struct {
templateName string
pair string
}
type argumentsError struct{}
func (e *argumentsError) Error() string {
return "Usage: pair with <email>\n\nExample:\n\n pair with 'Alice <alice@example.com>'\n"
}
func main() {
err := checkArgs(os.Args)
if err != nil {
fail(os.Stderr, err, 0)
}
switch os.Args[1] {
case "--version":
_ = pair.Version(os.Stdout, os.Stderr)
os.Exit(0)
case "with":
err = pair.With(os.Stdout, os.Stderr, os.Args[2])
if err != nil {
os.Exit(fail(os.Stderr, err, 20))
}
os.Exit(0)
case "stop":
err = pair.Stop(os.Stdout, os.Stderr)
if err != nil {
os.Exit(fail(os.Stderr, err, 21))
}
os.Exit(0)
default:
}
}
func checkArgs(args []string) error {
if len(args) == 3 && args[1] == "with" {
return nil
}
if len(args) == 2 && args[1] == "stop" {
return nil
}
if len(args) == 2 && args[1] == "--version" {
return nil
}
return &argumentsError{}
}
func fail(errors io.Writer, err error, code int) int {
if e, ok := err.(*argumentsError); ok {
fmt.Fprint(errors, e)
return code
}
var version bytes.Buffer
pair.Version(&version, errors)
fmt.Fprintf(errors, `
Oh no! You might have found a bug in pair!
Please open an issue mentioning (error %d) and maybe we can pair to fix it : )
https://github.com/gonzalo-bulnes/pair/issues
%serror: %v
`, code, version.String(), err)
return code
}