|
| 1 | +// internal/manifest/manifest.go |
| 2 | +package manifest |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + utils "github.com/open-edge-platform/image-composer/internal/utils/logger" |
| 10 | +) |
| 11 | + |
| 12 | +// SoftwarePackageManifest represents the structure of the manifest file. |
| 13 | +type SoftwarePackageManifest struct { |
| 14 | + SchemaVersion string `json:"schema_version"` |
| 15 | + ImageVersion string `json:"image_version"` |
| 16 | + BuiltAt string `json:"built_at"` |
| 17 | + Arch string `json:"arch"` |
| 18 | + SizeBytes int64 `json:"size_bytes"` |
| 19 | + Hash string `json:"hash"` |
| 20 | + HashAlg string `json:"hash_alg"` |
| 21 | + Signature string `json:"signature"` |
| 22 | + SigAlg string `json:"sig_alg"` |
| 23 | + MinCurrentVersion string `json:"min_current_version"` |
| 24 | +} |
| 25 | + |
| 26 | +// WriteManifestToFile writes the manifest to the specified output file. |
| 27 | +func WriteManifestToFile(manifest SoftwarePackageManifest, outputFile string) error { |
| 28 | + logger := utils.Logger() |
| 29 | + logger.Infof("Writing the Image Manifest to the file: %s", outputFile) |
| 30 | + // Marshal the manifest struct to JSON |
| 31 | + manifestJSON, err := json.MarshalIndent(manifest, "", " ") |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("error marshaling manifest to JSON: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Create or open the output file |
| 37 | + file, err := os.Create(outputFile) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("error creating/opening file: %w", err) |
| 40 | + } |
| 41 | + defer file.Close() |
| 42 | + |
| 43 | + // Write the JSON data to the file |
| 44 | + _, err = file.Write(manifestJSON) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("error writing to file: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
0 commit comments