github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/cos/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 COS 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 COS package.
    25  type Metadata struct {
    26  	Name          string
    27  	Version       string
    28  	Category      string
    29  	OSVersion     string
    30  	OSVersionID   string
    31  	EbuildVersion string
    32  }
    33  
    34  // ToDistro extracts the OS distro from the metadata.
    35  func (m *Metadata) ToDistro() string {
    36  	if m.OSVersionID != "" {
    37  		return "cos-" + m.OSVersionID
    38  	}
    39  
    40  	if m.OSVersion != "" {
    41  		log.Warnf("VERSION_ID not set in os-release, fallback to VERSION")
    42  		return "cos-" + m.OSVersion
    43  	}
    44  	log.Errorf("VERSION and VERSION_ID not set in os-release")
    45  	return ""
    46  }
    47  
    48  // SetProto sets the COSPackageMetadata field in the Package proto.
    49  func (m *Metadata) SetProto(p *pb.Package) {
    50  	if m == nil {
    51  		return
    52  	}
    53  	if p == nil {
    54  		return
    55  	}
    56  
    57  	p.Metadata = &pb.Package_CosMetadata{
    58  		CosMetadata: &pb.COSPackageMetadata{
    59  			Name:          m.Name,
    60  			Version:       m.Version,
    61  			Category:      m.Category,
    62  			OsVersion:     m.OSVersion,
    63  			OsVersionId:   m.OSVersionID,
    64  			EbuildVersion: m.EbuildVersion,
    65  		},
    66  	}
    67  }
    68  
    69  // ToStruct converts the COSPackageMetadata proto to a Metadata struct.
    70  func ToStruct(m *pb.COSPackageMetadata) *Metadata {
    71  	if m == nil {
    72  		return nil
    73  	}
    74  
    75  	return &Metadata{
    76  		Name:          m.GetName(),
    77  		Version:       m.GetVersion(),
    78  		Category:      m.GetCategory(),
    79  		OSVersion:     m.GetOsVersion(),
    80  		OSVersionID:   m.GetOsVersionId(),
    81  		EbuildVersion: m.GetEbuildVersion(),
    82  	}
    83  }