github.com/anchore/syft@v1.38.2/syft/source/snapsource/manifest.go (about)

     1  package snapsource
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/goccy/go-yaml"
     7  
     8  	"github.com/anchore/syft/internal"
     9  	"github.com/anchore/syft/syft/file"
    10  )
    11  
    12  type snapManifest struct {
    13  	Name          string   `yaml:"name"`
    14  	Version       string   `yaml:"version"`
    15  	Summary       string   `yaml:"summary"`
    16  	Base          string   `yaml:"base"`
    17  	Grade         string   `yaml:"grade"`
    18  	Confinement   string   `yaml:"confinement"`
    19  	Architectures []string `yaml:"architectures"`
    20  }
    21  
    22  const manifestLocation = "/meta/snap.yaml"
    23  
    24  func parseManifest(resolver file.Resolver) (*snapManifest, error) {
    25  	locations, err := resolver.FilesByPath(manifestLocation)
    26  	if err != nil {
    27  		return nil, fmt.Errorf("unable to find snap manifest file: %w", err)
    28  	}
    29  
    30  	if len(locations) == 0 {
    31  		return nil, fmt.Errorf("no snap manifest file found")
    32  	}
    33  
    34  	if len(locations) > 1 {
    35  		return nil, fmt.Errorf("multiple snap manifest files found")
    36  	}
    37  
    38  	manifestFile := locations[0]
    39  
    40  	reader, err := resolver.FileContentsByLocation(manifestFile)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("unable to read snap manifest file: %w", err)
    43  	}
    44  	defer internal.CloseAndLogError(reader, manifestFile.RealPath)
    45  
    46  	var manifest snapManifest
    47  	if err := yaml.NewDecoder(reader).Decode(&manifest); err != nil {
    48  		return nil, fmt.Errorf("unable to decode snap manifest file: %w", err)
    49  	}
    50  
    51  	if manifest.Name == "" || manifest.Version == "" {
    52  		return nil, fmt.Errorf("invalid snap manifest file: missing name or version")
    53  	}
    54  
    55  	return &manifest, nil
    56  }