-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
160 lines (145 loc) · 4.95 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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/jpoles1/gopherbadger/coverbadge"
"github.com/jpoles1/gopherbadger/logging"
"github.com/fatih/color"
)
const toolCoverCommand = "go tool cover -func=coverage.out"
func getCommandOutput(commandString string, isSilent bool) chan float64 {
cmd := exec.Command("bash", "-c", commandString)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if nil != err {
log.Fatalf("Error obtaining stdout: %s", err.Error())
}
reader := bufio.NewReader(stdout)
coverageFloatChannel := make(chan float64)
go func(reader io.Reader) {
re := regexp.MustCompile("total:\\s*\\(statements\\)?\\s*(\\d+\\.?\\d*)\\s*\\%")
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
lineText := scanner.Text()
match := re.FindStringSubmatch(lineText)
if len(match) == 2 {
if !isSilent {
color.Green(lineText)
}
coverageValue, err := strconv.ParseFloat(match[1], 32)
errCheck("Parsing coverage to float", err)
if err == nil {
coverageFloatChannel <- coverageValue
}
break
} else {
if !isSilent {
fmt.Println(lineText)
}
}
}
if err = cmd.Wait(); nil != err {
log.Fatalf("Error running program: %s, %s", cmd.Path, err)
}
}(reader)
if err := cmd.Start(); nil != err {
log.Fatalf("Error running program: %s, %s", cmd.Path, err)
}
return coverageFloatChannel
}
func containsString(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
type gopherBadgerConfig struct {
badgeOutputFlag bool
badgeStyleFlag string
updateMdFilesFlag string
coveragePrefixFlag string
coverageCommandFlag string
manualCoverageFlag float64
rootFolderFlag string
tagsFlag string
shortFlag bool
silentFlag bool
}
var badgeStyles = []string{"plastic", "flat", "flat-square", "for-the-badge", "social"}
func main() {
badgeOutputFlag := flag.Bool("png", true, "Boolean to decide if .png will be generated by the software")
badgeStyleFlag := flag.String("style", "flat", "Badge style from list: ["+strings.Join(badgeStyles, ",")+"]")
updateMdFilesFlag := flag.String("md", "", "A list of markdown filepaths for badge updates.")
coveragePrefixFlag := flag.String("prefix", "Go", "A prefix to specify the coverage in your badge.")
coverageCommandFlag := flag.String("covercmd", "", "gocover command to run; must print coverage report to stdout")
manualCoverageFlag := flag.Float64("manualcov", -1.0, "A manually inputted coverage float.")
rootFolderFlag := flag.String("root", ".", "A folder within your project from which to start recursively scanning and testing.")
tagsFlag := flag.String("tags", "", "The build tests you'd like to include in your coverage")
shortFlag := flag.Bool("short", false, "It will skip tests marked as testing.Short()")
silentFlag := flag.Bool("silent", false, "Do not output anything unless errors are encountered")
flag.Parse()
config := gopherBadgerConfig{
badgeOutputFlag: *badgeOutputFlag,
badgeStyleFlag: *badgeStyleFlag,
updateMdFilesFlag: *updateMdFilesFlag,
coveragePrefixFlag: *coveragePrefixFlag,
coverageCommandFlag: *coverageCommandFlag,
manualCoverageFlag: *manualCoverageFlag,
rootFolderFlag: *rootFolderFlag,
tagsFlag: *tagsFlag,
shortFlag: *shortFlag,
silentFlag: *silentFlag,
}
badger(config)
}
func badger(config gopherBadgerConfig) {
if !containsString(badgeStyles, config.badgeStyleFlag) {
logging.Fatal("Invalid style flag! Must be a member of list: ["+strings.Join(badgeStyles, ", ")+"]", errors.New("invalid style flag"))
}
coverageBadge := coverbadge.Badge{
CoveragePrefix: config.coveragePrefixFlag,
Style: config.badgeStyleFlag,
ImageExtension: ".png",
}
var coverageFloat float64
coverageCommand := ""
if config.coverageCommandFlag != "" {
coverageCommand = config.coverageCommandFlag
if config.tagsFlag != "" || config.shortFlag {
log.Println("Warning: When the covercmd flag is used the -tags and -short flags will be ignored.")
}
} else {
flagsCommands := ""
if config.tagsFlag != "" {
flagsCommands = flagsCommands + " -tags \"" + config.tagsFlag + "\""
}
if config.shortFlag {
flagsCommands = flagsCommands + " -short"
}
coverageCommand = fmt.Sprintf("go test %s/... -coverprofile=coverage.out %s && %s", config.rootFolderFlag, flagsCommands, toolCoverCommand)
}
if config.manualCoverageFlag == -1 {
coverageFloat = <-getCommandOutput(coverageCommand, config.silentFlag)
} else {
coverageFloat = config.manualCoverageFlag
}
if config.badgeOutputFlag {
coverageBadge.DownloadBadge("coverage_badge.png", coverageFloat)
}
if config.updateMdFilesFlag != "" {
for _, filepath := range strings.Split(config.updateMdFilesFlag, ",") {
coverageBadge.WriteBadgeToMd(filepath, coverageFloat, config.silentFlag)
}
}
}