forked from masutaka/github-nippou
/
init.go
222 lines (153 loc) · 3.95 KB
/
init.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
package lib
import (
"context"
"errors"
"fmt"
"os/exec"
"time"
)
// Init initializes github-nippou settings
func Init() error {
fmt.Print("** github-nippou Initialization **\n")
if err := setUser(); err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
ctx := context.Background()
if err := setAccessToken(ctx); err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
return createAndSetGist(ctx)
}
func setUser() error {
fmt.Print(`
== [Step: 1/3] GitHub user ==
`)
var msg string
if _, err := getUser(); err == nil {
msg = "Already initialized."
} else {
var user, answer string
fmt.Print("What's your GitHub account? ")
fmt.Scanln(&user)
if len(user) >= 1 {
fmt.Printf(`
The following command will be executed.
$ git config --global github-nippou.user %s
`, user)
fmt.Print("Are you sure? [y/n] ")
fmt.Scanln(&answer)
if string(answer[0]) != "y" {
return errors.New("Canceled")
}
cmd := exec.Command("git", "config", "--global", "github-nippou.user", user)
if err := cmd.Run(); err != nil {
return err
}
msg = "Thanks!"
}
}
fmt.Printf(`%s You can get it with the following command.
$ git config --global github-nippou.user
`, msg)
return nil
}
func setAccessToken(ctx context.Context) error {
fmt.Print(`
== [Step: 2/3] GitHub personal access token ==
To get new token with ` + "`repo`" + ` and ` + "`gist`" + ` scope, visit
https://github.com/settings/tokens/new
`)
var msg string
accessToken, err := getAccessToken()
if err == nil {
msg = "Already initialized."
} else {
var answer string
fmt.Print("What's your GitHub personal access token? ")
fmt.Scanln(&accessToken)
if len(accessToken) >= 1 {
fmt.Printf(`
The following command will be executed.
$ git config --global github-nippou.token %s
`, accessToken)
fmt.Print("Are you sure? [y/n] ")
fmt.Scanln(&answer)
if string(answer[0]) != "y" {
return errors.New("Canceled")
}
cmd := exec.Command("git", "config", "--global", "github-nippou.token", accessToken)
if err := cmd.Run(); err != nil {
return err
}
msg = "Thanks!"
}
}
fmt.Printf(`%s You can get it with the following command.
$ git config --global github-nippou.token
`, msg)
scopes, err := getClientScopes(ctx, getClient(ctx, accessToken))
if err != nil {
return err
}
if !isValidScopes(scopes) {
return errors.New(`!!!! ` + "`repo`" + ` and ` + "`gist`" + ` scopes are required. !!!!
You need personal access token which has ` + "`repo`" + ` and ` + "`gist`" + `
scopes. Please add these scopes to your personal access
token, visit https://github.com/settings/tokens
`)
}
return nil
}
func isValidScopes(scopes []string) bool {
var found1, found2 bool
for _, v := range scopes {
if v == "repo" {
found1 = true
}
if v == "gist" {
found2 = true
}
}
return found1 && found2
}
func createAndSetGist(ctx context.Context) error {
fmt.Print(`
== [Step: 3/3] Gist (optional) ==
`)
var msg string
if len(getGistID()) >= 1 {
msg = "Already initialized."
} else {
var answer string
fmt.Printf(`1. Create a gist with the content of %s
2. The following command will be executed
$ git config --global github-nippou.settings-gist-id <created gist id>
`, getDefaultSettingsURL())
fmt.Print("Are you sure? [y/n] ")
fmt.Scanln(&answer)
if string(answer[0]) != "y" {
return errors.New("Canceled")
}
accessToken, err := getAccessToken()
if err != nil {
return err
}
gist, _, err := createGist(ctx, getClient(ctx, accessToken))
if err != nil {
return err
}
cmd := exec.Command("git", "config", "--global", "github-nippou.settings-gist-id", *gist.ID)
if err := cmd.Run(); err != nil {
return err
}
msg = "Thanks!"
}
fmt.Printf(`%s You can get it with the following command.
$ git config --global github-nippou.settings-gist-id
And you can easily open the gist URL with web browser.
$ github-nippou open-settings
`, msg)
return nil
}