Skip to content

Commit 99ae94f

Browse files
committed
Use producer validation in WriteSpec
Signed-off-by: Evan Lezar <[email protected]>
1 parent f5d3f38 commit 99ae94f

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

pkg/cdi/cache.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,35 +281,31 @@ func (c *Cache) highestPrioritySpecDir() (string, int) {
281281
// WriteSpec writes a Spec file with the given content into the highest
282282
// priority Spec directory. If name has a "json" or "yaml" extension it
283283
// choses the encoding. Otherwise the default YAML encoding is used.
284+
//
285+
// Deprecated: use producer.NewSpecWriter instead.
284286
func (c *Cache) WriteSpec(raw *cdi.Spec, name string) error {
285287
var (
286288
specDir string
287-
prio int
288-
spec *Spec
289289
err error
290290
)
291-
specDir, prio = c.highestPrioritySpecDir()
291+
specDir, _ = c.highestPrioritySpecDir()
292292
if specDir == "" {
293293
return errors.New("no Spec directories to write to")
294294
}
295295

296296
path := filepath.Join(specDir, name)
297-
298-
// We call newSpec to perform the required validation.
299-
// This also sets the extension of the path.
300-
spec, err = newSpec(raw, path, prio)
301-
if err != nil {
302-
return err
297+
if ext := filepath.Ext(path); ext != ".yaml" && ext != ".json" {
298+
path += defaultSpecExt
303299
}
300+
path = filepath.Clean(path)
304301

305302
p, err := producer.NewSpecWriter(
306-
producer.WithSpecFormat(producer.SpecFormatYAML),
307303
producer.WithOverwrite(true),
308304
)
309305
if err != nil {
310306
return err
311307
}
312-
if _, err := p.Save(spec.Spec, spec.path); err != nil {
308+
if _, err := p.Save(raw, path); err != nil {
313309
return err
314310
}
315311
return nil

0 commit comments

Comments
 (0)