-
Notifications
You must be signed in to change notification settings - Fork 174
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 8 commits
182c8f0
495ab7e
d81fd04
f807af8
9134a8f
5104cc9
a9b1164
4d144be
4d18845
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,54 @@ | ||
/* | ||
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) | ||
wangxiaoxuan273 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// NewManifestIndexCreateHandler creates a new handler. | ||
func NewManifestIndexCreateHandler(out io.Writer, pretty bool, outputPath string) ManifestIndexCreateHandler { | ||
return &manifestIndexCreate{ | ||
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. reset pretty here if output path is |
||
pretty: pretty, | ||
stdout: out, | ||
outputPath: outputPath, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -174,9 +174,56 @@ 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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var statusHandler status.ManifestIndexCreateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var metadataHandler metadata.ManifestIndexCreateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var contentHandler content.ManifestIndexCreateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch outputPath { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case "": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewTextManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = text.NewManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case "-": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = metadata.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewTextManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = text.NewManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+183
to
+199
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return statusHandler, metadataHandler, contentHandler, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status.ManifestIndexUpdateHandler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadata.ManifestIndexUpdateHandler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
content.ManifestIndexUpdateHandler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var statusHandler status.ManifestIndexUpdateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var metadataHandler metadata.ManifestIndexUpdateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var contentHandler content.ManifestIndexUpdateHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch outputPath { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case "": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewTextManifestIndexUpdateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = text.NewManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case "-": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = metadata.NewDiscardHandler() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusHandler = status.NewTextManifestIndexUpdateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
metadataHandler = text.NewManifestIndexCreateHandler(printer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
contentHandler = content.NewManifestIndexCreateHandler(printer, pretty, outputPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return statusHandler, metadataHandler, contentHandler, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// NewCopyHandler returns copy handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,10 @@ | |||||
|
||||||
package metadata | ||||||
|
||||||
import ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||||||
import ( | ||||||
"github.com/opencontainers/go-digest" | ||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||||||
) | ||||||
|
||||||
type discard struct{} | ||||||
|
||||||
|
@@ -28,3 +31,13 @@ | |||||
func (discard) OnFetched(string, ocispec.Descriptor, []byte) error { | ||||||
return nil | ||||||
} | ||||||
|
||||||
// OnTagged implements ManifestIndexCreateHandler. | ||||||
func (discard) OnTagged(desc ocispec.Descriptor, tag string) error { | ||||||
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. NIT
Suggested change
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// OnCompleted implements ManifestIndexCreateHandler. | ||||||
func (discard) OnCompleted(digest digest.Digest) error { | ||||||
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.
Suggested change
|
||||||
return nil | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ limitations under the License. | |||||
package metadata | ||||||
|
||||||
import ( | ||||||
"github.com/opencontainers/go-digest" | ||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||||||
"oras.land/oras/cmd/oras/internal/option" | ||||||
) | ||||||
|
@@ -81,8 +82,12 @@ type ManifestPushHandler interface { | |||||
// ManifestIndexCreateHandler handles metadata output for index create events. | ||||||
type ManifestIndexCreateHandler interface { | ||||||
TaggedHandler | ||||||
OnCompleted(digest digest.Digest) error | ||||||
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.
Suggested change
|
||||||
} | ||||||
|
||||||
// ManifestIndexUpdateHandler handles metadata output for index update events. | ||||||
type ManifestIndexUpdateHandler ManifestIndexCreateHandler | ||||||
|
||||||
// CopyHandler handles metadata output for cp events. | ||||||
type CopyHandler interface { | ||||||
TaggedHandler | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ limitations under the License. | |
package text | ||
|
||
import ( | ||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/cmd/oras/internal/display/metadata" | ||
"oras.land/oras/cmd/oras/internal/output" | ||
|
@@ -37,3 +38,8 @@ func NewManifestIndexCreateHandler(printer *output.Printer) metadata.ManifestInd | |
func (h *ManifestIndexCreateHandler) OnTagged(_ ocispec.Descriptor, tag string) error { | ||
return h.printer.Println("Tagged", tag) | ||
} | ||
|
||
// OnCompleted implements ManifestIndexCreateHandler. | ||
func (h *ManifestIndexCreateHandler) OnCompleted(digest digest.Digest) error { | ||
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. Considering we are most likely to have format. Here should pass in a descriptor and output the digest of the descriptor |
||
return h.printer.Println("Digest:", digest) | ||
} |
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,24 @@ 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 { | ||
OnSourceManifestFetching(source string) error | ||
OnSourceManifestFetched(source string) error | ||
Comment on lines
+69
to
+70
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. Can we remove |
||
OnIndexPacked(shortDigest string) error | ||
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. Packed index should be moved to metadata output. 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. Packed, Updated, Pushed, Added, Removed, Merged should all be moved to metadata output. 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. Use one interface for packed and updated. |
||
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. |
||
OnIndexFetching(indexRef string) error | ||
OnIndexFetched(indexRef string, digest digest.Digest) error | ||
OnManifestFetching(manifestRef string) error | ||
OnManifestFetched(manifestRef string, digest digest.Digest) error | ||
OnManifestRemoved(digest digest.Digest) error | ||
OnManifestAdded(manifestRef string, digest digest.Digest) error | ||
OnIndexMerged(indexRef string, digest digest.Digest) error | ||
Comment on lines
+81
to
+83
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 be moved to metadata handler |
||
OnIndexUpdated(digest digest.Digest) error | ||
OnIndexPushed(path string) error | ||
} |
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.
if you changed outputPath to "" for "-" this logic would look less odd.