From 23e5ab19115414bb2f22282b8c6046cd47137d6a Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Thu, 12 Sep 2024 17:13:06 +0800 Subject: [PATCH] output Signed-off-by: Xiaoxuan Wang --- cmd/oras/root/manifest/index/create.go | 26 ++++++++++++++++-------- test/e2e/suite/command/manifest_index.go | 8 ++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/cmd/oras/root/manifest/index/create.go b/cmd/oras/root/manifest/index/create.go index 1305cf1fd..4a84ccd2a 100644 --- a/cmd/oras/root/manifest/index/create.go +++ b/cmd/oras/root/manifest/index/create.go @@ -44,11 +44,12 @@ var maxConfigSize int64 = 4 * 1024 * 1024 // 4 MiB type createOptions struct { option.Common - option.Packer option.Target + option.Pretty - sources []string - extraRefs []string + sources []string + extraRefs []string + outputPath string } func createCmd() *cobra.Command { @@ -73,8 +74,8 @@ Example - create an index and push it with multiple tags: Example - create an index and push to an OCI image layout folder 'layout-dir' and tag with 'v1': oras manifest index create layout-dir:v1 linux-amd64 sha256:99e4703fbf30916f549cd6bfa9cdbab614b5392fbe64fdee971359a77073cdf9 -Example - create an index and export it to index.json, auto push will be disabled: - oras manifest index create --export-manifest index.json localhost:5000/hello linux-amd64 linux-arm64 +Example - create an index and save it locally to index.json, auto push will be disabled: + oras manifest index create --output index.json localhost:5000/hello linux-amd64 linux-arm64 `, Args: oerrors.CheckArgs(argument.AtLeast(1), "the destination index to create."), PreRunE: func(cmd *cobra.Command, args []string) error { @@ -89,6 +90,7 @@ Example - create an index and export it to index.json, auto push will be disable return createIndex(cmd, opts) }, } + cmd.Flags().StringVarP(&opts.outputPath, "output", "o", "", "file `path` to write the created index to, use - for stdout") option.ApplyFlags(&opts, cmd.Flags()) return oerrors.Command(cmd, &opts.Target) } @@ -114,11 +116,17 @@ func createIndex(cmd *cobra.Command, opts createOptions) error { desc := content.NewDescriptorFromBytes(ocispec.MediaTypeImageIndex, indexBytes) opts.Println(status.IndexPromptPacked, descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex) - if opts.ManifestExportPath == "" { - return pushIndex(ctx, target, desc, indexBytes, opts.Reference, opts.extraRefs, opts.AnnotatedReference(), opts.Printer) + 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) + err = os.WriteFile(opts.outputPath, indexBytes, 0666) } - - return os.WriteFile(opts.ManifestExportPath, indexBytes, 0666) + return err } func fetchSourceManifests(ctx context.Context, target oras.ReadOnlyTarget, opts createOptions) ([]ocispec.Descriptor, error) { diff --git a/test/e2e/suite/command/manifest_index.go b/test/e2e/suite/command/manifest_index.go index 780fd56f5..c767cb7c7 100644 --- a/test/e2e/suite/command/manifest_index.go +++ b/test/e2e/suite/command/manifest_index.go @@ -132,10 +132,10 @@ var _ = Describe("1.1 registry users:", func() { }) It("should output created index to file", func() { - testRepo := indexTestRepo("create", "export-manifest") + testRepo := indexTestRepo("create", "output-to-file") CopyZOTRepo(ImageRepo, testRepo) filePath := filepath.Join(GinkgoT().TempDir(), "createdIndex") - ORAS("manifest", "index", "create", RegistryRef(ZOTHost, testRepo, ""), string(multi_arch.LinuxAMD64.Digest), "--export-manifest", filePath).Exec() + ORAS("manifest", "index", "create", RegistryRef(ZOTHost, testRepo, ""), string(multi_arch.LinuxAMD64.Digest), "--output", filePath).Exec() MatchFile(filePath, multi_arch.CreatedIndex, DefaultTimeout) }) @@ -222,9 +222,9 @@ var _ = Describe("OCI image layout users:", func() { It("should output created index to file", func() { root := PrepareTempOCI(ImageRepo) - indexRef := LayoutRef(root, "export-manifest") + indexRef := LayoutRef(root, "output-to-file") filePath := filepath.Join(GinkgoT().TempDir(), "createdIndex") - ORAS("manifest", "index", "create", Flags.Layout, indexRef, string(multi_arch.LinuxAMD64.Digest), "--export-manifest", filePath).Exec() + ORAS("manifest", "index", "create", Flags.Layout, indexRef, string(multi_arch.LinuxAMD64.Digest), "--output", filePath).Exec() MatchFile(filePath, multi_arch.CreatedIndex, DefaultTimeout) })