Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into format-pull
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Mar 28, 2024
2 parents 61e0241 + 24c2acf commit b82375f
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 62 deletions.
15 changes: 6 additions & 9 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ func NewPushHandler(format string, tty *os.File, out io.Writer, verbose bool) (s
var metadataHandler metadata.PushHandler
switch format {
case "":
metadataHandler = text.NewPushHandler()
metadataHandler = text.NewPushHandler(out)
case "json":
metadataHandler = json.NewPushHandler()
metadataHandler = json.NewPushHandler(out)
default:
metadataHandler = template.NewPushHandler(format)
metadataHandler = template.NewPushHandler(out, format)
}
metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}

Expand All @@ -64,13 +63,12 @@ func NewAttachHandler(format string, tty *os.File, out io.Writer, verbose bool)
var metadataHandler metadata.AttachHandler
switch format {
case "":
metadataHandler = text.NewAttachHandler()
metadataHandler = text.NewAttachHandler(out)
case "json":
metadataHandler = json.NewAttachHandler()
metadataHandler = json.NewAttachHandler(out)
default:
metadataHandler = template.NewAttachHandler(format)
metadataHandler = template.NewAttachHandler(out, format)
}
metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}

Expand All @@ -94,6 +92,5 @@ func NewPullHandler(format string, path string, tty *os.File, out io.Writer, ver
default:
metadataHandler = template.NewPullHandler(path, format)
}
metadataHandler.WithOutput(out)
return statusHandler, metadataHandler
}
4 changes: 0 additions & 4 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ package metadata

import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles metadata output for push events.
type PushHandler interface {
OnCopied(opts *option.Target) error
OnCompleted(root ocispec.Descriptor) error
view.Outputable
}

// AttachHandler handles metadata output for attach events.
type AttachHandler interface {
OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error
view.Outputable
}

// PullHandler handles metadata output for attach events.
Expand All @@ -40,5 +37,4 @@ type PullHandler interface {
OnFilePulled(name string, outputDir string, desc ocispec.Descriptor, descPath string)
// OnCompleted is called when the pull cmd execution is completed.
OnCompleted(opts *option.Target, desc ocispec.Descriptor, layerSkipped bool) error
view.Outputable
}
13 changes: 7 additions & 6 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ limitations under the License.
package json

