github.com/quay/claircore@v1.5.28/indexer/versionedscanner.go (about)

     1  package indexer
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  )
     7  
     8  const (
     9  	Package = "package"
    10  )
    11  
    12  // VersionedScanner can be embedded into specific scanner types. This allows for
    13  // methods and functions which only need to compare names and versions of
    14  // scanners not to require each scanner type as an argument.
    15  type VersionedScanner interface {
    16  	// unique name of the distribution scanner.
    17  	Name() string
    18  	// version of this scanner. this information will be persisted with the scan.
    19  	Version() string
    20  	// the kind of scanner. currently only package is implemented
    21  	Kind() string
    22  }
    23  
    24  // ConfigDeserializer can be thought of as an Unmarshal function with the byte
    25  // slice provided.
    26  //
    27  // This will typically be something like (*json.Decoder).Decode.
    28  type ConfigDeserializer func(interface{}) error
    29  
    30  // RPCScanner is an interface scanners can implement to receive configuration
    31  // and denote that they expect to be able to talk to the network at run time.
    32  type RPCScanner interface {
    33  	Configure(context.Context, ConfigDeserializer, *http.Client) error
    34  }
    35  
    36  // ConfigurableScanner is an interface scanners can implement to receive
    37  // configuration.
    38  type ConfigurableScanner interface {
    39  	Configure(context.Context, ConfigDeserializer) error
    40  }
    41  
    42  // VersionedScanners implements a list with construction methods
    43  // not concurrency safe
    44  type VersionedScanners []VersionedScanner
    45  
    46  func (vs *VersionedScanners) PStoVS(scnrs []PackageScanner) {
    47  	n := len(scnrs)
    48  	if cap(*vs) < n {
    49  		*vs = make([]VersionedScanner, n)
    50  	} else {
    51  		*vs = (*vs)[:n]
    52  	}
    53  	for i := 0; i < n; i++ {
    54  		(*vs)[i] = scnrs[i]
    55  	}
    56  }
    57  
    58  // VStoPS returns an array of PackageScanners
    59  func (vs VersionedScanners) VStoPS() []PackageScanner {
    60  	out := make([]PackageScanner, len(vs))
    61  	for _, vscnr := range vs {
    62  		out = append(out, vscnr.(PackageScanner))
    63  	}
    64  	return out
    65  }
    66  
    67  // DStoVS takes an array of DistributionScanners and appends VersionedScanners with
    68  // VersionScanner types.
    69  func (vs *VersionedScanners) DStoVS(scnrs []DistributionScanner) {
    70  	n := len(scnrs)
    71  	if cap(*vs) < n {
    72  		*vs = make([]VersionedScanner, n)
    73  	} else {
    74  		*vs = (*vs)[:n]
    75  	}
    76  	for i := 0; i < n; i++ {
    77  		(*vs)[i] = scnrs[i]
    78  	}
    79  }
    80  
    81  // VStoDS returns an array of DistributionScanners
    82  func (vs VersionedScanners) VStoDS() []DistributionScanner {
    83  	out := make([]DistributionScanner, len(vs))
    84  	for _, vscnr := range vs {
    85  		out = append(out, vscnr.(DistributionScanner))
    86  	}
    87  	return out
    88  }
    89  
    90  // RStoVS takes an array of RepositoryScanners and appends VersionedScanners with
    91  // VersionScanner types.
    92  func (vs *VersionedScanners) RStoVS(scnrs []RepositoryScanner) {
    93  	n := len(scnrs)
    94  	if cap(*vs) < n {
    95  		*vs = make([]VersionedScanner, n)
    96  	} else {
    97  		*vs = (*vs)[:n]
    98  	}
    99  	for i := 0; i < n; i++ {
   100  		(*vs)[i] = scnrs[i]
   101  	}
   102  }
   103  
   104  // VStoRS returns an array of RepositoryScanners
   105  func (vs VersionedScanners) VStoRS() []RepositoryScanner {
   106  	out := make([]RepositoryScanner, len(vs))
   107  	for _, vscnr := range vs {
   108  		out = append(out, vscnr.(RepositoryScanner))
   109  	}
   110  	return out
   111  }
   112  
   113  // FStoVS takes an array of FileScanners and appends VersionedScanners with
   114  // VersionScanner types.
   115  func (vs *VersionedScanners) FStoVS(scnrs []FileScanner) {
   116  	n := len(scnrs)
   117  	if cap(*vs) < n {
   118  		*vs = make([]VersionedScanner, n)
   119  	} else {
   120  		*vs = (*vs)[:n]
   121  	}
   122  	for i := 0; i < n; i++ {
   123  		(*vs)[i] = scnrs[i]
   124  	}
   125  }
   126  
   127  // MergeVS merges lists of scanners into a single list of VersionedScanner types
   128  func MergeVS(pscnr []PackageScanner, dscnr []DistributionScanner, rscnr []RepositoryScanner, fscnr []FileScanner) VersionedScanners {
   129  	out := make([]VersionedScanner, 0)
   130  	for _, ps := range pscnr {
   131  		out = append(out, VersionedScanner(ps))
   132  	}
   133  	for _, ds := range dscnr {
   134  		out = append(out, VersionedScanner(ds))
   135  	}
   136  	for _, rs := range rscnr {
   137  		out = append(out, VersionedScanner(rs))
   138  	}
   139  	for _, fs := range fscnr {
   140  		out = append(out, VersionedScanner(fs))
   141  	}
   142  	return out
   143  }