github.com/quay/claircore@v1.5.28/alpine/release.go (about)

     1  package alpine
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/quay/claircore"
     8  )
     9  
    10  // Alpine linux has patch releases but their security database
    11  // aggregates security information by major release. We choose
    12  // to normalize detected distributions into major.minor releases and
    13  // parse vulnerabilities into major.minor releases
    14  
    15  // release represents a particular release of the Alpine Linux distribution
    16  type release interface {
    17  	Distribution() *claircore.Distribution
    18  	String() string
    19  }
    20  
    21  var (
    22  	_ release = (*edgeRelease)(nil)
    23  	_ release = (*stableRelease)(nil)
    24  )
    25  
    26  // edgeRelease is the Alpine Linux edge distribution.
    27  type edgeRelease struct{}
    28  
    29  // stableRelease is a particular stable release of the Alpine Linux distribution.
    30  type stableRelease [2]int
    31  
    32  // Common os-release fields applicable for *claircore.Distribution usage.
    33  const (
    34  	distName = "Alpine Linux"
    35  	distID   = "alpine"
    36  )
    37  
    38  var (
    39  	relMap sync.Map
    40  
    41  	edgeDist = &claircore.Distribution{
    42  		Name:       distName,
    43  		DID:        distID,
    44  		VersionID:  edgeVersion,
    45  		PrettyName: edgePrettyName,
    46  	}
    47  )
    48  
    49  func (edgeRelease) Distribution() *claircore.Distribution {
    50  	return edgeDist
    51  }
    52  
    53  func (edgeRelease) String() string {
    54  	return edgeVersion
    55  }
    56  
    57  func (r stableRelease) Distribution() *claircore.Distribution {
    58  	// Dirty hack to keyify the release structure.
    59  	k := int64(r[0]<<32) | int64(r[1])
    60  	v, ok := relMap.Load(k)
    61  	if !ok {
    62  		v, _ = relMap.LoadOrStore(k, &claircore.Distribution{
    63  			Name:       distName,
    64  			DID:        distID,
    65  			VersionID:  fmt.Sprintf("%d.%d", r[0], r[1]),
    66  			PrettyName: fmt.Sprintf("Alpine Linux v%d.%d", r[0], r[1]),
    67  		})
    68  	}
    69  	return v.(*claircore.Distribution)
    70  }
    71  
    72  func (r stableRelease) String() string { return fmt.Sprintf("v%d.%d", r[0], r[1]) }