mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 07:40:48 -08:00
refactor: move artifact types under artifact package to avoid import cycles (#6652)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
@@ -37,23 +37,23 @@ func NewArtifact(filePath string, c cache.ArtifactCache, opt artifact.Option) (a
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a Artifact) Inspect(_ context.Context) (types.ArtifactReference, error) {
|
||||
func (a Artifact) Inspect(_ context.Context) (artifact.Reference, error) {
|
||||
f, err := os.Open(a.filePath)
|
||||
if err != nil {
|
||||
return types.ArtifactReference{}, xerrors.Errorf("failed to open sbom file error: %w", err)
|
||||
return artifact.Reference{}, xerrors.Errorf("failed to open sbom file error: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// Format auto-detection
|
||||
format, err := sbom.DetectFormat(f)
|
||||
if err != nil {
|
||||
return types.ArtifactReference{}, xerrors.Errorf("failed to detect SBOM format: %w", err)
|
||||
return artifact.Reference{}, xerrors.Errorf("failed to detect SBOM format: %w", err)
|
||||
}
|
||||
log.Info("Detected SBOM format", log.String("format", string(format)))
|
||||
|
||||
bom, err := sbom.Decode(f, format)
|
||||
if err != nil {
|
||||
return types.ArtifactReference{}, xerrors.Errorf("SBOM decode error: %w", err)
|
||||
return artifact.Reference{}, xerrors.Errorf("SBOM decode error: %w", err)
|
||||
}
|
||||
|
||||
blobInfo := types.BlobInfo{
|
||||
@@ -65,23 +65,23 @@ func (a Artifact) Inspect(_ context.Context) (types.ArtifactReference, error) {
|
||||
|
||||
cacheKey, err := a.calcCacheKey(blobInfo)
|
||||
if err != nil {
|
||||
return types.ArtifactReference{}, xerrors.Errorf("failed to calculate a cache key: %w", err)
|
||||
return artifact.Reference{}, xerrors.Errorf("failed to calculate a cache key: %w", err)
|
||||
}
|
||||
|
||||
if err = a.cache.PutBlob(cacheKey, blobInfo); err != nil {
|
||||
return types.ArtifactReference{}, xerrors.Errorf("failed to store blob (%s) in cache: %w", cacheKey, err)
|
||||
return artifact.Reference{}, xerrors.Errorf("failed to store blob (%s) in cache: %w", cacheKey, err)
|
||||
}
|
||||
|
||||
var artifactType types.ArtifactType
|
||||
var artifactType artifact.Type
|
||||
switch format {
|
||||
case sbom.FormatCycloneDXJSON, sbom.FormatCycloneDXXML, sbom.FormatAttestCycloneDXJSON, sbom.FormatLegacyCosignAttestCycloneDXJSON:
|
||||
artifactType = types.ArtifactCycloneDX
|
||||
artifactType = artifact.TypeCycloneDX
|
||||
case sbom.FormatSPDXTV, sbom.FormatSPDXJSON:
|
||||
artifactType = types.ArtifactSPDX
|
||||
artifactType = artifact.TypeSPDX
|
||||
|
||||
}
|
||||
|
||||
return types.ArtifactReference{
|
||||
return artifact.Reference{
|
||||
Name: a.filePath,
|
||||
Type: artifactType,
|
||||
ID: cacheKey, // use a cache key as pseudo artifact ID
|
||||
@@ -92,7 +92,7 @@ func (a Artifact) Inspect(_ context.Context) (types.ArtifactReference, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a Artifact) Clean(reference types.ArtifactReference) error {
|
||||
func (a Artifact) Clean(reference artifact.Reference) error {
|
||||
return a.cache.DeleteBlobs(reference.BlobIDs)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user