mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 20:38:08 +08:00
refactor: remove pkg/extensions/search/common and move the code to the appropriate packages (#1358)
Signed-off-by: Nicol Draghici <idraghic@cisco.com>
This commit is contained in:
@@ -119,4 +119,9 @@ func TestCommon(t *testing.T) {
|
||||
resultPtr, baseURL+"/v2/", ispec.MediaTypeImageManifest, log.NewLogger("", ""))
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Test image dir and digest", t, func() {
|
||||
repo, digest := common.GetImageDirAndDigest("image")
|
||||
So(repo, ShouldResemble, "image")
|
||||
So(digest, ShouldResemble, "")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type RepoInfo struct {
|
||||
Summary RepoSummary
|
||||
ImageSummaries []ImageSummary `json:"images"`
|
||||
}
|
||||
|
||||
type RepoSummary struct {
|
||||
Name string `json:"name"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
Size string `json:"size"`
|
||||
Platforms []Platform `json:"platforms"`
|
||||
Vendors []string `json:"vendors"`
|
||||
NewestImage ImageSummary `json:"newestImage"`
|
||||
}
|
||||
|
||||
type ImageSummary struct {
|
||||
RepoName string `json:"repoName"`
|
||||
Tag string `json:"tag"`
|
||||
Digest string `json:"digest"`
|
||||
MediaType string `json:"mediaType"`
|
||||
Manifests []ManifestSummary `json:"manifests"`
|
||||
Size string `json:"size"`
|
||||
DownloadCount int `json:"downloadCount"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
Description string `json:"description"`
|
||||
IsSigned bool `json:"isSigned"`
|
||||
Licenses string `json:"licenses"`
|
||||
Labels string `json:"labels"`
|
||||
Title string `json:"title"`
|
||||
Source string `json:"source"`
|
||||
Documentation string `json:"documentation"`
|
||||
Authors string `json:"authors"`
|
||||
Vendor string `json:"vendor"`
|
||||
Vulnerabilities ImageVulnerabilitySummary `json:"vulnerabilities"`
|
||||
Referrers []Referrer `json:"referrers"`
|
||||
}
|
||||
|
||||
type ManifestSummary struct {
|
||||
Digest string `json:"digest"`
|
||||
ConfigDigest string `json:"configDigest"`
|
||||
LastUpdated time.Time `json:"lastUpdated"`
|
||||
Size string `json:"size"`
|
||||
Platform Platform `json:"platform"`
|
||||
DownloadCount int `json:"downloadCount"`
|
||||
Layers []LayerSummary `json:"layers"`
|
||||
History []LayerHistory `json:"history"`
|
||||
Vulnerabilities ImageVulnerabilitySummary `json:"vulnerabilities"`
|
||||
}
|
||||
|
||||
type Platform struct {
|
||||
Os string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
}
|
||||
|
||||
type ErrorGraphQL struct {
|
||||
Message string `json:"message"`
|
||||
Path []string `json:"path"`
|
||||
}
|
||||
|
||||
type ImageVulnerabilitySummary struct {
|
||||
MaxSeverity string `json:"maxSeverity"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type LayerSummary struct {
|
||||
Size string `json:"size"`
|
||||
Digest string `json:"digest"`
|
||||
Score int `json:"score"`
|
||||
}
|
||||
|
||||
type LayerHistory struct {
|
||||
Layer LayerSummary `json:"layer"`
|
||||
HistoryDescription HistoryDescription `json:"historyDescription"`
|
||||
}
|
||||
|
||||
type HistoryDescription struct {
|
||||
Created time.Time `json:"created"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
Author string `json:"author"`
|
||||
Comment string `json:"comment"`
|
||||
EmptyLayer bool `json:"emptyLayer"`
|
||||
}
|
||||
|
||||
type Referrer struct {
|
||||
MediaType string `json:"mediatype"`
|
||||
ArtifactType string `json:"artifacttype"`
|
||||
Size int `json:"size"`
|
||||
Digest string `json:"digest"`
|
||||
Annotations []Annotation `json:"annotations"`
|
||||
}
|
||||
|
||||
type Annotation struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
func GetImageDirAndTag(imageName string) (string, string) {
|
||||
var imageDir string
|
||||
|
||||
var imageTag string
|
||||
|
||||
if strings.Contains(imageName, ":") {
|
||||
imageDir, imageTag, _ = strings.Cut(imageName, ":")
|
||||
} else {
|
||||
imageDir = imageName
|
||||
}
|
||||
|
||||
return imageDir, imageTag
|
||||
}
|
||||
|
||||
func GetImageDirAndDigest(imageName string) (string, string) {
|
||||
var imageDir string
|
||||
|
||||
var imageDigest string
|
||||
|
||||
if strings.Contains(imageName, "@") {
|
||||
imageDir, imageDigest, _ = strings.Cut(imageName, "@")
|
||||
} else {
|
||||
imageDir = imageName
|
||||
}
|
||||
|
||||
return imageDir, imageDigest
|
||||
}
|
||||
|
||||
// GetImageDirAndReference returns the repo, digest and isTag.
|
||||
func GetImageDirAndReference(imageName string) (string, string, bool) {
|
||||
if strings.Contains(imageName, "@") {
|
||||
repo, digest := GetImageDirAndDigest(imageName)
|
||||
|
||||
return repo, digest, false
|
||||
}
|
||||
|
||||
repo, tag := GetImageDirAndTag(imageName)
|
||||
|
||||
return repo, tag, true
|
||||
}
|
||||
|
||||
// GetImageLastUpdated This method will return last updated timestamp.
|
||||
// The Created timestamp is used, but if it is missing, look at the
|
||||
// history field and, if provided, return the timestamp of last entry in history.
|
||||
func GetImageLastUpdated(imageInfo ispec.Image) time.Time {
|
||||
timeStamp := imageInfo.Created
|
||||
|
||||
if timeStamp != nil && !timeStamp.IsZero() {
|
||||
return *timeStamp
|
||||
}
|
||||
|
||||
if len(imageInfo.History) > 0 {
|
||||
timeStamp = imageInfo.History[len(imageInfo.History)-1].Created
|
||||
}
|
||||
|
||||
if timeStamp == nil {
|
||||
timeStamp = &time.Time{}
|
||||
}
|
||||
|
||||
return *timeStamp
|
||||
}
|
||||
Reference in New Issue
Block a user