-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds a SpecProducer that can be used by clients that are only concerned with outputing specs. A spec producer is configured on construction to allow for the default output format, file permissions, and spec validation to be specified. Signed-off-by: Evan Lezar <[email protected]>
- Loading branch information
Showing
13 changed files
with
802 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright © 2024 The CDI Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package producer | ||
|
||
import cdi "tags.cncf.io/container-device-interface/specs-go" | ||
|
||
type SpecFormat string | ||
Check failure on line 21 in pkg/cdi/producer/api.go GitHub Actions / Run go sanity tools (linux, amd64)
|
||
|
||
const ( | ||
// DefaultSpecFormat defines the default encoding used to write CDI specs. | ||
DefaultSpecFormat = SpecFormatYAML | ||
|
||
// SpecFormatJSON defines a CDI spec formatted as JSON. | ||
SpecFormatJSON = SpecFormat(".json") | ||
// SpecFormatYAML defines a CDI spec formatted as YAML. | ||
SpecFormatYAML = SpecFormat(".yaml") | ||
) | ||
|
||
// A SpecValidator is used to validate a CDI spec. | ||
type SpecValidator interface { | ||
Validate(*cdi.Spec) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright © 2024 The CDI Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
|
||
"tags.cncf.io/container-device-interface/pkg/cdi/producer/validator" | ||
) | ||
|
||
// An Option defines a functional option for constructing a producer. | ||
type Option func(*options) error | ||
|
||
type options struct { | ||
specFormat SpecFormat | ||
specValidator SpecValidator | ||
overwrite bool | ||
permissions fs.FileMode | ||
} | ||
|
||
// WithSpecFormat sets the output format of a CDI specification. | ||
func WithSpecFormat(format SpecFormat) Option { | ||
return func(o *options) error { | ||
switch format { | ||
case SpecFormatJSON, SpecFormatYAML: | ||
o.specFormat = format | ||
default: | ||
return fmt.Errorf("invalid CDI spec format %v", format) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// WithSpecValidator sets a validator to be used when writing an output spec. | ||
func WithSpecValidator(specValidator SpecValidator) Option { | ||
return func(o *options) error { | ||
if specValidator == nil { | ||
specValidator = validator.Disabled | ||
} | ||
o.specValidator = specValidator | ||
return nil | ||
} | ||
} | ||
|
||
// WithOverwrite specifies whether a producer should overwrite a CDI spec when | ||
// saving to file. | ||
func WithOverwrite(overwrite bool) Option { | ||
return func(o *options) error { | ||
o.overwrite = overwrite | ||
return nil | ||
} | ||
} | ||
|
||
// WithPermissions sets the file mode to be used for a saved CDI spec. | ||
func WithPermissions(permissions fs.FileMode) Option { | ||
return func(o *options) error { | ||
o.permissions = permissions | ||
return nil | ||
} | ||
} |
Oops, something went wrong.