github.com/quay/claircore@v1.5.28/rhel/internal/pulp/manifest.go (about)

     1  // Package pulp is for reading a Pulp manifest.
     2  package pulp
     3  
     4  import (
     5  	"encoding/csv"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"io"
     9  	"strconv"
    10  )
    11  
    12  // A Manifest is a series of Entries indicating where to find resources in the
    13  // pulp repository.
    14  type Manifest []Entry
    15  
    16  // Entry is an entry in a pulp manifest.
    17  type Entry struct {
    18  	// This path should be parsed in the context of the manifest's URL.
    19  	Path     string
    20  	Checksum []byte
    21  	Size     int64
    22  }
    23  
    24  // Load populates the manifest from the io.Reader.
    25  //
    26  // The data is expected in the manifest CSV format.
    27  func (m *Manifest) Load(r io.Reader) error {
    28  	rd := csv.NewReader(r)
    29  	rd.FieldsPerRecord = 3
    30  	rd.ReuseRecord = true
    31  	l := 0
    32  	rec, err := rd.Read()
    33  	for ; err == nil; rec, err = rd.Read() {
    34  		var err error
    35  		e := Entry{}
    36  		e.Path += rec[0] // This += should result in us getting a copy.
    37  		e.Checksum, err = hex.DecodeString(rec[1])
    38  		if err != nil {
    39  			return fmt.Errorf("line %d: %w", l, err)
    40  		}
    41  		e.Size, err = strconv.ParseInt(rec[2], 10, 64)
    42  		if err != nil {
    43  			return fmt.Errorf("line %d: %w", l, err)
    44  		}
    45  		*m = append(*m, e)
    46  		l++
    47  	}
    48  	if err != io.EOF {
    49  		return err
    50  	}
    51  	return nil
    52  }