github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/snap/metadata/metadata.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package metadata defines a metadata struct for SNAP packages.
    16  package metadata
    17  
    18  import (
    19  	"github.com/google/osv-scalibr/log"
    20  
    21  	pb "github.com/google/osv-scalibr/binary/proto/scan_result_go_proto"
    22  )
    23  
    24  // Metadata holds parsing information for a SNAP package.
    25  type Metadata struct {
    26  	Name              string
    27  	Version           string
    28  	Grade             string
    29  	Type              string
    30  	Architectures     []string
    31  	OSID              string
    32  	OSVersionCodename string
    33  	OSVersionID       string
    34  }
    35  
    36  // ToNamespace extracts the PURL namespace from the metadata.
    37  func (m *Metadata) ToNamespace() string {
    38  	if m.OSID != "" {
    39  		return m.OSID
    40  	}
    41  	log.Errorf("os-release[ID] not set, fallback to ''")
    42  	return ""
    43  }
    44  
    45  // ToDistro extracts the OS distro from the metadata.
    46  func (m *Metadata) ToDistro() string {
    47  	// e.g. jammy
    48  	if m.OSVersionCodename != "" {
    49  		return m.OSVersionCodename
    50  	}
    51  	// fallback: e.g. 22.04
    52  	if m.OSVersionID != "" {
    53  		log.Warnf("VERSION_CODENAME not set in os-release, fallback to VERSION_ID")
    54  		return m.OSVersionID
    55  	}
    56  	log.Errorf("VERSION_CODENAME and VERSION_ID not set in os-release")
    57  	return ""
    58  }
    59  
    60  // SetProto sets the SNAPPackageMetadata field in the Package proto.
    61  func (m *Metadata) SetProto(p *pb.Package) {
    62  	if m == nil {
    63  		return
    64  	}
    65  	if p == nil {
    66  		return
    67  	}
    68  
    69  	p.Metadata = &pb.Package_SnapMetadata{
    70  		SnapMetadata: &pb.SNAPPackageMetadata{
    71  			Name:              m.Name,
    72  			Version:           m.Version,
    73  			Grade:             m.Grade,
    74  			Type:              m.Type,
    75  			Architectures:     m.Architectures,
    76  			OsId:              m.OSID,
    77  			OsVersionCodename: m.OSVersionCodename,
    78  			OsVersionId:       m.OSVersionID,
    79  		},
    80  	}
    81  }
    82  
    83  // ToStruct converts the SNAPPackageMetadata proto to a Metadata struct.
    84  func ToStruct(m *pb.SNAPPackageMetadata) *Metadata {
    85  	if m == nil {
    86  		return nil
    87  	}
    88  
    89  	return &Metadata{
    90  		Name:              m.GetName(),
    91  		Version:           m.GetVersion(),
    92  		Grade:             m.GetGrade(),
    93  		Type:              m.GetType(),
    94  		Architectures:     m.GetArchitectures(),
    95  		OSID:              m.GetOsId(),
    96  		OSVersionCodename: m.GetOsVersionCodename(),
    97  		OSVersionID:       m.GetOsVersionId(),
    98  	}
    99  }