2023-02-20 12:29:27 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package spec
|
|
|
|
|
|
|
|
import (
|
2023-03-01 05:50:46 +00:00
|
|
|
"fmt"
|
2023-02-22 14:19:22 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-02-20 12:29:27 +00:00
|
|
|
|
|
|
|
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
|
|
|
|
"github.com/container-orchestrated-devices/container-device-interface/specs-go"
|
|
|
|
)
|
|
|
|
|
2023-02-22 14:19:22 +00:00
|
|
|
type spec struct {
|
|
|
|
*specs.Spec
|
|
|
|
format string
|
|
|
|
}
|
2023-02-20 12:29:27 +00:00
|
|
|
|
|
|
|
var _ Interface = (*spec)(nil)
|
|
|
|
|
2023-02-22 14:19:22 +00:00
|
|
|
// New creates a new spec with the specified options.
|
|
|
|
func New(opts ...Option) (Interface, error) {
|
2023-02-22 14:33:30 +00:00
|
|
|
return newBuilder(opts...).Build()
|
2023-02-22 14:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes the spec to the specified path and overwrites the file if it exists.
|
|
|
|
func (s *spec) Save(path string) error {
|
2023-03-01 05:50:46 +00:00
|
|
|
path, err := s.normalizePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to normalize path: %w", err)
|
|
|
|
}
|
2023-02-22 14:19:22 +00:00
|
|
|
|
|
|
|
specDir := filepath.Dir(path)
|
|
|
|
registry := cdi.GetRegistry(
|
|
|
|
cdi.WithAutoRefresh(false),
|
|
|
|
cdi.WithSpecDirs(specDir),
|
|
|
|
)
|
|
|
|
|
|
|
|
return registry.SpecDB().WriteSpec(s.Raw(), filepath.Base(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo writes the spec to the specified writer.
|
|
|
|
func (s *spec) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
name, err := cdi.GenerateNameForSpec(s.Raw())
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2023-02-20 12:29:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 05:50:46 +00:00
|
|
|
path, _ := s.normalizePath(name)
|
2023-02-22 14:19:22 +00:00
|
|
|
tmpFile, err := os.CreateTemp("", "*"+filepath.Base(path))
|
2023-02-20 12:29:27 +00:00
|
|
|
if err != nil {
|
2023-02-22 14:19:22 +00:00
|
|
|
return 0, err
|
2023-02-20 12:29:27 +00:00
|
|
|
}
|
2023-02-22 14:19:22 +00:00
|
|
|
defer os.Remove(tmpFile.Name())
|
2023-02-20 12:29:27 +00:00
|
|
|
|
2023-02-22 14:19:22 +00:00
|
|
|
if err := s.Save(tmpFile.Name()); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-02-20 12:29:27 +00:00
|
|
|
|
2023-02-22 14:19:22 +00:00
|
|
|
err = tmpFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to close temporary file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := os.Open(tmpFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed to open temporary file: %w", err)
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
return io.Copy(w, r)
|
2023-02-20 12:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Raw returns a pointer to the raw spec.
|
|
|
|
func (s *spec) Raw() *specs.Spec {
|
2023-02-22 14:19:22 +00:00
|
|
|
return s.Spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizePath ensures that the specified path has a supported extension
|
2023-03-01 05:50:46 +00:00
|
|
|
func (s *spec) normalizePath(path string) (string, error) {
|
2023-02-22 14:19:22 +00:00
|
|
|
if ext := filepath.Ext(path); ext != ".yaml" && ext != ".json" {
|
|
|
|
path += s.extension()
|
|
|
|
}
|
|
|
|
|
2023-03-01 05:50:46 +00:00
|
|
|
if filepath.Clean(filepath.Dir(path)) == "." {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return path, fmt.Errorf("failed to get current working directory: %v", err)
|
|
|
|
}
|
|
|
|
path = filepath.Join(pwd, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
2023-02-22 14:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *spec) extension() string {
|
|
|
|
switch s.format {
|
2023-02-22 14:33:30 +00:00
|
|
|
case FormatJSON:
|
2023-02-22 14:19:22 +00:00
|
|
|
return ".json"
|
2023-02-22 14:33:30 +00:00
|
|
|
case FormatYAML:
|
2023-02-22 14:19:22 +00:00
|
|
|
return ".yaml"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ".yaml"
|
2023-02-20 12:29:27 +00:00
|
|
|
}
|