-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
286 lines (240 loc) · 6 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"sort"
"strings"
"sync"
"time"
"github.com/briandowns/spinner"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/fatih/color"
"github.com/google/go-github/v61/github"
"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
"github.com/yanskun/pflag"
"golang.org/x/text/message"
)
func main() {
var filterVal float64
var helpFlag bool
pflag.BoolVarP(&helpFlag, "help", "h", false, "Show help for command")
pflag.Float64VarP(&filterVal, "filter", "f", 1.0, "Filter past by float value in years (e.g. 0.5)")
pflag.Parse()
if helpFlag {
printHelp()
return
}
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Start()
account := pflag.Arg(0)
if account == "" {
account, _ = getGitHubUsername()
}
client, err := api.DefaultRESTClient()
if err != nil {
return
}
repos, err := getRepositories(client, account)
if err != nil {
return
}
var filter time.Time
if filterVal != 0.0 {
filter = computeFilter(filterVal)
repos = filterRepositories(repos, filter)
}
results, err := getLanguages(client, repos)
if err != nil {
return
}
languages := sumLanguages(results)
s.Stop()
printTable(languages)
fmt.Printf("https:github.com/%s has %d repositories\n", account, len(repos))
if filterVal != 0.0 {
fmt.Printf("Last updated after %s\n", filter.Format("2006-01-02"))
}
}
func printHelp() {
b := color.New(color.Bold)
b.Println("\nUSAGE")
fmt.Println(" gh langs <command> [options]\n")
b.Println("COMMANDS")
fmt.Println(" account: Get languages used by a GitHub user or organization\n")
b.Println("OPTIONS")
pflag.Usage()
return
}
func computeFilter(filterVal float64) time.Time {
var filter time.Time
totalDays := int(filterVal * 365)
years := -totalDays / 365
remainingDays := totalDays % 365
months := -remainingDays / 30
days := -remainingDays % 30
filter = time.Now().AddDate(years, months, days)
return filter
}
func getGitHubUsername() (string, error) {
cmd := exec.Command("gh", "api", "user", "--jq", ".login")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
return strings.TrimSpace(out.String()), nil
}
func findAccountType(client *api.RESTClient, account string) (string, error) {
response, err := client.Request(http.MethodGet, fmt.Sprintf("users/%s", account), nil)
if err != nil {
return "", err
}
decoder := json.NewDecoder(response.Body)
data := github.User{}
if err := decoder.Decode(&data); err != nil {
return "", err
}
if err := response.Body.Close(); err != nil {
return "", err
}
if data.GetType() == "User" {
return "users", nil
} else if data.GetType() == "Organization" {
return "orgs", nil
}
return "", fmt.Errorf("Unknown account type: %s", data.GetType())
}
func getRepositories(client *api.RESTClient, account string) ([]github.Repository, error) {
var repos []github.Repository
page := 1
t, err := findAccountType(client, account)
if err != nil {
return nil, err
}
endpoint := fmt.Sprintf("%s/%s/repos", t, account)
for {
url := fmt.Sprintf("%s?per_page=100&page=%d", endpoint, page)
response, err := client.Request(http.MethodGet, url, nil)
if err != nil {
fmt.Printf("%s is not a valid GitHub username\n", account)
return nil, err
}
decoder := json.NewDecoder(response.Body)
data := []github.Repository{}
if err := decoder.Decode(&data); err != nil {
return nil, err
}
if err := response.Body.Close(); err != nil {
return nil, err
}
if len(data) == 0 {
break
}
repos = append(repos, data...)
page++
}
return repos, nil
}
func filterRepositories(repos []github.Repository, filter time.Time) []github.Repository {
var results []github.Repository
for _, repo := range repos {
if repo.GetUpdatedAt().After(filter) {
results = append(results, repo)
}
}
return results
}
type (
Languages map[string]int
LanguagesList []Languages
)
func getLanguages(client *api.RESTClient, data []github.Repository) (LanguagesList, error) {
results := make(LanguagesList, 0, len(data))
var wg sync.WaitGroup
for _, repo := range data {
wg.Add(1)
go func(repo github.Repository) {
defer wg.Done()
fullName := repo.GetFullName()
response, err := client.Request(http.MethodGet, fmt.Sprintf("repos/%s/languages", fullName), nil)
if err != nil {
log.Fatal(err)
return
}
decoder := json.NewDecoder(response.Body)
data := Languages{}
if err := decoder.Decode(&data); err != nil {
log.Fatal(err)
return
}
if err := response.Body.Close(); err != nil {
log.Fatal(err)
return
}
results = append(results, data)
}(repo)
}
wg.Wait()
return results, nil
}
func sumLanguages(list LanguagesList) Languages {
results := Languages{}
for _, languages := range list {
for lang, lines := range languages {
results[lang] += lines
}
}
return results
}
type Pair struct {
Key string
Value int
}
func printTable(languages Languages) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
p := message.NewPrinter(message.MatchLanguage("en"))
t.AppendHeader(table.Row{"Language", "Lines"})
// Convert map to slice
pairs := make([]Pair, 0, len(languages))
for k, v := range languages {
pairs = append(pairs, Pair{k, v})
}
// Sort slice in descending order by Value
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].Value > pairs[j].Value
})
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "Language",
Align: text.AlignLeft,
},
{
Name: "Lines",
Align: text.AlignRight,
Transformer: func(val interface{}) string {
return p.Sprintf("%d", val)
},
TransformerFooter: func(val interface{}) string {
return p.Sprintf("%d", val)
},
},
})
// Append rows in sorted order
sumLines := 0
for _, pair := range pairs {
sumLines += pair.Value
t.AppendRow(table.Row{pair.Key, pair.Value})
}
t.AppendFooter(table.Row{"Total", sumLines})
t.Render()
}
// For more examples of using go-gh, see:
// https://github.com/cli/go-gh/blob/trunk/example_gh_test.go