Add list artifacts command
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
70
internal/oci/list.go
Normal file
70
internal/oci/list.go
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright 2022 The Flux authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package oci
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
gcrv1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
)
|
||||
|
||||
// List fetches the tags and their manifests for a given OCI repository.
|
||||
func List(ctx context.Context, url string) ([]Metadata, error) {
|
||||
metas := make([]Metadata, 0)
|
||||
tags, err := crane.ListTags(url, craneOptions(ctx)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("listing tags failed: %w", err)
|
||||
}
|
||||
|
||||
sort.Slice(tags, func(i, j int) bool { return tags[i] > tags[j] })
|
||||
|
||||
for _, tag := range tags {
|
||||
// exclude cosign signatures
|
||||
if strings.HasSuffix(tag, ".sig") {
|
||||
continue
|
||||
}
|
||||
|
||||
meta := Metadata{
|
||||
URL: fmt.Sprintf("%s:%s", url, tag),
|
||||
}
|
||||
|
||||
manifestJSON, err := crane.Manifest(meta.URL, craneOptions(ctx)...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetching manifest failed: %w", err)
|
||||
}
|
||||
|
||||
manifest, err := gcrv1.ParseManifest(bytes.NewReader(manifestJSON))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parsing manifest failed: %w", err)
|
||||
}
|
||||
|
||||
meta.Digest = manifest.Config.Digest.String()
|
||||
if m, err := MetadataFromAnnotations(manifest.Annotations); err == nil {
|
||||
meta.Revision = m.Revision
|
||||
meta.Source = m.Source
|
||||
}
|
||||
|
||||
metas = append(metas, meta)
|
||||
}
|
||||
|
||||
return metas, nil
|
||||
}
|
||||
@@ -29,6 +29,7 @@ type Metadata struct {
|
||||
Source string `json:"source_url"`
|
||||
Revision string `json:"source_revision"`
|
||||
Digest string `json:"digest"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func (m *Metadata) ToAnnotations() map[string]string {
|
||||
@@ -40,7 +41,7 @@ func (m *Metadata) ToAnnotations() map[string]string {
|
||||
return annotations
|
||||
}
|
||||
|
||||
func GetMetadata(annotations map[string]string) (*Metadata, error) {
|
||||
func MetadataFromAnnotations(annotations map[string]string) (*Metadata, error) {
|
||||
source, ok := annotations[SourceAnnotation]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("'%s' annotation not found", SourceAnnotation)
|
||||
|
||||
@@ -47,7 +47,7 @@ func Pull(ctx context.Context, url, outDir string) (*Metadata, error) {
|
||||
return nil, fmt.Errorf("parsing manifest failed: %w", err)
|
||||
}
|
||||
|
||||
meta, err := GetMetadata(manifest.Annotations)
|
||||
meta, err := MetadataFromAnnotations(manifest.Annotations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user