-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
193 lines (170 loc) · 4.85 KB
/
main.go
File metadata and controls
193 lines (170 loc) · 4.85 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/openshift-kni/olm-annotation-lint/pkg/linter"
"github.com/openshift-kni/olm-annotation-lint/pkg/reporter"
"github.com/openshift-kni/olm-annotation-lint/pkg/rules"
"gopkg.in/yaml.v3"
)
var version = "dev"
func splitAndTrim(s string) []string {
parts := strings.Split(s, ",")
for i := range parts {
parts[i] = strings.TrimSpace(parts[i])
}
return parts
}
type stringOrList []string
func (s *stringOrList) UnmarshalYAML(value *yaml.Node) error {
if value.Kind == yaml.ScalarNode {
*s = splitAndTrim(value.Value)
return nil
}
var list []string
if err := value.Decode(&list); err != nil {
return err
}
*s = list
return nil
}
type config struct {
Path stringOrList `yaml:"path"`
Exclude stringOrList `yaml:"exclude"`
Allow stringOrList `yaml:"allow"`
Strict *bool `yaml:"strict"`
}
func loadConfig(path string) (*config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing config %s: %w", path, err)
}
return &cfg, nil
}
func discoverConfig() (*config, error) {
cfg, err := loadConfig(".olm-lint.yaml")
if err == nil {
return cfg, nil
}
if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
return nil, nil
}
func main() {
var (
path string
exclude string
allow string
strict bool
format string
configPath string
showVersion bool
listRules bool
)
flag.StringVar(&path, "path", ".", "Path or comma-separated paths to scan")
flag.StringVar(&path, "p", ".", "Path or comma-separated paths to scan (shorthand)")
flag.StringVar(&exclude, "exclude", "", "Comma-separated paths to exclude")
flag.StringVar(&exclude, "e", "", "Comma-separated paths to exclude (shorthand)")
flag.StringVar(&allow, "allow", "", "Comma-separated annotation keys to allow (bypass unknown annotation errors)")
flag.StringVar(&allow, "a", "", "Comma-separated annotation keys to allow (shorthand)")
flag.BoolVar(&strict, "strict", false, "Treat warnings as errors")
flag.BoolVar(&strict, "s", false, "Treat warnings as errors (shorthand)")
flag.StringVar(&format, "format", "text", "Output format: text, json, github")
flag.StringVar(&format, "f", "text", "Output format: text, json, github (shorthand)")
flag.StringVar(&configPath, "config", "", "Path to config file (default: .olm-lint.yaml in current directory)")
flag.StringVar(&configPath, "c", "", "Path to config file (shorthand)")
flag.BoolVar(&showVersion, "version", false, "Print version and exit")
flag.BoolVar(&showVersion, "v", false, "Print version and exit (shorthand)")
flag.BoolVar(&listRules, "list-rules", false, "List all known OLM annotations and exit")
flag.BoolVar(&listRules, "l", false, "List all known OLM annotations and exit (shorthand)")
flag.Parse()
if showVersion {
fmt.Println(version)
return
}
if listRules {
rules.PrintRules(os.Stdout)
return
}
setFlags := map[string]bool{}
flag.Visit(func(f *flag.Flag) { setFlags[f.Name] = true })
var cfg *config
var err error
if configPath != "" {
cfg, err = loadConfig(configPath)
} else {
cfg, err = discoverConfig()
}
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(2)
}
paths := splitAndTrim(path)
var excludePaths []string
if exclude != "" {
excludePaths = splitAndTrim(exclude)
}
var allowedAnnotations []string
if allow != "" {
allowedAnnotations = splitAndTrim(allow)
}
if cfg != nil {
if !setFlags["path"] && !setFlags["p"] && len(cfg.Path) > 0 {
paths = cfg.Path
}
if !setFlags["exclude"] && !setFlags["e"] && len(cfg.Exclude) > 0 {
excludePaths = cfg.Exclude
}
if !setFlags["allow"] && !setFlags["a"] && len(cfg.Allow) > 0 {
allowedAnnotations = cfg.Allow
}
if !setFlags["strict"] && !setFlags["s"] && cfg.Strict != nil {
strict = *cfg.Strict
}
}
violations, err := linter.Run(linter.Options{
Paths: paths,
Exclude: excludePaths,
AllowedAnnotations: allowedAnnotations,
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(2)
}
var outputFormat reporter.Format
switch format {
case "json":
outputFormat = reporter.FormatJSON
case "github":
outputFormat = reporter.FormatGitHub
default:
outputFormat = reporter.FormatText
}
if len(violations) > 0 {
reporter.Report(os.Stdout, violations, outputFormat)
}
var errorCount, warningCount int
for _, v := range violations {
switch v.Severity {
case rules.SeverityError:
errorCount++
case rules.SeverityWarning:
warningCount++
}
}
if reporter.HasErrors(violations, strict) {
_, _ = fmt.Fprintf(os.Stderr, "\nFound %d error(s) and %d warning(s)\n", errorCount, warningCount)
os.Exit(1)
}
if warningCount > 0 {
_, _ = fmt.Fprintf(os.Stderr, "\nFound %d warning(s)\n", warningCount)
}
}