Move formatJSON and formatYAML to nvcdi/spec package

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-02-22 16:33:30 +02:00
parent 890a519121
commit 8be6de177f
4 changed files with 20 additions and 17 deletions

View File

@ -35,9 +35,6 @@ import (
) )
const ( const (
formatJSON = "json"
formatYAML = "yaml"
allDeviceName = "all" allDeviceName = "all"
) )
@ -87,7 +84,7 @@ func (m command) build() *cli.Command {
&cli.StringFlag{ &cli.StringFlag{
Name: "format", Name: "format",
Usage: "The output format for the generated spec [json | yaml]. This overrides the format defined by the output file extension (if specified).", Usage: "The output format for the generated spec [json | yaml]. This overrides the format defined by the output file extension (if specified).",
Value: formatYAML, Value: spec.FormatYAML,
Destination: &cfg.format, Destination: &cfg.format,
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -121,8 +118,8 @@ func (m command) validateFlags(c *cli.Context, cfg *config) error {
cfg.format = strings.ToLower(cfg.format) cfg.format = strings.ToLower(cfg.format)
switch cfg.format { switch cfg.format {
case formatJSON: case spec.FormatJSON:
case formatYAML: case spec.FormatYAML:
default: default:
return fmt.Errorf("invalid output format: %v", cfg.format) return fmt.Errorf("invalid output format: %v", cfg.format)
} }
@ -160,7 +157,7 @@ func (m command) run(c *cli.Context, cfg *config) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to generate CDI spec: %v", err) return fmt.Errorf("failed to generate CDI spec: %v", err)
} }
m.logger.Infof("Generated CDI spec with version", spec.Raw().Version) m.logger.Infof("Generated CDI spec with version %v", spec.Raw().Version)
if cfg.output == "" { if cfg.output == "" {
_, err := spec.WriteTo(os.Stdout) _, err := spec.WriteTo(os.Stdout)
@ -181,11 +178,9 @@ func formatFromFilename(filename string) string {
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
switch strings.ToLower(ext) { switch strings.ToLower(ext) {
case ".json": case ".json":
return formatJSON return spec.FormatJSON
case ".yaml": case ".yaml", ".yml":
return formatYAML return spec.FormatYAML
case ".yml":
return formatYAML
} }
return "" return ""

View File

@ -25,6 +25,11 @@ import (
const ( const (
// DetectMinimumVersion is a constant that triggers a spec to detect the minimum required version. // DetectMinimumVersion is a constant that triggers a spec to detect the minimum required version.
DetectMinimumVersion = "DETECT_MINIMUM_VERSION" DetectMinimumVersion = "DETECT_MINIMUM_VERSION"
// FormatJSON indicates a JSON output format
FormatJSON = "json"
// FormatYAML indicates a YAML output format
FormatYAML = "yaml"
) )
// Interface is the interface for the spec API // Interface is the interface for the spec API

View File

@ -33,8 +33,8 @@ type builder struct {
format string format string
} }
// NewBuilder creates a new spec builder with the supplied options // newBuilder creates a new spec builder with the supplied options
func NewBuilder(opts ...Option) *builder { func newBuilder(opts ...Option) *builder {
s := &builder{} s := &builder{}
for _, opt := range opts { for _, opt := range opts {
opt(s) opt(s)
@ -48,6 +48,9 @@ func NewBuilder(opts ...Option) *builder {
if s.class == "" { if s.class == "" {
s.class = "gpu" s.class = "gpu"
} }
if s.format == "" {
s.format = FormatYAML
}
return s return s
} }

View File

@ -34,7 +34,7 @@ var _ Interface = (*spec)(nil)
// New creates a new spec with the specified options. // New creates a new spec with the specified options.
func New(opts ...Option) (Interface, error) { func New(opts ...Option) (Interface, error) {
return NewBuilder(opts...).Build() return newBuilder(opts...).Build()
} }
// Save writes the spec to the specified path and overwrites the file if it exists. // Save writes the spec to the specified path and overwrites the file if it exists.
@ -98,9 +98,9 @@ func (s *spec) normalizePath(path string) string {
func (s *spec) extension() string { func (s *spec) extension() string {
switch s.format { switch s.format {
case "json": case FormatJSON:
return ".json" return ".json"
case "yaml", "yml": case FormatYAML:
return ".yaml" return ".yaml"
} }