github.com/quay/claircore@v1.5.28/aws/internal/alas/repomd.go (about)

     1  // Copyright 2019 RedHat
     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 alas
    16  
    17  import (
    18  	"errors"
    19  	"net/url"
    20  )
    21  
    22  type RepoType string
    23  
    24  var ErrRepoNotFound = errors.New("Repo not found")
    25  
    26  const (
    27  	PrimaryDB  RepoType = "primary_db"
    28  	OtherDB    RepoType = "other_db"
    29  	GroupGZ    RepoType = "group_gz"
    30  	Group      RepoType = "group"
    31  	FileLists  RepoType = "filelists_db"
    32  	UpdateInfo RepoType = "updateinfo"
    33  )
    34  
    35  type RepoMD struct {
    36  	XMLNS    string `xml:"xmlns,attr"`
    37  	XMLRPM   string `xml:"xmlns rpm,attr"`
    38  	Revision int    `xml:"revision"`
    39  	RepoList []Repo `xml:"data"`
    40  }
    41  
    42  type Repo struct {
    43  	Type            string   `xml:"type,attr"`
    44  	Checksum        Checksum `xml:"checksum"`
    45  	OpenChecksum    Checksum `xml:"open-checksum"`
    46  	Location        Location `xml:"location"`
    47  	Timestamp       int      `xml:"timestamp"`
    48  	DatabaseVersion int      `xml:"database_version"`
    49  	Size            int      `xml:"size"`
    50  	OpenSize        int      `xml:"open-size"`
    51  }
    52  
    53  type Checksum struct {
    54  	Sum  string `xml:",chardata"`
    55  	Type string `xml:"type,attr"`
    56  }
    57  
    58  type Location struct {
    59  	Href string `xml:"href,attr"`
    60  }
    61  
    62  // Repo returns a Repo struct per the specified RepoType.
    63  // If a mirror url is provided a fully qualified Repo.Location.Href is returned
    64  // A ErrRepoNotFound error is returned if the RepoType cannot be located.
    65  func (md *RepoMD) Repo(t RepoType, mirror string) (*Repo, error) {
    66  	for i := range md.RepoList {
    67  		repo := &md.RepoList[i]
    68  		if repo.Type != string(t) {
    69  			continue
    70  		}
    71  		if mirror != "" {
    72  			u, err := url.Parse(mirror)
    73  			if err != nil {
    74  				return nil, err
    75  			}
    76  			href, err := u.Parse(repo.Location.Href)
    77  			if err != nil {
    78  				return nil, err
    79  			}
    80  			repo.Location.Href = href.String()
    81  		}
    82  		return repo, nil
    83  	}
    84  	return nil, ErrRepoNotFound
    85  }