-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·199 lines (179 loc) · 4.57 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
var (
limit string
limitInt int
readmeFile string
instachartUrl string
subtitle string
style string
width string
height string
theme string
altText string
)
type SccStat struct {
Name string `json:"Name"`
Bytes int `json:"Bytes"`
CodeBytes int `json:"CodeBytes"`
Lines int `json:"Lines"`
Code int `json:"Code"`
Comment int `json:"Comment"`
Blank int `json:"Blank"`
Complexity int `json:"Complexity"`
Count int `json:"Count"`
WeightedComplexity int `json:"WeightedComplexity"`
Files []string `json:"Files"`
}
type BarData struct {
X []string `json:"x"`
Y [][]int `json:"y"`
Names []string `json:"names"`
}
func main() {
flags()
stats := readStatsFromPipe()
barChartURL := createBarChartURL(stats, limitInt)
markdownImgeURL := "![" + altText + "](" + barChartURL + ")"
newOrReplaceInFile(markdownImgeURL, readmeFile)
fmt.Println(markdownImgeURL)
}
func newOrReplaceInFile(markdownImgeURL string, filename string) {
file, err := os.Open(filename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
content, _ := io.ReadAll(file)
found := false
lines := strings.Split(string(content), "\n")
for i, line := range lines {
if strings.Contains(line, "!["+altText+"]") {
found = true
lines[i] = markdownImgeURL
}
}
if found {
newContent := []byte(strings.Join(lines, "\n"))
os.WriteFile(readmeFile, newContent, 0644)
}
if !found {
newContent := []byte(markdownImgeURL + "\n\n" + string(content))
os.WriteFile(readmeFile, newContent, 0644)
}
}
func normalizeName(name string) string {
name = strings.ReplaceAll(name, "/", "-")
name = strings.ReplaceAll(name, " ", "-")
return name
}
func createBarChartURL(scc []SccStat, limit int) string {
var chartURL string
var data BarData
data.Names = []string{"Code", "Comment", "Files", "Complexity"}
for idx, stat := range scc {
if idx >= limit {
break
}
data.X = append(data.X, normalizeName(stat.Name))
}
y := make([][]int, len(data.Names))
for _, x := range data.X {
for _, stat := range scc {
if x == normalizeName(stat.Name) {
y[0] = append(y[0], stat.Code)
y[1] = append(y[1], stat.Comment)
y[2] = append(y[2], stat.Count)
y[3] = append(y[3], stat.Complexity)
}
}
}
data.Y = y
json, _ := json.Marshal(data)
queries := map[string]string{
"title": "SCC+-+Sloc,+Cloc+and+Code",
"width": width,
"height": height,
"subtitle": subtitle,
"metric": "+lines",
"data": string(json),
}
chartURL = instachartUrl + "/" + style + "?" + createQuery(queries)
return chartURL
}
func readStatsFromPipe() []SccStat {
stat, _ := os.Stdin.Stat()
var stats []SccStat
if (stat.Mode() & os.ModeCharDevice) == 0 {
sccJSON, _ := ioutil.ReadAll(os.Stdin)
err := json.Unmarshal([]byte(sccJSON), &stats)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println("No input data")
os.Exit(1)
}
return stats
}
func createQuery(queries map[string]string) string {
var query string
for key, value := range queries {
query += key + "=" + value + "&"
}
// strip last &
query = query[:len(query)-1]
return query
}
func flags() {
flag.StringVar(&limit, "limit", "", "limit of the chart")
flag.StringVar(&readmeFile, "readme-file", "", "readme filename")
flag.StringVar(&instachartUrl, "instachart-url", "", "instachart url")
flag.StringVar(&subtitle, "subtitle", "", "chart subtitle")
flag.StringVar(&altText, "alt-text", "", "alt text for the image")
flag.StringVar(&style, "style", "", "chart style")
flag.StringVar(&width, "width", "", "chart width")
flag.StringVar(&height, "height", "", "chart height")
flag.StringVar(&theme, "theme", "", "chart theme")
flag.Parse()
// as flags can be empty string, and we want to use default values
if instachartUrl == "" {
flag.Set("instachart-url", "https://instachart.coveritup.app")
}
if subtitle == "" {
flag.Set("subtitle", "Source+Code+Counter")
}
if readmeFile == "" {
flag.Set("readme-file", "README.md")
}
if style == "" {
flag.Set("style", "bar")
}
if altText == "" {
flag.Set("alt-text", "action-"+style+"-scc")
}
if width == "" {
flag.Set("width", "960")
}
if height == "" {
flag.Set("height", "700")
}
if theme == "" {
flag.Set("theme", "light")
}
limitInt, _ = strconv.Atoi(limit)
if limitInt <= 0 {
limitInt = 4
}
}