Skip to content

Commit e85b265

Browse files
authored
Continue on lint errors (#273)
Signed-off-by: Pablo Chacin <pablochacin@gmail.com>
1 parent e4d236f commit e85b265

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

cmd/k6registry/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ func main() {
3030
log.SetFlags(0)
3131
log.Writer()
3232

33-
runCmd(newCmd(os.Args[1:], initLogging()))
33+
err := newCmd(os.Args[1:], initLogging()).Execute()
34+
if err != nil {
35+
slog.Error(formatError(err))
36+
os.Exit(1)
37+
}
3438
}
3539

3640
func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command {
@@ -46,7 +50,4 @@ func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command {
4650
}
4751

4852
func runCmd(cmd *cobra.Command) {
49-
if err := cmd.Execute(); err != nil {
50-
log.Fatal(formatError(err))
51-
}
5253
}

cmd/lint.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const (
2020
complianceCacheTTL = 60 * 60 * 24 * 7
2121

2222
xk6Binary = "xk6"
23+
24+
// xk6 lint returns 2 if some check failed. Other codes mean the lint failed.
25+
lintFailedRC = 2
2326
)
2427

2528
// Check is the result of a particular inspection.
@@ -104,7 +107,6 @@ func checkCompliance(
104107
version string,
105108
official bool,
106109
checks []string,
107-
ignoreLintErrors bool,
108110
cloneURL string,
109111
tstamp int64,
110112
) (*Compliance, error) {
@@ -153,9 +155,10 @@ func checkCompliance(
153155

154156
err = lintCmd.Run()
155157
if err != nil {
156-
slog.Debug("xk6 execution failed", "rc", lintCmd.ProcessState.ExitCode(), "stderr", lintErr.String())
158+
rc := lintCmd.ProcessState.ExitCode()
159+
slog.Debug("xk6 execution failed", "rc", rc, "stderr", lintErr.String())
157160

158-
if !ignoreLintErrors {
161+
if rc != lintFailedRC {
159162
return nil, fmt.Errorf("xk6 lint failed %w", err)
160163
}
161164
}

cmd/load.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"gopkg.in/yaml.v3"
2121
)
2222

23+
var errCompliance = errors.New("compliance check failed")
24+
2325
type loadOptions struct {
2426
lint bool
2527
ignoreLintErrors bool
@@ -73,7 +75,7 @@ func loadSource(in io.Reader) (k6registry.Registry, error) {
7375
return registry, nil
7476
}
7577

76-
func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool, checks []string, ignoreLintErrors bool) error {
78+
func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool, checks []string) error {
7779
if len(ext.Tier) == 0 {
7880
ext.Tier = k6registry.TierCommunity
7981
}
@@ -97,6 +99,7 @@ func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool, checks [
9799
ext.Compliance = make(k6registry.ExtensionCompliance)
98100
}
99101

102+
complianceErrors := []error{}
100103
for _, version := range ext.Versions {
101104
official := ext.Tier == k6registry.TierOfficial
102105

@@ -106,7 +109,6 @@ func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool, checks [
106109
version,
107110
official,
108111
checks,
109-
ignoreLintErrors,
110112
repo.CloneURL,
111113
int64(repo.Timestamp),
112114
)
@@ -122,12 +124,16 @@ func loadOne(ctx context.Context, ext *k6registry.Extension, lint bool, checks [
122124
}
123125
}
124126

127+
if len(issues) > 0 {
128+
complianceErrors = append(complianceErrors, fmt.Errorf("%w %s@%s", errCompliance, ext.Module, version))
129+
}
130+
125131
ext.Compliance[version] = k6registry.Compliance{
126132
Issues: issues,
127133
}
128134
}
129135

130-
return nil
136+
return errors.Join(complianceErrors...)
131137
}
132138

133139
func load(
@@ -140,14 +146,19 @@ func load(
140146
return nil, err
141147
}
142148

149+
compliancedErrors := []error{}
143150
for idx := range registry {
144151
ext := &registry[idx]
145152

146153
slog.Debug("Process extension", "module", ext.Module)
147154

148-
err := loadOne(ctx, ext, opts.lint, opts.lintChecks, opts.ignoreLintErrors)
155+
err := loadOne(ctx, ext, opts.lint, opts.lintChecks)
149156
if err != nil {
150-
return nil, err
157+
if !errors.Is(err, errCompliance) {
158+
return nil, err
159+
}
160+
161+
compliancedErrors = append(compliancedErrors, err)
151162
}
152163

153164
if len(ext.Constraints) > 0 {
@@ -164,7 +175,17 @@ func load(
164175
}
165176
}
166177

167-
return registry, nil
178+
if len(compliancedErrors) == 0 {
179+
return registry, nil
180+
}
181+
182+
slog.Warn(errors.Join(compliancedErrors...).Error())
183+
184+
if opts.ignoreLintErrors {
185+
return registry, nil
186+
}
187+
188+
return registry, errCompliance
168189
}
169190

170191
func loadRepository(ctx context.Context, ext *k6registry.Extension) (*k6registry.Repository, []string, error) {

0 commit comments

Comments
 (0)