diff --git a/cmd/nvidia-ctk/cdi/generate/generate.go b/cmd/nvidia-ctk/cdi/generate/generate.go index c9a146fb..4e930e05 100644 --- a/cmd/nvidia-ctk/cdi/generate/generate.go +++ b/cmd/nvidia-ctk/cdi/generate/generate.go @@ -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 "" diff --git a/pkg/nvcdi/spec/api.go b/pkg/nvcdi/spec/api.go index e79216c2..a72c6dff 100644 --- a/pkg/nvcdi/spec/api.go +++ b/pkg/nvcdi/spec/api.go @@ -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 diff --git a/pkg/nvcdi/spec/builder.go b/pkg/nvcdi/spec/builder.go index e8fc4b71..bdf68fe2 100644 --- a/pkg/nvcdi/spec/builder.go +++ b/pkg/nvcdi/spec/builder.go @@ -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 } diff --git a/pkg/nvcdi/spec/spec.go b/pkg/nvcdi/spec/spec.go index 961993a2..c0e96946 100644 --- a/pkg/nvcdi/spec/spec.go +++ b/pkg/nvcdi/spec/spec.go @@ -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" }