@@ -4,32 +4,51 @@ import (
44 "errors"
55 "fmt"
66 "strings"
7+
8+ rspec "github.com/opencontainers/runtime-spec/specs-go"
79)
810
911// ComplianceLevel represents the OCI compliance levels
1012type ComplianceLevel int
1113
1214const (
1315 // MAY-level
16+
17+ // ComplianceMay represents 'MAY' in RFC2119
1418 ComplianceMay ComplianceLevel = iota
19+ // ComplianceOptional represents 'OPTIONAL' in RFC2119
1520 ComplianceOptional
21+
1622 // SHOULD-level
23+
24+ // ComplianceShould represents 'SHOULD' in RFC2119
1725 ComplianceShould
26+ // ComplianceShouldNot represents 'SHOULD NOT' in RFC2119
1827 ComplianceShouldNot
28+ // ComplianceRecommended represents 'RECOMMENDED' in RFC2119
1929 ComplianceRecommended
30+ // ComplianceNotRecommended represents 'NOT RECOMMENDED' in RFC2119
2031 ComplianceNotRecommended
32+
2133 // MUST-level
34+
35+ // ComplianceMust represents 'MUST' in RFC2119
2236 ComplianceMust
37+ // ComplianceMustNot represents 'MUST NOT' in RFC2119
2338 ComplianceMustNot
39+ // ComplianceShall represents 'SHALL' in RFC2119
2440 ComplianceShall
41+ // ComplianceShallNot represents 'SHALL NOT' in RFC2119
2542 ComplianceShallNot
43+ // ComplianceRequired represents 'REQUIRED' in RFC2119
2644 ComplianceRequired
2745)
2846
2947// ErrorCode represents the compliance content
3048type ErrorCode int
3149
3250const (
51+ // DefaultFilesystems represents the error code of default filesystems test
3352 DefaultFilesystems ErrorCode = iota
3453)
3554
@@ -40,28 +59,27 @@ type Error struct {
4059 Err error
4160}
4261
43- //FIXME: change to tagged spec releases
44- const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/"
62+ const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob"
4563
4664var ociErrors = map [ErrorCode ]Error {
4765 DefaultFilesystems : Error {Level : ComplianceShould , Reference : "config-linux.md#default-filesystems" },
4866}
4967
5068// ParseLevel takes a string level and returns the OCI compliance level constant
51- func ParseLevel (level string ) ComplianceLevel {
69+ func ParseLevel (level string ) ( ComplianceLevel , error ) {
5270 switch strings .ToUpper (level ) {
5371 case "MAY" :
5472 fallthrough
5573 case "OPTIONAL" :
56- return ComplianceMay
74+ return ComplianceMay , nil
5775 case "SHOULD" :
5876 fallthrough
5977 case "SHOULDNOT" :
6078 fallthrough
6179 case "RECOMMENDED" :
6280 fallthrough
6381 case "NOTRECOMMENDED" :
64- return ComplianceShould
82+ return ComplianceShould , nil
6583 case "MUST" :
6684 fallthrough
6785 case "MUSTNOT" :
@@ -71,10 +89,11 @@ func ParseLevel(level string) ComplianceLevel {
7189 case "SHALLNOT" :
7290 fallthrough
7391 case "REQUIRED" :
74- return ComplianceMust
75- default :
76- return ComplianceMust
92+ return ComplianceMust , nil
7793 }
94+
95+ var l ComplianceLevel
96+ return l , fmt .Errorf ("%q is not a valid compliance level" , level )
7897}
7998
8099// NewError creates an Error by ErrorCode and message
@@ -87,5 +106,5 @@ func NewError(code ErrorCode, msg string) error {
87106
88107// Error returns the error message with OCI reference
89108func (oci * Error ) Error () string {
90- return fmt .Sprintf ("%s\n Refer to: %s%s " , oci .Err .Error (), referencePrefix , oci .Reference )
109+ return fmt .Sprintf ("%s\n Refer to: %s/v%s/%s " , oci .Err .Error (), referencePrefix , rspec . Version , oci .Reference )
91110}
0 commit comments