From 5d6ffa3207b1f3340fd15afb8af692cc55a3c430 Mon Sep 17 00:00:00 2001 From: Meredith Lancaster Date: Thu, 6 Feb 2025 12:37:23 -0700 Subject: [PATCH] dedup local bundle err handling Signed-off-by: Meredith Lancaster --- .../attestation/verification/attestation.go | 46 ++++++++----------- .../verification/attestation_test.go | 4 +- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/pkg/cmd/attestation/verification/attestation.go b/pkg/cmd/attestation/verification/attestation.go index fdfd667bd..f613ba2d5 100644 --- a/pkg/cmd/attestation/verification/attestation.go +++ b/pkg/cmd/attestation/verification/attestation.go @@ -29,34 +29,28 @@ type FetchRemoteAttestationsParams struct { // GetLocalAttestations returns a slice of attestations read from a local bundle file. func GetLocalAttestations(path string) ([]*api.Attestation, error) { + var attestations []*api.Attestation + var err error fileExt := filepath.Ext(path) - switch fileExt { - case ".json": - attestations, err := loadBundleFromJSONFile(path) - if err != nil { - var pathErr *os.PathError - if errors.As(err, &pathErr) { - return nil, fmt.Errorf("bundle could not be loaded from JSON file at %s", path) - } else if errors.Is(err, bundle.ErrValidation) { - return nil, err - } - return nil, fmt.Errorf("bundle content could not be parsed") - } - return attestations, nil - case ".jsonl": - attestations, err := loadBundlesFromJSONLinesFile(path) - if err != nil { - var pathErr *os.PathError - if errors.As(err, &pathErr) { - return nil, fmt.Errorf("bundles could not be loaded from JSON lines file at %s", path) - } else if errors.Is(err, bundle.ErrValidation) { - return nil, err - } - return nil, fmt.Errorf("bundle content could not be parsed") - } - return attestations, nil + if fileExt == ".json" { + attestations, err = loadBundleFromJSONFile(path) + } else if fileExt == ".jsonl" { + attestations, err = loadBundlesFromJSONLinesFile(path) + } else { + return nil, ErrUnrecognisedBundleExtension } - return nil, ErrUnrecognisedBundleExtension + + if err != nil { + var pathErr *os.PathError + if errors.As(err, &pathErr) { + return nil, fmt.Errorf("could not load content from file path %s", path) + } else if errors.Is(err, bundle.ErrValidation) { + return nil, err + } + return nil, fmt.Errorf("bundle content could not be parsed") + } + + return attestations, nil } func loadBundleFromJSONFile(path string) ([]*api.Attestation, error) { diff --git a/pkg/cmd/attestation/verification/attestation_test.go b/pkg/cmd/attestation/verification/attestation_test.go index 08f0ccfef..8acff0c37 100644 --- a/pkg/cmd/attestation/verification/attestation_test.go +++ b/pkg/cmd/attestation/verification/attestation_test.go @@ -92,7 +92,7 @@ func TestGetLocalAttestations(t *testing.T) { path := "../test/data/not-found-bundle.json" attestations, err := GetLocalAttestations(path) - require.ErrorContains(t, err, "bundle could not be loaded from JSON file") + require.ErrorContains(t, err, "could not load content from file path") require.Nil(t, attestations) }) @@ -100,7 +100,7 @@ func TestGetLocalAttestations(t *testing.T) { path := "../test/data/not-found-bundle.jsonl" attestations, err := GetLocalAttestations(path) - require.ErrorContains(t, err, "bundles could not be loaded from JSON lines file") + require.ErrorContains(t, err, "could not load content from file path") require.Nil(t, attestations) })