|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "html/template" |
| 8 | + "log" |
| 9 | +) |
| 10 | + |
| 11 | +type Contributor struct { |
| 12 | + Login string `json:"login"` |
| 13 | + Avatar_Url string `json:"avatar_url"` |
| 14 | + Name string `json:"name"` |
| 15 | + Url string `json:"url"` |
| 16 | + Profile string `json:"profile"` |
| 17 | + Contributions int `json:"contributions"` |
| 18 | + ContributionType []string `json:"contribution_type"` |
| 19 | +} |
| 20 | + |
| 21 | +const GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-leetcode/contributors" |
| 22 | +const GITHUB_CONTRIBUTOR_TMPL_PATH = "./tpl/.all-contributorsrc" |
| 23 | + |
| 24 | +func getContributorBufer() []byte { |
| 25 | + |
| 26 | + contributor_buffer := Request("GET", GITHUB_CONTRIBUTOR_API_URL, nil) |
| 27 | + |
| 28 | + return contributor_buffer |
| 29 | +} |
| 30 | + |
| 31 | +func GetContributorString() string { |
| 32 | + str := string(getContributorBufer()) |
| 33 | + return str |
| 34 | +} |
| 35 | + |
| 36 | +// get the josnmarshal json |
| 37 | +func GetContributorJosnMarshal(prefix, indent string) string { |
| 38 | + contributors := []Contributor{} |
| 39 | + |
| 40 | + if err := json.Unmarshal(getContributorBufer(), &contributors); err != nil { |
| 41 | + log.Fatalln(err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + contributors_buffer, err := json.MarshalIndent(contributors, prefix, indent) |
| 45 | + if err != nil { |
| 46 | + log.Fatalln(err.Error()) |
| 47 | + } |
| 48 | + |
| 49 | + return string(contributors_buffer) |
| 50 | +} |
| 51 | + |
| 52 | +func GetContributorInstance() []Contributor { |
| 53 | + contributors := []Contributor{} |
| 54 | + |
| 55 | + if err := json.Unmarshal(getContributorBufer(), &contributors); err != nil { |
| 56 | + log.Fatalln(err.Error()) |
| 57 | + } |
| 58 | + return contributors |
| 59 | +} |
| 60 | +func getContributorTemplate() string { |
| 61 | + buffer := ReadFile(GITHUB_CONTRIBUTOR_TMPL_PATH) |
| 62 | + return string(buffer) |
| 63 | +} |
| 64 | + |
| 65 | +func GenerateContributorTemplete() { |
| 66 | + tpl_str := getContributorTemplate() |
| 67 | + //fmt.Println(tpl_str) |
| 68 | + contributors := GetContributorInstance() |
| 69 | + |
| 70 | + fmt.Println(contributors) |
| 71 | + // Tmpl |
| 72 | + var tmpRes bytes.Buffer |
| 73 | + |
| 74 | + tmpl, err := template.New("Contributors: ").Parse(tpl_str) |
| 75 | + if err != nil { |
| 76 | + log.Printf("%s", err) |
| 77 | + } |
| 78 | + err = tmpl.Execute(&tmpRes, contributors) |
| 79 | + fmt.Println(string(tmpRes.Bytes())) |
| 80 | +} |
0 commit comments