forked from imsky/covbadger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
102 lines (86 loc) · 2.4 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
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"os"
"strconv"
"text/template"
)
//Badge represents a coverage badge
type Badge struct {
Coverage int
Color string
}
var colors = map[string]string{
"brightgreen": "#44cc11",
"green": "#97ca00",
"yellow": "#dfb317",
"orange": "#fe7d37",
"red": "#e05d44",
}
var _badgeTemplate string = `<svg xmlns="http://www.w3.org/2000/svg" width="96" height="20">
<title>{{.Coverage}}</title>
<desc>Generated with covbadger (https://github.com/imsky/covbadger)</desc>
<linearGradient id="smooth" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1" />
<stop offset="1" stop-opacity=".1" />
</linearGradient>
<rect rx="3" width="96" height="20" fill="#555" />
<rect rx="3" x="60" width="36" height="20" fill="{{.Color}}" />
<rect x="60" width="4" height="20" fill="{{.Color}}" />
<rect rx="3" width="96" height="20" fill="url(#smooth)" />
<g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,sans-serif" font-size="11">
<text x="30" y="15" fill="#010101" fill-opacity=".3">coverage</text>
<text x="30" y="14">coverage</text>
<text x="78" y="15" fill="#010101" fill-opacity=".3">{{.Coverage}}%</text>
<text x="78" y="14">{{.Coverage}}%</text>
</g>
</svg>`
func RenderBadge(coverage int) (string, error) {
if coverage < 0 || coverage > 100 {
return "", errors.New("Invalid coverage: " + strconv.Itoa(coverage))
}
var buffer bytes.Buffer
badgeTemplate, _ := template.New("badge").Parse(_badgeTemplate)
color := colors["red"]
if coverage > 95 {
color = colors["brightgreen"]
} else if coverage > 80 {
color = colors["green"]
} else if coverage > 60 {
color = colors["yellow"]
} else if coverage > 40 {
color = colors["orange"]
}
_ = badgeTemplate.Execute(&buffer, &Badge{coverage, color})
return buffer.String(), nil
}
func Run(args []string) {
if len(args) != 1 {
flag.Usage()
return
}
coverage := args[0]
if coverage == "-" {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
coverage = scanner.Text()
}
coverageValue, _ := strconv.ParseFloat(coverage, 64)
badge, err := RenderBadge(int(coverageValue))
if err != nil {
panic(err)
} else {
fmt.Println(badge)
}
}
func main() {
flag.Usage = func() {
fmt.Println(`Usage: covbadger [coverage]`)
}
flag.Parse()
Run(flag.Args())
}