Skip to content

Commit

Permalink
output
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
Xiaoxuan Wang committed Sep 12, 2024
1 parent 2652246 commit 23e5ab1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 17 additions & 9 deletions cmd/oras/root/manifest/index/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)

Check warning on line 124 in cmd/oras/root/manifest/index/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/root/manifest/index/create.go#L122-L124

Added lines #L122 - L124 were not covered by tests
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) {
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 @@ -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)
})

Expand Down Expand Up @@ -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)
})

Expand Down

0 comments on commit 23e5ab1

Please sign in to comment.