Skip to content

Commit

Permalink
feat: support --format in oras discover (#1299)
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Apr 11, 2024
1 parent b5217ef commit cc9b2b6
Show file tree
Hide file tree
Showing 18 changed files with 549 additions and 270 deletions.
18 changes: 18 additions & 0 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ import (
"io"
"os"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"

"oras.land/oras/cmd/oras/internal/display/content"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/descriptor"
"oras.land/oras/cmd/oras/internal/display/metadata/json"
"oras.land/oras/cmd/oras/internal/display/metadata/table"
"oras.land/oras/cmd/oras/internal/display/metadata/template"
"oras.land/oras/cmd/oras/internal/display/metadata/text"
"oras.land/oras/cmd/oras/internal/display/metadata/tree"
"oras.land/oras/cmd/oras/internal/display/status"
)

Expand Down Expand Up @@ -97,6 +101,20 @@ func NewPullHandler(format string, path string, tty *os.File, out io.Writer, ver
return statusHandler, metadataHandler
}

// NewDiscoverHandler returns status and metadata handlers for discover command.
func NewDiscoverHandler(out io.Writer, outputType string, path string, rawReference string, desc ocispec.Descriptor, verbose bool) metadata.DiscoverHandler {
switch outputType {
case "tree", "":
return tree.NewDiscoverHandler(out, path, desc, verbose)
case "table":
return table.NewDiscoverHandler(out, rawReference, desc, verbose)
case "json":
return json.NewDiscoverHandler(out, desc, path)
default:
return template.NewDiscoverHandler(out, desc, path, outputType)
}
}

// NewManifestFetchHandler returns a manifest fetch handler.
func NewManifestFetchHandler(out io.Writer, format string, outputDescriptor, pretty bool, outputPath string) (metadata.ManifestFetchHandler, content.ManifestFetchHandler) {
var metadataHandler metadata.ManifestFetchHandler
Expand Down
11 changes: 11 additions & 0 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ type AttachHandler interface {
OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error
}

// DiscoverHandler handles metadata output for discover events.
type DiscoverHandler interface {
// MultiLevelSupported returns true if the handler supports multi-level
// discovery.
MultiLevelSupported() bool
// OnDiscovered is called after a referrer is discovered.
OnDiscovered(referrer, subject ocispec.Descriptor) error
// OnCompleted is called when referrer discovery is completed.
OnCompleted() error
}

// ManifestFetchHandler handles metadata output for manifest fetch events.
type ManifestFetchHandler interface {
// OnFetched is called after the manifest content is fetched.
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/utils"
"oras.land/oras/cmd/oras/internal/option"
)

Expand All @@ -38,5 +39,5 @@ func NewAttachHandler(out io.Writer) metadata.AttachHandler {

// OnCompleted is called when the attach command is completed.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return printJSON(ah.out, model.NewPush(root, opts.Path))
return utils.PrintPrettyJSON(ah.out, model.NewPush(root, opts.Path))
}
63 changes: 63 additions & 0 deletions cmd/oras/internal/display/metadata/json/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 json

import (
"fmt"
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/utils"
)

// discoverHandler handles json metadata output for discover events.
type discoverHandler struct {
out io.Writer
root ocispec.Descriptor
path string
referrers []ocispec.Descriptor
}

// NewDiscoverHandler creates a new handler for discover events.
func NewDiscoverHandler(out io.Writer, root ocispec.Descriptor, path string) metadata.DiscoverHandler {
return &discoverHandler{
out: out,
root: root,
path: path,
}
}

// MultiLevelSupported implements metadata.DiscoverHandler.
func (h *discoverHandler) MultiLevelSupported() bool {
return false
}

// OnDiscovered implements metadata.DiscoverHandler.
func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) error {
if !content.Equal(subject, h.root) {
return fmt.Errorf("unexpected subject descriptor: %v", subject)
}
h.referrers = append(h.referrers, referrer)
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
return utils.PrintPrettyJSON(h.out, model.NewDiscover(h.path, h.referrers))
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/json/manifest_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/utils"
)

// manifestFetchHandler handles JSON metadata output for manifest fetch events.
Expand All @@ -42,5 +43,5 @@ func (h *manifestFetchHandler) OnFetched(path string, desc ocispec.Descriptor, c
if err := json.Unmarshal(content, &manifest); err != nil {
manifest = nil
}
return printJSON(h.out, model.NewFetched(path, desc, manifest))
return utils.PrintPrettyJSON(h.out, model.NewFetched(path, desc, manifest))
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/json/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/utils"
"oras.land/oras/cmd/oras/internal/option"
)

Expand Down Expand Up @@ -51,5 +52,5 @@ func (ph *PullHandler) OnFilePulled(name string, outputDir string, desc ocispec.

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
return printJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
return utils.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/display/utils"
"oras.land/oras/cmd/oras/internal/option"
)

Expand All @@ -45,5 +46,5 @@ func (ph *PushHandler) OnCopied(opts *option.Target) error {

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
return printJSON(ph.out, model.NewPush(root, ph.path))
return utils.PrintPrettyJSON(ph.out, model.NewPush(root, ph.path))
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package json
package model

import (
"encoding/json"
"io"
)
import ocispec "github.com/opencontainers/image-spec/specs-go/v1"

func printJSON(out io.Writer, object any) error {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(object)
type discover struct {
Manifests []Descriptor
}

// NewDiscover creates a new discover model.
func NewDiscover(name string, descs []ocispec.Descriptor) discover {
discover := discover{
Manifests: make([]Descriptor, 0, len(descs)),
}
for _, desc := range descs {
discover.Manifests = append(discover.Manifests, FromDescriptor(name, desc))
}
return discover
}
100 changes: 100 additions & 0 deletions cmd/oras/internal/display/metadata/table/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 table

import (
"fmt"
"io"
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/utils"
)

// discoverHandler handles json metadata output for discover events.
type discoverHandler struct {
out io.Writer
rawReference string
root ocispec.Descriptor
verbose bool
referrers []ocispec.Descriptor
}

// NewDiscoverHandler creates a new handler for discover events.
func NewDiscoverHandler(out io.Writer, rawReference string, root ocispec.Descriptor, verbose bool) metadata.DiscoverHandler {
return &discoverHandler{
out: out,
rawReference: rawReference,
root: root,
verbose: verbose,
}
}

// MultiLevelSupported implements metadata.DiscoverHandler.
func (h *discoverHandler) MultiLevelSupported() bool {
return false
}

// OnDiscovered implements metadata.DiscoverHandler.
func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) error {
if !content.Equal(subject, h.root) {
return fmt.Errorf("unexpected subject descriptor: %v", subject)
}
h.referrers = append(h.referrers, referrer)
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
if n := len(h.referrers); n > 1 {
fmt.Fprintln(h.out, "Discovered", n, "artifacts referencing", h.rawReference)
} else {
fmt.Fprintln(h.out, "Discovered", n, "artifact referencing", h.rawReference)
}
fmt.Fprintln(h.out, "Digest:", h.root.Digest)
if len(h.referrers) == 0 {
return nil
}
fmt.Fprintln(h.out)
return h.printDiscoveredReferrersTable()
}

func (h *discoverHandler) printDiscoveredReferrersTable() error {
typeNameTitle := "Artifact Type"
typeNameLength := len(typeNameTitle)
for _, ref := range h.referrers {
if length := len(ref.ArtifactType); length > typeNameLength {
typeNameLength = length
}
}

print := func(key string, value interface{}) {
fmt.Fprintln(h.out, key, strings.Repeat(" ", typeNameLength-len(key)+1), value)
}

print(typeNameTitle, "Digest")
for _, ref := range h.referrers {
print(ref.ArtifactType, ref.Digest)
if h.verbose {
if err := utils.PrintPrettyJSON(h.out, ref); err != nil {
return fmt.Errorf("error printing JSON: %w", err)
}
}
}
return nil
}
64 changes: 64 additions & 0 deletions cmd/oras/internal/display/metadata/template/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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 template

import (
"fmt"
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
)

// discoverHandler handles json metadata output for discover events.
type discoverHandler struct {
referrers []ocispec.Descriptor
template string
path string
root ocispec.Descriptor
out io.Writer
}

// NewDiscoverHandler creates a new handler for discover events.
func NewDiscoverHandler(out io.Writer, root ocispec.Descriptor, path string, template string) metadata.DiscoverHandler {
return &discoverHandler{
out: out,
root: root,
path: path,
template: template,
}
}

// MultiLevelSupported implements metadata.DiscoverHandler.
func (h *discoverHandler) MultiLevelSupported() bool {
return false
}

// OnDiscovered implements metadata.DiscoverHandler.
func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) error {
if !content.Equal(subject, h.root) {
return fmt.Errorf("unexpected subject descriptor: %v", subject)
}
h.referrers = append(h.referrers, referrer)
return nil
}

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
return parseAndWrite(h.out, model.NewDiscover(h.path, h.referrers), h.template)
}
Loading

0 comments on commit cc9b2b6

Please sign in to comment.