Skip to content

Commit 6089f63

Browse files
committed
specerror: Add SplitLevel helper
This doesn't save much time for folks who are iterating over the removed errors (e.g. to print warnings about them), but Aleksa asked for it [1] and Ma suggested the helper struct [2]. I haven't used it in runtimetest. I'm waiting for the in-flight 7aa3987 (cmd/runtimetest/main: Use TAP diagnostics for errors, 2017-07-28, #439) to settle, since that commit cleans up some of the runtimetest error-processing code with: validations := defaultValidations if platform == "linux" { validations = append(validations, linuxValidations...) } and a single loop over the constructed validations array. [1]: #492 (comment) [2]: #492 (comment) Signed-off-by: W. Trevor King <[email protected]>
1 parent a9dbd7e commit 6089f63

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

cmd/oci-runtime-tool/validate.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/hashicorp/go-multierror"
76
rfc2119 "github.com/opencontainers/runtime-tools/error"
87
"github.com/opencontainers/runtime-tools/specerror"
98
"github.com/opencontainers/runtime-tools/validate"
@@ -37,20 +36,14 @@ var bundleValidateCommand = cli.Command{
3736
}
3837

3938
if err := v.CheckAll(); err != nil {
40-
merr, ok := err.(*multierror.Error)
41-
if !ok {
39+
levelErrors, err := specerror.SplitLevel(err, complianceLevel)
40+
if err != nil {
4241
return err
4342
}
44-
var validationErrors error
45-
for _, err = range merr.Errors {
46-
e, ok := err.(*specerror.Error)
47-
if ok && e.Err.Level < complianceLevel {
48-
logrus.Warn(e)
49-
continue
50-
}
51-
validationErrors = multierror.Append(validationErrors, err)
43+
for _, e := range levelErrors.Warnings {
44+
logrus.Warn(e)
5245
}
53-
return validationErrors
46+
return levelErrors.Error
5447
}
5548
fmt.Println("Bundle validation succeeded.")
5649
return nil

specerror/error.go

+30
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ type Error struct {
6969
Code Code
7070
}
7171

72+
// LevelErrors represents Errors filtered into fatal and warnings.
73+
type LevelErrors struct {
74+
// Warnings holds Errors that were below a compliance-level threshold.
75+
Warnings []*Error
76+
77+
// Error holds errors that were at or above a compliance-level
78+
// threshold, as well as errors that are not Errors.
79+
Error *multierror.Error
80+
}
81+
7282
var (
7383
containerFormatRef = func(version string) (reference string, err error) {
7484
return fmt.Sprintf(referenceTemplate, version, "bundle.md#container-format"), nil
@@ -168,3 +178,23 @@ func FindError(err error, code Code) Code {
168178
}
169179
return NonRFCError
170180
}
181+
182+
// SplitLevel removes RFC 2119 errors with a level less than 'level'
183+
// from the source error. If the source error is not a multierror, it
184+
// is returned unchanged.
185+
func SplitLevel(errIn error, level rfc2119.Level) (levelErrors LevelErrors, errOut error) {
186+
merr, ok := errIn.(*multierror.Error)
187+
if !ok {
188+
return levelErrors, errIn
189+
}
190+
for _, err := range merr.Errors {
191+
e, ok := err.(*Error)
192+
if ok && e.Err.Level < level {
193+
fmt.Println(e)
194+
levelErrors.Warnings = append(levelErrors.Warnings, e)
195+
continue
196+
}
197+
levelErrors.Error = multierror.Append(levelErrors.Error, err)
198+
}
199+
return levelErrors, nil
200+
}

0 commit comments

Comments
 (0)