WritePredicate writes the Predicate as a JSON file to a temp directory and returns the path.
(predicate *Predicate)
| 340 | |
| 341 | // WritePredicate writes the Predicate as a JSON file to a temp directory and returns the path. |
| 342 | func (w *Writer) WritePredicate(predicate *Predicate) (string, error) { |
| 343 | log.Infof("Writing VSA for images: %v", predicate.ImageRefs) |
| 344 | |
| 345 | // Serialize with indent |
| 346 | data, err := json.MarshalIndent(predicate, "", " ") |
| 347 | if err != nil { |
| 348 | return "", fmt.Errorf("failed to marshal VSA predicate: %w", err) |
| 349 | } |
| 350 | |
| 351 | // Create or use directory based on whether TempDirPrefix is an absolute path |
| 352 | var tempDir string |
| 353 | if filepath.IsAbs(w.TempDirPrefix) { |
| 354 | // TempDirPrefix is an absolute path - use it directly |
| 355 | tempDir = w.TempDirPrefix |
| 356 | // Ensure the directory exists |
| 357 | if err := w.FS.MkdirAll(tempDir, 0o755); err != nil { |
| 358 | return "", fmt.Errorf("failed to create output directory: %w", err) |
| 359 | } |
| 360 | } else { |
| 361 | // TempDirPrefix is a prefix - create temp directory |
| 362 | var err error |
| 363 | tempDir, err = afero.TempDir(w.FS, "", w.TempDirPrefix) |
| 364 | if err != nil { |
| 365 | return "", fmt.Errorf("failed to create temp directory: %w", err) |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // Write to file with same naming convention as old VSA |
| 370 | componentName := "unknown" |
| 371 | if predicate.Summary.Component.Name != "" { |
| 372 | componentName = predicate.Summary.Component.Name |
| 373 | } |
| 374 | filename := fmt.Sprintf("vsa-%s.json", componentName) |
| 375 | filepath := filepath.Join(tempDir, filename) |
| 376 | err = afero.WriteFile(w.FS, filepath, data, w.FilePerm) |
| 377 | if err != nil { |
| 378 | return "", fmt.Errorf("failed to write VSA predicate to file: %w", err) |
| 379 | } |
| 380 | |
| 381 | log.Infof("VSA predicate written to: %s", filepath) |
| 382 | return filepath, nil |
| 383 | } |
| 384 | |
| 385 | // VSALookupResult represents the result of looking up an existing VSA |
| 386 | type VSALookupResult struct { |