|
| 1 | +/* |
| 2 | + Copyright © 2024 The CDI Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package producer |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + cdi "tags.cncf.io/container-device-interface/specs-go" |
| 26 | +) |
| 27 | + |
| 28 | +// A SpecWriter defines a structure for outputting CDI specifications. |
| 29 | +type SpecWriter struct { |
| 30 | + options |
| 31 | +} |
| 32 | + |
| 33 | +// NewSpecWriter creates a spec writer with the supplied options. |
| 34 | +func NewSpecWriter(opts ...Option) (*SpecWriter, error) { |
| 35 | + sw := &SpecWriter{ |
| 36 | + options: options{ |
| 37 | + overwrite: true, |
| 38 | + // TODO: This could be updated to 0644 to be world-readable. |
| 39 | + permissions: 0600, |
| 40 | + specFormat: DefaultSpecFormat, |
| 41 | + }, |
| 42 | + } |
| 43 | + for _, opt := range opts { |
| 44 | + err := opt(&sw.options) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + } |
| 49 | + return sw, nil |
| 50 | +} |
| 51 | + |
| 52 | +// Save writes a CDI spec to a file with the specified name. |
| 53 | +// If the filename ends in a supported extension, the format implied by the |
| 54 | +// extension takes precedence over the format with which the SpecWriter was |
| 55 | +// configured. |
| 56 | +func (p *SpecWriter) Save(spec *cdi.Spec, filename string) (string, error) { |
| 57 | + filename, outputFormat := p.specFormat.normalizeFilename(filename) |
| 58 | + |
| 59 | + options := p.options |
| 60 | + options.specFormat = outputFormat |
| 61 | + specFormatter := specFormatter{ |
| 62 | + Spec: spec, |
| 63 | + options: options, |
| 64 | + } |
| 65 | + |
| 66 | + dir := filepath.Dir(filename) |
| 67 | + if dir != "" { |
| 68 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 69 | + return "", fmt.Errorf("failed to create Spec dir: %w", err) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + tmp, err := os.CreateTemp(dir, "spec.*.tmp") |
| 74 | + if err != nil { |
| 75 | + return "", fmt.Errorf("failed to create Spec file: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + _, err = specFormatter.WriteTo(tmp) |
| 79 | + tmp.Close() |
| 80 | + if err != nil { |
| 81 | + return "", fmt.Errorf("failed to write Spec file: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + if err := os.Chmod(tmp.Name(), p.permissions); err != nil { |
| 85 | + return "", fmt.Errorf("failed to set permissions on spec file: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + err = renameIn(dir, filepath.Base(tmp.Name()), filepath.Base(filename), p.overwrite) |
| 89 | + if err != nil { |
| 90 | + _ = os.Remove(tmp.Name()) |
| 91 | + return "", fmt.Errorf("failed to write Spec file: %w", err) |
| 92 | + } |
| 93 | + return filename, nil |
| 94 | +} |
| 95 | + |
| 96 | +// WriteSpecTo writes the specified spec to the specified writer. |
| 97 | +func (p *SpecWriter) WriteSpecTo(spec *cdi.Spec, w io.Writer) (int64, error) { |
| 98 | + specFormatter := specFormatter{ |
| 99 | + Spec: spec, |
| 100 | + options: p.options, |
| 101 | + } |
| 102 | + |
| 103 | + return specFormatter.WriteTo(w) |
| 104 | +} |
0 commit comments