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 (
formatJSON = "json"
formatYAML = "yaml"
allDeviceName = "all"
)
@ -87,7 +84,7 @@ func (m command) build() *cli.Command {
&cli.StringFlag{
Name: "format",
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,
},
&cli.StringFlag{
@ -121,8 +118,8 @@ func (m command) validateFlags(c *cli.Context, cfg *config) error {
cfg.format = strings.ToLower(cfg.format)
switch cfg.format {
case formatJSON:
case formatYAML:
case spec.FormatJSON:
case spec.FormatYAML:
default:
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 {
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 == "" {
_, err := spec.WriteTo(os.Stdout)
@ -181,11 +178,9 @@ func formatFromFilename(filename string) string {
ext := filepath.Ext(filename)
switch strings.ToLower(ext) {
case ".json":
return formatJSON
case ".yaml":
return formatYAML
case ".yml":
return formatYAML
return spec.FormatJSON
case ".yaml", ".yml":
return spec.FormatYAML
}
return ""

View File

@ -25,6 +25,11 @@ import (
const (
// DetectMinimumVersion is a constant that triggers a spec to detect the minimum required 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

View File

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

View File

@ -34,7 +34,7 @@ var _ Interface = (*spec)(nil)
// New creates a new spec with the specified options.
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.
@ -98,9 +98,9 @@ func (s *spec) normalizePath(path string) string {
func (s *spec) extension() string {
switch s.format {
case "json":
case FormatJSON:
return ".json"
case "yaml", "yml":
case FormatYAML:
return ".yaml"
}