Skip to content

Commit dd67e3a

Browse files
committed
refactor: Consolidate handling of output to stdout
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 2798612 commit dd67e3a

3 files changed

Lines changed: 9 additions & 21 deletions

File tree

cmd/nvidia-ctk/cdi/generate/generate.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"os"
2423
"path/filepath"
2524
"slices"
2625
"strings"
@@ -352,14 +351,6 @@ type generatedSpecs struct {
352351
func (g *generatedSpecs) Save(filename string) error {
353352
filename = g.updateFilename(filename)
354353

355-
if filename == "" {
356-
_, err := g.WriteTo(os.Stdout)
357-
if err != nil {
358-
return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err)
359-
}
360-
return nil
361-
}
362-
363354
return g.Interface.Save(filename)
364355
}
365356

cmd/nvidia-ctk/cdi/transform/root/root.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,5 @@ func (o transformOptions) getContents() ([]byte, error) {
159159

160160
// Save saves the CDI specification to the output file
161161
func (o transformOptions) Save(s spec.Interface) error {
162-
if o.output == "" {
163-
_, err := s.WriteTo(os.Stdout)
164-
if err != nil {
165-
return fmt.Errorf("failed to write CDI spec to STDOUT: %v", err)
166-
}
167-
return nil
168-
}
169-
170162
return s.Save(o.output)
171163
}

pkg/nvcdi/spec/spec.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,16 @@ func (t *transfromAsValidator) Validate(s *specs.Spec) error {
8686

8787
// Save writes the spec to the specified path and overwrites the file if it exists.
8888
func (s *spec) Save(path string) error {
89-
pathWithExtension := s.ensureExtension(path)
89+
if pathWithExtension := s.ensureExtension(path); pathWithExtension != "" {
90+
return producer.Save(s.Raw(), pathWithExtension,
91+
s.producerOptions()...,
92+
)
93+
}
94+
if _, err := s.WriteTo(os.Stdout); err != nil {
95+
return fmt.Errorf("failed to write CDI spec to STDOUT: %w", err)
96+
}
9097

91-
return producer.Save(s.Raw(), pathWithExtension,
92-
s.producerOptions()...,
93-
)
98+
return nil
9499
}
95100

96101
// WriteTo writes the spec to the specified writer.

0 commit comments

Comments
 (0)