|
| 1 | +package artifact |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/containers/common/pkg/completion" |
| 8 | + "github.com/containers/common/pkg/report" |
| 9 | + "github.com/containers/image/v5/docker/reference" |
| 10 | + "github.com/containers/podman/v5/cmd/podman/common" |
| 11 | + "github.com/containers/podman/v5/cmd/podman/registry" |
| 12 | + "github.com/containers/podman/v5/cmd/podman/validate" |
| 13 | + "github.com/containers/podman/v5/pkg/domain/entities" |
| 14 | + "github.com/docker/go-units" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + ListCmd = &cobra.Command{ |
| 20 | + Use: "ls [options]", |
| 21 | + Aliases: []string{"list"}, |
| 22 | + Short: "List OCI artifacts", |
| 23 | + Long: "List OCI artifacts in local store", |
| 24 | + RunE: list, |
| 25 | + Args: validate.NoArgs, |
| 26 | + ValidArgsFunction: completion.AutocompleteNone, |
| 27 | + Example: `podman artifact ls`, |
| 28 | + } |
| 29 | + listFlag = listFlagType{} |
| 30 | +) |
| 31 | + |
| 32 | +type listFlagType struct { |
| 33 | + format string |
| 34 | +} |
| 35 | + |
| 36 | +type artifactListOutput struct { |
| 37 | + Digest string |
| 38 | + Repository string |
| 39 | + Size string |
| 40 | + Tag string |
| 41 | +} |
| 42 | + |
| 43 | +var ( |
| 44 | + defaultArtifactListOutputFormat = "{{range .}}{{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.Size}}\n{{end -}}" |
| 45 | +) |
| 46 | + |
| 47 | +func init() { |
| 48 | + registry.Commands = append(registry.Commands, registry.CliCommand{ |
| 49 | + Command: ListCmd, |
| 50 | + Parent: artifactCmd, |
| 51 | + }) |
| 52 | + flags := ListCmd.Flags() |
| 53 | + formatFlagName := "format" |
| 54 | + flags.StringVar(&listFlag.format, formatFlagName, defaultArtifactListOutputFormat, "Format volume output using JSON or a Go template") |
| 55 | + _ = ListCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&artifactListOutput{})) |
| 56 | + // TODO When the inspect structure has been defined, we need to uncomment and redirect this. Reminder, this |
| 57 | + // will also need to be reflected in the podman-artifact-inspect man page |
| 58 | + // _ = inspectCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(&machine.InspectInfo{})) |
| 59 | +} |
| 60 | + |
| 61 | +func list(cmd *cobra.Command, _ []string) error { |
| 62 | + reports, err := registry.ImageEngine().ArtifactList(registry.GetContext(), entities.ArtifactListOptions{}) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + return outputTemplate(cmd, reports) |
| 68 | +} |
| 69 | + |
| 70 | +func outputTemplate(cmd *cobra.Command, lrs []*entities.ArtifactListReport) error { |
| 71 | + var err error |
| 72 | + artifacts := make([]artifactListOutput, 0) |
| 73 | + for _, lr := range lrs { |
| 74 | + var ( |
| 75 | + tag string |
| 76 | + ) |
| 77 | + artifactName, err := lr.Artifact.GetName() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + repo, err := reference.Parse(artifactName) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + named, ok := repo.(reference.Named) |
| 86 | + if !ok { |
| 87 | + return fmt.Errorf("%q is an invalid artifact name", artifactName) |
| 88 | + } |
| 89 | + if tagged, ok := named.(reference.Tagged); ok { |
| 90 | + tag = tagged.Tag() |
| 91 | + } |
| 92 | + |
| 93 | + // Note: Right now we only support things that are single manifests |
| 94 | + // We should certainly expand this support for things like arch, etc |
| 95 | + // as we move on |
| 96 | + artifactDigest, err := lr.Artifact.GetDigest() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + // TODO when we default to shorter ids, i would foresee a switch |
| 101 | + // like images that will show the full ids. |
| 102 | + artifacts = append(artifacts, artifactListOutput{ |
| 103 | + Digest: artifactDigest.Encoded(), |
| 104 | + Repository: named.Name(), |
| 105 | + Size: units.HumanSize(float64(lr.Artifact.TotalSizeBytes())), |
| 106 | + Tag: tag, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + headers := report.Headers(artifactListOutput{}, map[string]string{ |
| 111 | + "REPOSITORY": "REPOSITORY", |
| 112 | + "Tag": "TAG", |
| 113 | + "Size": "SIZE", |
| 114 | + "Digest": "DIGEST", |
| 115 | + }) |
| 116 | + |
| 117 | + rpt := report.New(os.Stdout, cmd.Name()) |
| 118 | + defer rpt.Flush() |
| 119 | + |
| 120 | + switch { |
| 121 | + case cmd.Flag("format").Changed: |
| 122 | + rpt, err = rpt.Parse(report.OriginUser, listFlag.format) |
| 123 | + default: |
| 124 | + rpt, err = rpt.Parse(report.OriginPodman, listFlag.format) |
| 125 | + } |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if rpt.RenderHeaders { |
| 131 | + if err := rpt.Execute(headers); err != nil { |
| 132 | + return fmt.Errorf("failed to write report column headers: %w", err) |
| 133 | + } |
| 134 | + } |
| 135 | + return rpt.Execute(artifacts) |
| 136 | +} |
0 commit comments