-
Notifications
You must be signed in to change notification settings - Fork 177
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
refactor: status, metadata and content handlers for manifest index
commands
#1509
base: main
Are you sure you want to change the base?
Changes from all commits
182c8f0
495ab7e
d81fd04
f807af8
9134a8f
5104cc9
a9b1164
4d144be
4d18845
8ea9b14
16cf5ab
c56fdec
49091a2
66249a8
6e3b09a
851701d
3a2fb99
a7a88d8
f6c0790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,58 @@ | ||||||||||
/* | ||||||||||
Copyright The ORAS 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 content | ||||||||||
|
||||||||||
import ( | ||||||||||
"fmt" | ||||||||||
"io" | ||||||||||
"os" | ||||||||||
|
||||||||||
"oras.land/oras/cmd/oras/internal/output" | ||||||||||
) | ||||||||||
|
||||||||||
// manifestIndexCreate handles raw content output. | ||||||||||
type manifestIndexCreate struct { | ||||||||||
pretty bool | ||||||||||
stdout io.Writer | ||||||||||
outputPath string | ||||||||||
} | ||||||||||
|
||||||||||
// OnContentCreated is called after index content is created. | ||||||||||
func (h *manifestIndexCreate) OnContentCreated(manifest []byte) error { | ||||||||||
out := h.stdout | ||||||||||
if h.outputPath != "" && h.outputPath != "-" { | ||||||||||
f, err := os.Create(h.outputPath) | ||||||||||
if err != nil { | ||||||||||
return fmt.Errorf("failed to open %q: %w", h.outputPath, err) | ||||||||||
} | ||||||||||
defer f.Close() | ||||||||||
out = f | ||||||||||
} | ||||||||||
return output.PrintJSON(out, manifest, h.pretty) | ||||||||||
qweeah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} | ||||||||||
|
||||||||||
// NewManifestIndexCreateHandler creates a new handler. | ||||||||||
func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string) ManifestIndexCreateHandler { | ||||||||||
// ignore --pretty when output to a file | ||||||||||
if outputPath != "" && outputPath != "-" { | ||||||||||
pretty = false | ||||||||||
} | ||||||||||
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is different from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
oras/cmd/oras/root/manifest/fetch.go Lines 80 to 83 in dcef719
I think we need to remove it and change the flag reset to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added pretty value reset in manifest fetch handler too. |
||||||||||
return &manifestIndexCreate{ | ||||||||||
qweeah marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
pretty: pretty, | ||||||||||
stdout: out, | ||||||||||
outputPath: outputPath, | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,9 +174,40 @@ func NewManifestPushHandler(printer *output.Printer) metadata.ManifestPushHandle | |
return text.NewManifestPushHandler(printer) | ||
} | ||
|
||
// NewManifestIndexCreateHandler returns an index create handler. | ||
func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestIndexCreateHandler { | ||
return text.NewManifestIndexCreateHandler(printer) | ||
// NewManifestIndexCreateHandler returns status, metadata and content handlers for index create command. | ||
func NewManifestIndexCreateHandler(outputPath string, printer *output.Printer, pretty bool) ( | ||
status.ManifestIndexCreateHandler, | ||
metadata.ManifestIndexCreateHandler, | ||
content.ManifestIndexCreateHandler) { | ||
statusHandler := status.NewTextManifestIndexCreateHandler(printer) | ||
metadataHandler := text.NewManifestIndexCreateHandler(printer) | ||
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||
switch outputPath { | ||
case "": | ||
contentHandler = content.NewDiscardHandler() | ||
case "-": | ||
statusHandler = status.NewDiscardHandler() | ||
metadataHandler = metadata.NewDiscardHandler() | ||
} | ||
return statusHandler, metadataHandler, contentHandler | ||
} | ||
|
||
// NewManifestIndexUpdateHandler returns status, metadata and content handlers for index update command. | ||
func NewManifestIndexUpdateHandler(outputPath string, printer *output.Printer, pretty bool) ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should there just be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently we need both create and update handlers. |
||
status.ManifestIndexUpdateHandler, | ||
metadata.ManifestIndexUpdateHandler, | ||
content.ManifestIndexUpdateHandler) { | ||
statusHandler := status.NewTextManifestIndexUpdateHandler(printer) | ||
metadataHandler := text.NewManifestIndexCreateHandler(printer) | ||
contentHandler := content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||
switch outputPath { | ||
case "": | ||
contentHandler = content.NewDiscardHandler() | ||
case "-": | ||
statusHandler = status.NewDiscardHandler() | ||
metadataHandler = metadata.NewDiscardHandler() | ||
} | ||
return statusHandler, metadataHandler, contentHandler | ||
} | ||
|
||
// NewCopyHandler returns copy handlers. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ package status | |
|
||
import ( | ||
"context" | ||
|
||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2" | ||
) | ||
|
@@ -61,3 +63,19 @@ type CopyHandler interface { | |
PostCopy(ctx context.Context, desc ocispec.Descriptor) error | ||
OnMounted(ctx context.Context, desc ocispec.Descriptor) error | ||
} | ||
|
||
// ManifestIndexCreateHandler handles status output for manifest index create command. | ||
type ManifestIndexCreateHandler interface { | ||
OnFetching(manifestRef string) error | ||
OnFetched(manifestRef string, desc ocispec.Descriptor) error | ||
OnIndexPacked(desc ocispec.Descriptor) error | ||
OnIndexPushed(path string) error | ||
} | ||
|
||
// ManifestIndexUpdateHandler handles status output for manifest index update command. | ||
type ManifestIndexUpdateHandler interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Contains duplicated methods OnIndexFetching(indexRef string) error
OnIndexFetched(indexRef string, digest digest.Digest) error You can create a new interface and use it in both interfaces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created an interface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on this comment, this interface is now removed. |
||
ManifestIndexCreateHandler | ||
OnManifestRemoved(digest digest.Digest) error | ||
OnManifestAdded(manifestRef string, desc ocispec.Descriptor) error | ||
OnIndexMerged(indexRef string, desc ocispec.Descriptor) error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,11 @@ | |
"context" | ||
"sync" | ||
|
||
"oras.land/oras/internal/contentutil" | ||
"oras.land/oras/internal/descriptor" | ||
"oras.land/oras/internal/graph" | ||
|
||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content" | ||
|
@@ -179,3 +182,93 @@ | |
ch.committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle]) | ||
return ch.printer.PrintStatus(desc, copyPromptMounted) | ||
} | ||
|
||
// TextManifestIndexCreateHandler handles text status output for manifest index create events. | ||
type TextManifestIndexCreateHandler struct { | ||
printer *output.Printer | ||
} | ||
|
||
// OnFetching implements ManifestIndexCreateHandler. | ||
func (mich TextManifestIndexCreateHandler) OnFetching(source string) error { | ||
return mich.printer.Println(IndexPromptFetching, source) | ||
} | ||
|
||
// OnFetched implements ManifestIndexCreateHandler. | ||
func (mich TextManifestIndexCreateHandler) OnFetched(source string, _ ocispec.Descriptor) error { | ||
return mich.printer.Println(IndexPromptFetched, source) | ||
} | ||
|
||
// OnIndexPacked implements ManifestIndexCreateHandler. | ||
func (mich TextManifestIndexCreateHandler) OnIndexPacked(desc ocispec.Descriptor) error { | ||
return mich.printer.Println(IndexPromptPacked, descriptor.ShortDigest(desc), ocispec.MediaTypeImageIndex) | ||
} | ||
|
||
// OnIndexPushed implements ManifestIndexCreateHandler. | ||
func (mich TextManifestIndexCreateHandler) OnIndexPushed(path string) error { | ||
return mich.printer.Println(IndexPromptPushed, path) | ||
} | ||
|
||
// NewTextManifestIndexCreateHandler returns a new handler for manifest index create command. | ||
func NewTextManifestIndexCreateHandler(printer *output.Printer) ManifestIndexCreateHandler { | ||
tmich := TextManifestIndexCreateHandler{ | ||
printer: printer, | ||
} | ||
return &tmich | ||
Comment on lines
+213
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current naming style is aligned with the rest of the file. |
||
} | ||
|
||
// TextManifestIndexUpdateHandler handles text status output for manifest index update events. | ||
type TextManifestIndexUpdateHandler struct { | ||
printer *output.Printer | ||
} | ||
|
||
// OnFetching implements ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnFetching(ref string) error { | ||
return miuh.printer.Println(IndexPromptFetching, ref) | ||
} | ||
|
||
// OnFetched implements ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnFetched(ref string, desc ocispec.Descriptor) error { | ||
if contentutil.IsDigest(ref) { | ||
return miuh.printer.Println(IndexPromptFetched, ref) | ||
} | ||
return miuh.printer.Println(IndexPromptFetched, desc.Digest, ref) | ||
} | ||
|
||
// OnManifestRemoved implements metadata.ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnManifestRemoved(digest digest.Digest) error { | ||
return miuh.printer.Println("Removed", digest) | ||
} | ||
|
||
// OnManifestAdded implements metadata.ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnManifestAdded(ref string, desc ocispec.Descriptor) error { | ||
if contentutil.IsDigest(ref) { | ||
return miuh.printer.Println("Added", ref) | ||
} | ||
return miuh.printer.Println("Added", desc.Digest, ref) | ||
} | ||
|
||
// OnIndexMerged implements metadata.ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnIndexMerged(ref string, desc ocispec.Descriptor) error { | ||
if contentutil.IsDigest(ref) { | ||
return miuh.printer.Println("Merged", ref) | ||
} | ||
return miuh.printer.Println("Merged", desc.Digest, ref) | ||
} | ||
|
||
// OnIndexUpdated implements metadata.ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnIndexPacked(desc ocispec.Descriptor) error { | ||
return miuh.printer.Println("Updated", desc.Digest) | ||
} | ||
|
||
// OnIndexPushed implements metadata.ManifestIndexUpdateHandler. | ||
func (miuh TextManifestIndexUpdateHandler) OnIndexPushed(indexRef string) error { | ||
return miuh.printer.Println("Pushed", indexRef) | ||
} | ||
|
||
// NewTextManifestIndexUpdateHandler returns a new handler for manifest index create command. | ||
func NewTextManifestIndexUpdateHandler(printer *output.Printer) ManifestIndexUpdateHandler { | ||
miuh := TextManifestIndexUpdateHandler{ | ||
printer: printer, | ||
} | ||
return &miuh | ||
Comment on lines
+270
to
+273
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current naming style is aligned with the rest of the file. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to create an issue to track handling errors returned by
Close()
in all write paths.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #1516