github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/internal/remote/remote.go (about) 1 package remote 2 3 import ( 4 "encoding/json" 5 6 "github.com/criteo/command-launcher/internal/command" 7 ) 8 9 type PackageInfo struct { 10 Name string `json:"name"` 11 Version string `json:"version"` 12 Url string `json:"url"` 13 Checksum string `json:"checksum"` 14 StartPartition uint8 `json:"startPartition"` 15 EndPartition uint8 `json:"endPartition"` 16 } 17 18 // Custom unmarshal method to deal with default StartPartition and EndPartition 19 func (t *PackageInfo) UnmarshalJSON(data []byte) error { 20 type packageInfoAlias PackageInfo 21 pkgInfo := &packageInfoAlias{ 22 StartPartition: 0, 23 EndPartition: 9, 24 } 25 26 err := json.Unmarshal(data, pkgInfo) 27 if err != nil { 28 return err 29 } 30 31 *t = PackageInfo(*pkgInfo) 32 return nil 33 } 34 35 // PackagesByVersion type help us to sort the packages by their version 36 type PackagesByVersion []PackageInfo 37 38 func (a PackagesByVersion) Less(i, j int) bool { 39 var l defaultVersion 40 var r defaultVersion 41 42 err := ParseVersion(a[i].Version, &l) 43 if err != nil { // wrong format version is considered smaller 44 return true 45 } 46 err = ParseVersion(a[j].Version, &r) 47 if err != nil { // wrong fromat version is considered smaller 48 return false 49 } 50 51 return Less(l, r) 52 } 53 func (a PackagesByVersion) Len() int { return len(a) } 54 func (a PackagesByVersion) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 55 56 // RemoteRepository represents a group of commands packages, and the current version of them 57 // to be used by cdt 58 type RemoteRepository interface { 59 // Fetch the remote repository metadata 60 Fetch() error 61 62 // Get informations of all packages 63 All() ([]PackageInfo, error) 64 65 // Get all available package names 66 PackageNames() ([]string, error) 67 68 // Get the available versions of a given package 69 Versions(packageName string) ([]string, error) 70 71 // Get the latest available version of a given package 72 LatestVersion(packageName string) (string, error) 73 74 // Get the latest version of a given package following a filter function 75 // this function will pass the package info to the filter from the latest version to the oldest one 76 // until the filter returns true 77 QueryLatestVersion(packageName string, filter PackageInfoFilterFunc) (string, error) 78 79 // Get the package info of the latest available version 80 LatestPackageInfo(packageName string) (*PackageInfo, error) 81 82 // Get the latest package info according to a filter function 83 // this function will pass the package to the filter from the latest version to the oldest one 84 // until the filter returns true 85 QueryLatestPackageInfo(packageName string, filter PackageInfoFilterFunc) (*PackageInfo, error) 86 87 // Download the package of the command with specific version 88 Package(packageName string, packageVersion string) (command.Package, error) 89 90 // get the package information of a given version 91 PackageInfo(packageName string, packageVersion string) (*PackageInfo, error) 92 93 // Verify package: support two verifications: checksum and signature 94 Verify(pkg command.Package, verifyChecksum, verifySignature bool) (bool, error) 95 } 96 97 type PackageInfoFilterFunc func(pkgInfo *PackageInfo) bool