forked from vladopajic/go-test-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (158 loc) · 4.87 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
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/alexflint/go-arg"
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage"
)
const (
Version = "v2.8.2"
Name = "go-test-coverage"
)
const (
// default value of string variables passed by CI
ciDefaultString = `''`
// default value of int variables passed by CI
ciDefaultnt = -1
)
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"`
BadgeFileName string `arg:"-b,--badge-file-name"`
CDNKey string `arg:"--cdn-key"`
CDNSecret string `arg:"--cdn-secret"`
CDNRegion string `arg:"--cdn-region"`
CDNEndpoint string `arg:"--cdn-endpoint"`
CDNFileName string `arg:"--cdn-file-name"`
CDNBucketName string `arg:"--cdn-bucket-name"`
CDNForcePathStyle bool `arg:"--cdn-force-path-style"`
GitToken string `arg:"--git-token"`
GitRepository string `arg:"--git-repository"`
GitBranch string `arg:"--git-branch"`
GitFileName string `arg:"--git-file-name"`
}
func newArgs() args {
return args{
ConfigPath: ciDefaultString,
Profile: ciDefaultString,
LocalPrefix: ciDefaultString,
GithubActionOutput: false,
ThresholdFile: ciDefaultnt,
ThresholdPackage: ciDefaultnt,
ThresholdTotal: ciDefaultnt,
// Badge
BadgeFileName: ciDefaultString,
// CDN
CDNKey: ciDefaultString,
CDNSecret: ciDefaultString,
CDNRegion: ciDefaultString,
CDNEndpoint: ciDefaultString,
CDNFileName: ciDefaultString,
CDNBucketName: ciDefaultString,
CDNForcePathStyle: false,
// Git
GitToken: ciDefaultString,
GitRepository: ciDefaultString,
GitBranch: ciDefaultString,
GitFileName: ciDefaultString,
}
}
func (args) Version() string {
return Name + " " + Version
}
//nolint:cyclop,maintidx,gomnd,goerr113 // relax
func (a *args) overrideConfig(cfg testcoverage.Config) (testcoverage.Config, error) {
if !isCIDefaultString(a.Profile) {
cfg.Profile = a.Profile
}
if a.GithubActionOutput {
cfg.GithubActionOutput = true
}
if !isCIDefaultString(a.LocalPrefix) {
cfg.LocalPrefix = a.LocalPrefix
}
if !isCIDefaultnt(a.ThresholdFile) {
cfg.Threshold.File = a.ThresholdFile
}
if !isCIDefaultnt(a.ThresholdPackage) {
cfg.Threshold.Package = a.ThresholdPackage
}
if !isCIDefaultnt(a.ThresholdPackage) {
cfg.Threshold.Total = a.ThresholdTotal
}
if !isCIDefaultString(a.BadgeFileName) {
cfg.Badge.FileName = a.BadgeFileName
}
if !isCIDefaultString(a.CDNSecret) {
cfg.Badge.CDN.Secret = a.CDNSecret
cfg.Badge.CDN.Key = escapeCiDefaultString(a.CDNKey)
cfg.Badge.CDN.Region = escapeCiDefaultString(a.CDNRegion)
cfg.Badge.CDN.FileName = escapeCiDefaultString(a.CDNFileName)
cfg.Badge.CDN.BucketName = escapeCiDefaultString(a.CDNBucketName)
cfg.Badge.CDN.ForcePathStyle = a.CDNForcePathStyle
if !isCIDefaultString(a.CDNEndpoint) {
cfg.Badge.CDN.Endpoint = a.CDNEndpoint
}
}
if !isCIDefaultString(a.GitToken) {
cfg.Badge.Git.Token = a.GitToken
cfg.Badge.Git.Branch = escapeCiDefaultString(a.GitBranch)
cfg.Badge.Git.FileName = escapeCiDefaultString(a.GitFileName)
parts := strings.Split(escapeCiDefaultString(a.GitRepository), "/")
if len(parts) != 2 {
return cfg, errors.New("--git-repository flag should have format {owner}/{repository}")
}
cfg.Badge.Git.Owner = parts[0]
cfg.Badge.Git.Repository = parts[1]
}
return cfg, nil
}
//nolint:forbidigo // relax
func main() {
cfg, err := readConfig()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
pass := testcoverage.Check(os.Stdout, cfg)
if !pass {
os.Exit(1)
}
}
func readConfig() (testcoverage.Config, error) {
cmdArgs := newArgs()
arg.MustParse(&cmdArgs)
cfg := testcoverage.Config{}
// Load config from file
if !isCIDefaultString(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, err := cmdArgs.overrideConfig(cfg)
if err != nil {
return testcoverage.Config{}, fmt.Errorf("argument is not valid: %w", err)
}
// Validate config
if err := cfg.Validate(); err != nil {
return testcoverage.Config{}, fmt.Errorf("config file is not valid: %w", err)
}
return cfg, nil
}
func isCIDefaultString(v string) bool { return v == ciDefaultString }
func isCIDefaultnt(v int) bool { return v == ciDefaultnt }
func escapeCiDefaultString(v string) string {
if v == ciDefaultString {
return ""
}
return v
}