forked from chai2010/advanced-go-programming-book
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_contributors.go
69 lines (58 loc) · 1.47 KB
/
gen_contributors.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
// Copyright 2017 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// https://api.github.com/repos/{user}/{repo}/contributors
// https://api.github.com/repos/chai2010/advanced-go-programming-book/contributors
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
)
var (
flagFile = flag.String("file", "contributors.json", "set contributors file")
)
type User struct {
Login string `json:"login"`
AvatarUrl string `json:"avatar_url"`
}
func main() {
flag.Parse()
content, err := ioutil.ReadFile(*flagFile)
if err != nil {
log.Fatal(err)
}
var users []User
if err := json.Unmarshal(content, &users); err != nil {
log.Fatal(err)
}
// skip chai2010 and cch123
fmt.Println(genTableHeader(users[2:]))
fmt.Println(genTableHeaderSepLine())
fmt.Println(genTableElemLines(users[2:]))
}
func genTableHeader(users []User) string {
var s = "|"
for i := 0; i < len(users) && i < 7; i++ {
s += fmt.Sprintf(
` [<img src="%s" width="100px;"/><br /><sub><b>%s</b></sub>](https://github.com/%s) |`,
users[i].AvatarUrl,
users[i].Login,
users[i].Login,
)
}
return s
}
func genTableHeaderSepLine() string {
return "| :---: | :---: | :---: | :---: | :---: | :---: | :---: |"
}
func genTableElemLines(users []User) string {
var s string
for i := 7; i < len(users); i += 7 {
s += genTableHeader(users[i:]) + "\n"
}
return s
}