| 36 | } |
| 37 | |
| 38 | func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp []*api.Attestation) (string, error) { |
| 39 | metadataFilePath := s.createJSONLinesFilePath(artifactDigest) |
| 40 | |
| 41 | f, err := os.Create(metadataFilePath) |
| 42 | if err != nil { |
| 43 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to create file: %v", err)) |
| 44 | } |
| 45 | |
| 46 | for _, resp := range attestationsResp { |
| 47 | bundle := resp.Bundle |
| 48 | attBytes, err := json.Marshal(bundle) |
| 49 | if err != nil { |
| 50 | if err = f.Close(); err != nil { |
| 51 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while marshalling JSON: %v", err)) |
| 52 | } |
| 53 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to marshall attestation to JSON while writing to file: %v", err)) |
| 54 | } |
| 55 | |
| 56 | withNewline := fmt.Sprintf("%s\n", attBytes) |
| 57 | _, err = f.Write([]byte(withNewline)) |
| 58 | if err != nil { |
| 59 | if err = f.Close(); err != nil { |
| 60 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while handling write error: %v", err)) |
| 61 | } |
| 62 | |
| 63 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to write attestations: %v", err)) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if err = f.Close(); err != nil { |
| 68 | return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file after writing attestations: %v", err)) |
| 69 | } |
| 70 | |
| 71 | return metadataFilePath, nil |
| 72 | } |
| 73 | |
| 74 | func NewLiveStore(outputPath string) *LiveStore { |
| 75 | return &LiveStore{ |