forked from ozh/github-colors
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgithub-colors.go
152 lines (133 loc) · 3.55 KB
/
github-colors.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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"io"
"strings"
"sort"
)
type Lang struct {
//color hex code, language url
color, url string
}
func main() {
m := GetGithubColors()
writeToJson(m)
writeToReadme(m)
defer fmt.Println("ByeBye")
}
//use gobind to call this in other language(like java)
func GetGithubColors() map[string]Lang {
m := readFile()
langMap := make(map[string]Lang)
fmt.Printf("Find %v languages in Github\n", len(m))
for name, attrs := range m {
//fmt.Printf("%s: %v \n", name, attrs)
attrsMap, ok := attrs.(map[interface{}]interface{})
color, okk := attrsMap["color"]
stringColor := fmt.Sprintf("%s", color)
//remove space from name
newName := strings.Replace(name, " ", "-", -1)
if okk && ok {
langMap[newName] = Lang{stringColor, fmt.Sprintf("https://github.com/trending?l=%s", newName)}
} else {
langMap[newName] = Lang{"", fmt.Sprintf("https://github.com/trending?l=%s", newName)}
}
}
return langMap
}
func sliceOfKeys(m map[string]Lang) []string {
keys := make([]string, 0)
for k := range m {
keys = append(keys, k)
}
return keys
}
func downloadFile(filePath string, url string) (err error) {
// Create the file
out, err := os.Create(filePath)
checkErr(err)
defer out.Close()
// Get the data
resp, err := http.Get(url)
checkErr(err)
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
checkErr(err)
return nil
}
func readFile() map[string]interface{} {
//check exist. if not, create the file
if _, err := os.Stat("language.yml"); os.IsNotExist(err) {
fmt.Println("start downloding...")
downloadFile("language.yml", "https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml")
}
m := make(map[string]interface{})
ymlBytes, err := ioutil.ReadFile("language.yml")
checkErr(err)
err = yaml.Unmarshal(ymlBytes, m)
checkErr(err)
return m
}
func checkErr(err error) (hasErr bool) {
hasErr = err == nil
if err != nil {
fmt.Println(err)
}
return hasErr
}
func writeToJson(m map[string]Lang) {
fmt.Println("Write into color.json...")
colorMap := make(map[string]string)
for name, lang := range m {
colorMap[name] = lang.color
}
jsonBytes, err := json.Marshal(colorMap)
checkErr(err)
ioutil.WriteFile("color.json", jsonBytes, 0644)
}
func writeToReadme(m map[string]Lang) {
fmt.Println("Write into README.md...")
var b []byte
s := "# Colors of programming languages on GitHub\n\n"
s += "> Thanks [golang](golang.org) and [ymal](https://github.com/go-yaml/yaml).\n\n"
colorless := make(map[string]Lang)
//get slice of keys in map
keys := sliceOfKeys(m)
//sort keys
sort.Strings(keys)
//write color pic to file
for _, name := range keys {
lang := m[name]
if lang.color != "" {
//replace space -> -;remove #
b = []byte(fmt.Sprintf("[](%s)", lang.color[1:], name, lang.url))
s += string(b)
} else {
colorless[name] = lang
}
}
fmt.Printf("And %v languages are have no color\n", len(colorless))
//write cloerless
s += "\n\nA few(lot) other languages don't have their own color on GitHub :( \n\n"
keys = sliceOfKeys(colorless)
sort.Strings(keys)
for _, name := range keys {
lang := colorless[name]
b = []byte(fmt.Sprintf("- [%s](%s)\n", name, lang.url))
s += string(b)
}
/*
for name, lang := range colorless {
b = []byte(fmt.Sprintf("- [%s](%s)\n", name, lang.url))
s += string(b)
}*/
s += "\n\nCurious about all this? Check `ABOUT.md`.\n"
outByte := []byte(s)
ioutil.WriteFile("README.md", outByte, 0644)
}