import (
"io"

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/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// AttachHandler handles json metadata output for attach events.
type AttachHandler struct {
view.Printer
out io.Writer
}

// NewAttachHandler creates a new handler for attach events.
func NewAttachHandler() metadata.AttachHandler {
func NewAttachHandler(out io.Writer) metadata.AttachHandler {
return &AttachHandler{
Printer: view.NewPrinter(),
out: out,
}
}

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

func printJSON(out io.Writer, object any) error {
encoder := json.NewEncoder(out)
encoder.SetIndent("", " ")
return encoder.Encode(object)
}
11 changes: 6 additions & 5 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ limitations under the License.
package json

import (
"io"

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/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles JSON metadata output for push events.
type PushHandler struct {
path string
view.Printer
out io.Writer
}

// NewPushHandler creates a new handler for push events.
func NewPushHandler() metadata.PushHandler {
func NewPushHandler(out io.Writer) metadata.PushHandler {
return &PushHandler{
Printer: view.NewPrinter(),
out: out,
}
}

Expand All @@ -44,5 +45,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 ph.PrintJSON(model.NewPush(root, ph.path))
return printJSON(ph.out, model.NewPush(root, ph.path))
}
11 changes: 6 additions & 5 deletions cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ limitations under the License.
package template

import (
"io"

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/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// AttachHandler handles go-template metadata output for attach events.
type AttachHandler struct {
template string
view.Printer
out io.Writer
}

// NewAttachHandler returns a new handler for attach metadata events.
func NewAttachHandler(template string) metadata.AttachHandler {
func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler {
return &AttachHandler{
out: out,
template: template,
Printer: view.NewPrinter(),
}
}

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return ah.ParseAndWrite(model.NewPush(root, opts.Path), ah.template)
return parseAndWrite(ah.out, model.NewPush(root, opts.Path), ah.template)
}
11 changes: 6 additions & 5 deletions cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ limitations under the License.
package template

import (
"io"

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/metadata/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles go-template metadata output for push events.
type PushHandler struct {
template string
path string
view.Printer
out io.Writer
}

// NewPushHandler returns a new handler for push events.
func NewPushHandler(template string) metadata.PushHandler {
func NewPushHandler(out io.Writer, template string) metadata.PushHandler {
return &PushHandler{
out: out,
template: template,
Printer: view.NewPrinter(),
}
}

Expand All @@ -46,5 +47,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 ph.ParseAndWrite(model.NewPush(root, ph.path), ph.template)
return parseAndWrite(ph.out, model.NewPush(root, ph.path), ph.template)
}
31 changes: 31 additions & 0 deletions cmd/oras/internal/display/metadata/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
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 (
"io"
"text/template"

"github.com/Masterminds/sprig/v3"
)

func parseAndWrite(out io.Writer, object any, templateStr string) error {
t, err := template.New("format output").Funcs(sprig.FuncMap()).Parse(templateStr)
if err != nil {
return err
}
return t.Execute(out, object)
}
12 changes: 6 additions & 6 deletions cmd/oras/internal/display/metadata/text/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ package text

import (
"fmt"
"io"
"strings"

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/view"
"oras.land/oras/cmd/oras/internal/option"
)

// AttachHandler handles text metadata output for attach events.
type AttachHandler struct {
view.Printer
out io.Writer
}

// NewAttachHandler returns a new handler for attach events.
func NewAttachHandler() metadata.AttachHandler {
func NewAttachHandler(out io.Writer) metadata.AttachHandler {
return &AttachHandler{
Printer: view.NewPrinter(),
out: out,
}
}

Expand All @@ -43,10 +43,10 @@ func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.
if !strings.HasSuffix(opts.RawReference, digest) {
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)
}
_, err := ah.Println("Attached to", opts.AnnotatedReference())
_, err := fmt.Fprintln(ah.out, "Attached to", opts.AnnotatedReference())
if err != nil {
return err
}
_, err = ah.Println("Digest:", root.Digest)
_, err = fmt.Fprintln(ah.out, "Digest:", root.Digest)
return err
}
14 changes: 8 additions & 6 deletions cmd/oras/internal/display/metadata/text/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@ limitations under the License.
package text

import (
"fmt"
"io"

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/view"
"oras.land/oras/cmd/oras/internal/option"
)

// PushHandler handles text metadata output for push events.
type PushHandler struct {
view.Printer
out io.Writer
}

// NewPushHandler returns a new handler for push events.
func NewPushHandler() metadata.PushHandler {
func NewPushHandler(out io.Writer) metadata.PushHandler {
return &PushHandler{
Printer: view.NewPrinter(),
out: out,
}
}

// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
_, err := ph.Println("Pushed", opts.AnnotatedReference())
_, err := fmt.Fprintln(ph.out, "Pushed", opts.AnnotatedReference())
return err
}

// OnCompleted is called after the push is completed.
func (ph *PushHandler) OnCompleted(root ocispec.Descriptor) error {
_, err := ph.Println("Digest:", root.Digest)
_, err := fmt.Fprintln(ph.out, "Digest:", root.Digest)
return err
}
11 changes: 5 additions & 6 deletions cmd/oras/internal/display/status/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
utils "oras.land/oras/cmd/oras/internal/display/utils"
)

// TextPushHandler handles text status output for push events.
Expand Down Expand Up @@ -107,27 +106,27 @@ func (ph *TextPullHandler) Close() error {

// OnNodeDownloading implements PullHandler.
func (ph *TextPullHandler) OnNodeDownloading(desc ocispec.Descriptor) error {
return PrintStatus(desc, utils.PullPromptDownloading, ph.verbose)
return PrintStatus(desc, PullPromptDownloading, ph.verbose)
}

// OnNodeDownloaded implements PullHandler.
func (ph *TextPullHandler) OnNodeDownloaded(desc ocispec.Descriptor) error {
return PrintStatus(desc, utils.PullPromptDownloaded, ph.verbose)
return PrintStatus(desc, PullPromptDownloaded, ph.verbose)
}

// OnNodeRestored implements PullHandler.
func (ph *TextPullHandler) OnNodeRestored(desc ocispec.Descriptor) error {
return PrintStatus(desc, utils.PullPromptRestored, ph.verbose)
return PrintStatus(desc, PullPromptRestored, ph.verbose)
}

// OnNodeProcessing implements PullHandler.
func (ph *TextPullHandler) OnNodeProcessing(desc ocispec.Descriptor) error {
return PrintStatus(desc, utils.PullPromptProcessing, ph.verbose)
return PrintStatus(desc, PullPromptProcessing, ph.verbose)
}

// OnNodeProcessing implements PullHandler.
func (ph *TextPullHandler) OnNodeSkipped(desc ocispec.Descriptor) error {
return PrintStatus(desc, utils.PullPromptSkipped, ph.verbose)
return PrintStatus(desc, PullPromptSkipped, ph.verbose)
}

// NewTextPullHandler returns a new handler for pull command.
Expand Down
Loading

0 comments on commit b82375f

Please sign in to comment.