-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
246 lines (197 loc) · 5.07 KB
/
repos.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
type Repository struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
URL string `json:"html_url"`
IsPrivate bool `json:"private"`
IsFork bool `json:"fork"`
}
const (
GET = "GET"
DELETE = "DELETE"
)
func (v *Repository) String() string {
return fmt.Sprintf("[%s] - %s", v.FullName, v.Description)
}
func FetchAndSaveRepositories() {
repos, err := FetchRepositories()
if err != nil {
printError("Error on fetching repos", err)
os.Exit(1)
}
fmt.Println(format(GREEN, "Repositories are fetched successfully"))
err = SaveRepositories(repos)
if err != nil {
printError("Error on saving repos", err)
os.Exit(1)
}
fmt.Println(format(GREEN, "Repositories are saved successfully"))
}
func SaveRepositories(repos []Repository) error {
file, err := os.Create(linksFile)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(repos); err != nil {
return err
}
return nil
}
func FetchRepositories() ([]Repository, error) {
var repositories []Repository
page := 1
for {
repos, err := doFetchRequest(fmt.Sprintf(FetchReposURL, config.UserName), page)
if err != nil {
return nil, err
}
if len(repos) == 0 {
break
}
repositories = append(repositories, onlyForks(repos)...)
page++
}
return repositories, nil
}
func onlyForks(repos []Repository) []Repository {
var forks []Repository
for _, repo := range repos {
if !repo.IsFork {
continue
}
forks = append(forks, repo)
}
return forks
}
func newClient() *http.Client {
return &http.Client{Timeout: 10 * time.Second}
}
func newRequest(method, url string) (*http.Request, error) {
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", RootURL, url), nil)
if err != nil {
return nil, fmt.Errorf("error on creating request: %w", err)
}
req.Header.Set("X-GitHub-Api-Version", APIVersion)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", config.UserName)
req.Header.Set("Authorization", fmt.Sprintf("token %s", config.AccessToken))
return req, nil
}
func doFetchRequest(url string, page int) ([]Repository, error) {
client := newClient()
req, err := newRequest(GET, url)
if err != nil {
return nil, fmt.Errorf("doFetchRequest: %w", err)
}
q := req.URL.Query()
q.Add("page", fmt.Sprintf("%d", page))
req.URL.RawQuery = q.Encode()
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error on making request: %w", err)
}
defer response.Body.Close()
var repos []Repository
err = json.NewDecoder(response.Body).Decode(&repos)
if err != nil {
return nil, err
}
return repos, nil
}
func deleteRepositories(repos []Repository) error {
_ = repos
for _, repo := range repos {
url := fmt.Sprintf(DeleteRepoURL, repo.FullName)
err := doDeleteRequest(url)
if err != nil {
fmt.Printf("Remove %s error: %v", repo.FullName, err.Error())
}
}
return nil
}
func listRepositories() ([]Repository, error) {
file, err := os.Open(linksFile)
if err != nil {
return nil, fmt.Errorf("listRepositories: %w", err)
}
var result []Repository
err = json.NewDecoder(file).Decode(&result)
if err != nil {
return nil, fmt.Errorf("error parsing JSON: %w", err)
}
return result, nil
}
func doDeleteRequest(url string) error {
client := newClient()
req, err := newRequest(DELETE, url)
if err != nil {
return fmt.Errorf("doDeleteRequest: %w", err)
}
response, err := client.Do(req)
if err != nil {
return fmt.Errorf("error on making request: %w", err)
}
defer response.Body.Close()
return nil
}
func removeCmdAll(repos []Repository) {
fmt.Println("The following repos will be deleted:")
for _, repo := range repos {
fmt.Printf("%s\n", repo.String())
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("Are you sure you want to delete the following repos? y/n/q: ")
input, _ := reader.ReadString('\n')
if strings.TrimSpace(input) == "y" {
err := deleteRepositories(repos)
if err != nil {
printError("On delete repositories", err)
os.Exit(1)
}
fmt.Printf("Repositories deleted successfully")
} else {
fmt.Printf("Cancelled")
}
}
func removeWithCheck(repos []Repository) {
reader := bufio.NewReader(os.Stdin)
var reposForDelete []Repository
outOfLoop:
for _, repo := range repos {
fmt.Printf("Delete repository '%s - %s'? (y/n/q/s): ",
repo.FullName, repo.Description)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
switch strings.ToLower(input) {
case "y":
reposForDelete = append(reposForDelete, repo)
fmt.Println("Added repository for deletion. It will happen after you check ALL of them")
case "n":
fmt.Println("Skipping...")
case "s":
fmt.Println("Skipping all next repositories...")
break outOfLoop
case "q":
return
}
}
err := deleteRepositories(reposForDelete)
if err != nil {
printError("On delete repositories", err)
os.Exit(1)
}
fmt.Printf("Repositories deleted successfully")
}