look for err specific to file write
Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
parent
7f477afa5d
commit
8e3c197755
2 changed files with 9 additions and 5 deletions
|
|
@ -310,5 +310,6 @@ func TestRunDownload(t *testing.T) {
|
|||
|
||||
err := runDownload(&opts)
|
||||
require.Error(t, err)
|
||||
require.ErrorAs(t, err, &ErrAttestationFileCreation)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,15 @@ package download
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
|
||||
)
|
||||
|
||||
var ErrAttestationFileCreation = fmt.Errorf("failed to write attestations to file")
|
||||
|
||||
type MetadataStore interface {
|
||||
createMetadataFile(artifactDigest string, attestationsResp []*api.Attestation) (string, error)
|
||||
}
|
||||
|
|
@ -29,29 +32,29 @@ func (s *LiveStore) createMetadataFile(artifactDigest string, attestationsResp [
|
|||
|
||||
f, err := os.Create(metadataFilePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create trusted metadata file: %w", err)
|
||||
return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to create file: %w", err))
|
||||
}
|
||||
|
||||
for _, resp := range attestationsResp {
|
||||
bundle := resp.Bundle
|
||||
attBytes, err := json.Marshal(bundle)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to marshall attestation to JSON: %w", err)
|
||||
return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to marshall attestation to JSON while writing to file: %w", err))
|
||||
}
|
||||
|
||||
withNewline := fmt.Sprintf("%s\n", attBytes)
|
||||
_, err = f.Write([]byte(withNewline))
|
||||
if err != nil {
|
||||
if err = f.Close(); err != nil {
|
||||
return "", fmt.Errorf("failed to close file while handling write error: %w", err)
|
||||
return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file while handling write error: %w", err))
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("failed to write trusted metadata: %w", err)
|
||||
return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to write attestations: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
if err = f.Close(); err != nil {
|
||||
return "", fmt.Errorf("failed to close file after writing metadata: %w", err)
|
||||
return "", errors.Join(ErrAttestationFileCreation, fmt.Errorf("failed to close file after writing attestations: %w", err))
|
||||
}
|
||||
|
||||
return metadataFilePath, nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue