Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hide status output when using --output - #1506

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions cmd/oras/root/manifest/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type createOptions struct {
sources []string
extraRefs []string
outputPath string
stdoutput bool
}

func createCmd() *cobra.Command {
Expand Down Expand Up @@ -87,6 +88,7 @@ Example - Create an index and output the index to stdout, auto push will be disa
opts.RawReference = refs[0]
opts.extraRefs = refs[1:]
opts.sources = args[1:]
opts.stdoutput = (opts.outputPath == "-")
return option.Parse(cmd, &opts)
},
Aliases: []string{"pack"},
Expand Down Expand Up @@ -121,13 +123,12 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
return err
}
desc := content.NewDescriptorFromBytes(ocispec.MediaTypeImageIndex, indexBytes)
opts.Println(status.IndexPromptPacked, descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex)
printCreateStatus(opts.stdoutput, opts.Printer, status.IndexPromptPacked, descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex)

switch opts.outputPath {
case "":
err = pushIndex(ctx, target, desc, indexBytes, opts.Reference, opts.extraRefs, opts.AnnotatedReference(), opts.Printer)
case "-":
opts.Println("Digest:", desc.Digest)
err = opts.Output(os.Stdout, indexBytes)
default:
opts.Println("Digest:", desc.Digest)
Expand All @@ -139,15 +140,15 @@ func createIndex(cmd *cobra.Command, opts createOptions) error {
func fetchSourceManifests(ctx context.Context, target oras.ReadOnlyTarget, opts createOptions) ([]ocispec.Descriptor, error) {
resolved := []ocispec.Descriptor{}
for _, source := range opts.sources {
opts.Println(status.IndexPromptFetching, source)
printCreateStatus(opts.stdoutput, opts.Printer, status.IndexPromptFetching, source)
desc, content, err := oras.FetchBytes(ctx, target, source, oras.DefaultFetchBytesOptions)
if err != nil {
return nil, fmt.Errorf("could not find the manifest %s: %w", source, err)
}
if !descriptor.IsManifest(desc) {
return nil, fmt.Errorf("%s is not a manifest", source)
}
opts.Println(status.IndexPromptFetched, source)
printCreateStatus(opts.stdoutput, opts.Printer, status.IndexPromptFetched, source)
desc = descriptor.Plain(desc)
if descriptor.IsImageManifest(desc) {
desc.Platform, err = getPlatform(ctx, target, content)
Expand Down Expand Up @@ -204,3 +205,9 @@ func pushIndex(ctx context.Context, target oras.Target, desc ocispec.Descriptor,
}
return printer.Println("Digest:", desc.Digest)
}

func printCreateStatus(stdoutput bool, printer *output.Printer, vals ...any) {
if !stdoutput {
printer.Println(vals...)
}
}
32 changes: 18 additions & 14 deletions cmd/oras/root/manifest/index/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type updateOptions struct {
removeArguments []string
tags []string
outputPath string
stdoutput bool
}

func updateCmd() *cobra.Command {
Expand Down Expand Up @@ -85,6 +86,7 @@ Example - Update an index and output the index to stdout, auto push will be disa
return fmt.Errorf("remove: %s is not a digest", manifestRef)
}
}
opts.stdoutput = (opts.outputPath == "-")
return option.Parse(cmd, &opts)
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -138,13 +140,12 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
}
desc := content.NewDescriptorFromBytes(index.MediaType, indexBytes)

printUpdateStatus(status.IndexPromptUpdated, string(desc.Digest), "", opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptUpdated, string(desc.Digest), "", opts.Printer)
path := getPushPath(opts.RawReference, opts.Type, opts.Reference, opts.Path)
switch opts.outputPath {
case "":
err = pushIndex(ctx, target, desc, indexBytes, opts.Reference, opts.tags, path, opts.Printer)
case "-":
opts.Println("Digest:", desc.Digest)
err = opts.Output(os.Stdout, indexBytes)
default:
opts.Println("Digest:", desc.Digest)
Expand All @@ -154,15 +155,15 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
}

func fetchIndex(ctx context.Context, target oras.ReadOnlyTarget, opts updateOptions) (ocispec.Index, error) {
printUpdateStatus(status.IndexPromptFetching, opts.Reference, "", opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetching, opts.Reference, "", opts.Printer)
desc, content, err := oras.FetchBytes(ctx, target, opts.Reference, oras.DefaultFetchBytesOptions)
if err != nil {
return ocispec.Index{}, fmt.Errorf("could not find the index %s: %w", opts.Reference, err)
}
if !descriptor.IsIndex(desc) {
return ocispec.Index{}, fmt.Errorf("%s is not an index", opts.Reference)
}
printUpdateStatus(status.IndexPromptFetched, opts.Reference, string(desc.Digest), opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetched, opts.Reference, string(desc.Digest), opts.Printer)
var index ocispec.Index
if err := json.Unmarshal(content, &index); err != nil {
return ocispec.Index{}, err
Expand All @@ -172,44 +173,44 @@ func fetchIndex(ctx context.Context, target oras.ReadOnlyTarget, opts updateOpti

func addManifests(ctx context.Context, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, opts updateOptions) ([]ocispec.Descriptor, error) {
for _, manifestRef := range opts.addArguments {
printUpdateStatus(status.IndexPromptFetching, manifestRef, "", opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetching, manifestRef, "", opts.Printer)
desc, content, err := oras.FetchBytes(ctx, target, manifestRef, oras.DefaultFetchBytesOptions)
if err != nil {
return nil, fmt.Errorf("could not find the manifest %s: %w", manifestRef, err)
}
if !descriptor.IsManifest(desc) {
return nil, fmt.Errorf("%s is not a manifest", manifestRef)
}
printUpdateStatus(status.IndexPromptFetched, manifestRef, string(desc.Digest), opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetched, manifestRef, string(desc.Digest), opts.Printer)
if descriptor.IsImageManifest(desc) {
desc.Platform, err = getPlatform(ctx, target, content)
if err != nil {
return nil, err
}
}
manifests = append(manifests, desc)
printUpdateStatus(status.IndexPromptAdded, manifestRef, string(desc.Digest), opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptAdded, manifestRef, string(desc.Digest), opts.Printer)
}
return manifests, nil
}

func mergeIndexes(ctx context.Context, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, opts updateOptions) ([]ocispec.Descriptor, error) {
for _, indexRef := range opts.mergeArguments {
printUpdateStatus(status.IndexPromptFetching, indexRef, "", opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetching, indexRef, "", opts.Printer)
desc, content, err := oras.FetchBytes(ctx, target, indexRef, oras.DefaultFetchBytesOptions)
if err != nil {
return nil, fmt.Errorf("could not find the index %s: %w", indexRef, err)
}
if !descriptor.IsIndex(desc) {
return nil, fmt.Errorf("%s is not an index", indexRef)
}
printUpdateStatus(status.IndexPromptFetched, indexRef, string(desc.Digest), opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptFetched, indexRef, string(desc.Digest), opts.Printer)
var index ocispec.Index
if err := json.Unmarshal(content, &index); err != nil {
return nil, err
}
manifests = append(manifests, index.Manifests...)
printUpdateStatus(status.IndexPromptMerged, indexRef, string(desc.Digest), opts.Printer)
printUpdateStatus(opts.stdoutput, status.IndexPromptMerged, indexRef, string(desc.Digest), opts.Printer)
}
return manifests, nil
}
Expand All @@ -220,10 +221,10 @@ func removeManifests(ctx context.Context, manifests []ocispec.Descriptor, target
for _, manifestRef := range opts.removeArguments {
digestToRemove[digest.Digest(manifestRef)] = false
}
return doRemoveManifests(manifests, digestToRemove, opts.Printer, opts.Reference)
return doRemoveManifests(manifests, digestToRemove, opts.Printer, opts.stdoutput, opts.Reference)
}

func doRemoveManifests(originalManifests []ocispec.Descriptor, digestToRemove map[digest.Digest]bool, printer *output.Printer, indexRef string) ([]ocispec.Descriptor, error) {
func doRemoveManifests(originalManifests []ocispec.Descriptor, digestToRemove map[digest.Digest]bool, printer *output.Printer, stdoutput bool, indexRef string) ([]ocispec.Descriptor, error) {
manifests := []ocispec.Descriptor{}
for _, m := range originalManifests {
if _, exists := digestToRemove[m.Digest]; exists {
Expand All @@ -236,7 +237,7 @@ func doRemoveManifests(originalManifests []ocispec.Descriptor, digestToRemove ma
if !removed {
return nil, fmt.Errorf("%s does not exist in the index %s", digest, indexRef)
}
printUpdateStatus(status.IndexPromptRemoved, string(digest), "", printer)
printUpdateStatus(stdoutput, status.IndexPromptRemoved, string(digest), "", printer)
}
return manifests, nil
}
Expand All @@ -245,7 +246,10 @@ func updateFlagsUsed(flags *pflag.FlagSet) bool {
return flags.Changed("add") || flags.Changed("remove") || flags.Changed("merge")
}

func printUpdateStatus(verb string, reference string, resolvedDigest string, printer *output.Printer) {
func printUpdateStatus(stdoutput bool, verb string, reference string, resolvedDigest string, printer *output.Printer) {
if stdoutput {
return
}
if resolvedDigest == "" || contentutil.IsDigest(reference) {
printer.Println(verb, reference)
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/manifest/index/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func Test_doRemoveManifests(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := doRemoveManifests(tt.manifests, tt.digestSet, tt.printer, tt.indexRef)
got, err := doRemoveManifests(tt.manifests, tt.digestSet, tt.printer, false, tt.indexRef)
if (err != nil) != tt.wantErr {
t.Errorf("removeManifestsFromIndex() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/suite/command/manifest_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var _ = Describe("1.1 registry users:", func() {
testRepo := indexTestRepo("create", "output-to-stdout")
CopyZOTRepo(ImageRepo, testRepo)
ORAS("manifest", "index", "create", RegistryRef(ZOTHost, testRepo, ""), string(multi_arch.LinuxAMD64.Digest),
"--output", "-").MatchKeyWords(multi_arch.OutputIndex).Exec()
"--output", "-").MatchContent(multi_arch.OutputIndex).Exec()
})

It("should fail if given a reference that does not exist in the repo", func() {
Expand Down Expand Up @@ -249,7 +249,7 @@ var _ = Describe("1.1 registry users:", func() {
ORAS("manifest", "index", "create", RegistryRef(ZOTHost, testRepo, "v1")).Exec()
// add a manifest to the index
ORAS("manifest", "index", "update", RegistryRef(ZOTHost, testRepo, "v1"),
"--add", string(multi_arch.LinuxAMD64.Digest), "--output", "-").MatchKeyWords(multi_arch.OutputIndex).Exec()
"--add", string(multi_arch.LinuxAMD64.Digest), "--output", "-").MatchContent(multi_arch.OutputIndex).Exec()
})

It("should tell user nothing to update if no update flags are used", func() {
Expand Down Expand Up @@ -417,7 +417,7 @@ var _ = Describe("OCI image layout users:", func() {
root := PrepareTempOCI(ImageRepo)
indexRef := LayoutRef(root, "output-to-stdout")
ORAS("manifest", "index", "create", Flags.Layout, indexRef, string(multi_arch.LinuxAMD64.Digest),
"--output", "-").MatchKeyWords(multi_arch.OutputIndex).Exec()
"--output", "-").MatchContent(multi_arch.OutputIndex).Exec()
})

It("should fail if given a reference that does not exist in the repo", func() {
Expand Down Expand Up @@ -502,7 +502,7 @@ var _ = Describe("OCI image layout users:", func() {
ORAS("manifest", "index", "create", Flags.Layout, LayoutRef(root, "index01")).Exec()
// add a manifest to the index
ORAS("manifest", "index", "update", Flags.Layout, LayoutRef(root, "index01"),
"--add", string(multi_arch.LinuxAMD64.Digest), "--output", "-").MatchKeyWords(multi_arch.OutputIndex).Exec()
"--add", string(multi_arch.LinuxAMD64.Digest), "--output", "-").MatchContent(multi_arch.OutputIndex).Exec()
})

It("should tell user nothing to update if no update flags are used", func() {
Expand Down
Loading