-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
116 lines (91 loc) · 2.37 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
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/alexflint/go-arg"
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage"
)
// Version value is injected at build time
//
//nolint:gochecknoglobals // must be global var
var Version string
//nolint:gochecknoinits // relax
func init() {
if Version == "" {
Version = "unknown-" + strconv.Itoa(int(time.Now().Unix()))
}
}
type args struct {
ConfigPath string `arg:"-c,--config"`
Profile string `arg:"-p,--profile" help:"path to coverage profile"`
LocalPrefix string `arg:"-l,--local-prefix"`
GithubActionOutput bool `arg:"-o,--github-action-output"`
ThresholdFile int `arg:"-f,--threshold-file"`
ThresholdPackage int `arg:"-k,--threshold-package"`
ThresholdTotal int `arg:"-t,--threshold-total"`
}
func (args) Version() string {
return "go-test-coverage " + Version
}
func (a *args) overrideConfig(cfg testcoverage.Config) testcoverage.Config {
if !isMagicString(a.Profile) {
cfg.Profile = a.Profile
}
if a.GithubActionOutput {
cfg.GithubActionOutput = true
}
if !isMagicString(a.LocalPrefix) {
cfg.LocalPrefix = a.LocalPrefix
}
if !isMagicInt(a.ThresholdFile) {
cfg.Threshold.File = a.ThresholdFile
}
if !isMagicInt(a.ThresholdPackage) {
cfg.Threshold.Package = a.ThresholdPackage
}
if !isMagicInt(a.ThresholdPackage) {
cfg.Threshold.Total = a.ThresholdTotal
}
return cfg
}
//nolint:forbidigo // relax
func main() {
cfg, err := readConfig()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
result, err := testcoverage.Check(os.Stdout, cfg)
if err != nil || !result.Pass() {
if err != nil {
fmt.Println(err.Error())
}
os.Exit(1)
}
}
func readConfig() (testcoverage.Config, error) {
cmdArgs := args{}
arg.MustParse(&cmdArgs)
cfg := testcoverage.Config{}
// Load config from file
if !isMagicString(cmdArgs.ConfigPath) {
err := testcoverage.ConfigFromFile(&cfg, cmdArgs.ConfigPath)
if err != nil {
return testcoverage.Config{}, fmt.Errorf("failed loading config from file: %w", err)
}
}
// Override config with values from args
cfg = cmdArgs.overrideConfig(cfg)
if err := cfg.Validate(); err != nil {
return testcoverage.Config{}, fmt.Errorf("config file is not valid: %w", err)
}
return cfg, nil
}
func isMagicString(v string) bool {
return v == `''`
}
func isMagicInt(v int) bool {
return v == -1
}