mirror of https://github.com/fluxcd/flux2.git
Add list artifacts command
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>pull/2971/head
parent
2d8db4f20d
commit
2f35367a7f
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List artifacts",
|
||||||
|
Long: "The list command is used for printing the OCI artifacts metadata.",
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(listCmd)
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
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 main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/fluxcd/flux2/internal/oci"
|
||||||
|
"github.com/fluxcd/flux2/pkg/printers"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listArtifactsCmd = &cobra.Command{
|
||||||
|
Use: "artifacts",
|
||||||
|
Short: "list artifacts",
|
||||||
|
Long: `The list command fetches the tags and their metadata from a remote OCI repository.
|
||||||
|
The list command uses the credentials from '~/.docker/config.json'.`,
|
||||||
|
Example: `# list the artifacts stored in an OCI repository
|
||||||
|
flux list artifact ghcr.io/org/manifests/app
|
||||||
|
`,
|
||||||
|
RunE: listArtifactsCmdRun,
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
listCmd.AddCommand(listArtifactsCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func listArtifactsCmdRun(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < 1 {
|
||||||
|
return fmt.Errorf("artifact repository is required")
|
||||||
|
}
|
||||||
|
url := args[0]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
metas, err := oci.List(ctx, url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var rows [][]string
|
||||||
|
for _, meta := range metas {
|
||||||
|
rows = append(rows, []string{meta.URL, meta.Digest, meta.Source, meta.Revision})
|
||||||
|
}
|
||||||
|
|
||||||
|
err = printers.TablePrinter([]string{"artifact", "digest", "source", "revision"}).Print(cmd.OutOrStdout(), rows)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue