Skip to content

Commit

Permalink
refactor: add stdout to metadata handlers
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Mar 27, 2024
1 parent d99475b commit dbeb38d
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 123 deletions.
16 changes: 8 additions & 8 deletions cmd/oras/internal/display/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ 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(format, out)
}
metadataHandler.WithOutput(out)

return statusHandler, metadataHandler
}

Expand All @@ -64,12 +64,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(format, out)
}
metadataHandler.WithOutput(out)

return statusHandler, metadataHandler
}
3 changes: 0 additions & 3 deletions cmd/oras/internal/display/metadata/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ 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
}
11 changes: 6 additions & 5 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))
return printJSON(a.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(template string, out io.Writer) metadata.AttachHandler {
return &AttachHandler{
Printer: view.NewPrinter(),
out: out,
template: template,
}
}

// 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,24 +16,25 @@ 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(template string, out io.Writer) metadata.PushHandler {
return &PushHandler{
Printer: view.NewPrinter(),
out: out,
template: template,
}
}
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 (a *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.D
if !strings.HasSuffix(opts.RawReference, digest) {
opts.RawReference = fmt.Sprintf("%s@%s", opts.Path, subject.Digest)
}
_, err := a.Println("Attached to", opts.AnnotatedReference())
_, err := fmt.Fprintln(a.out, "Attached to", opts.AnnotatedReference())
if err != nil {
return err
}
_, err = a.Println("Digest:", root.Digest)
_, err = fmt.Fprintln(a.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 (p *PushHandler) OnCopied(opts *option.Target) error {
_, err := p.Println("Pushed", opts.AnnotatedReference())
_, err := fmt.Fprintln(p.out, "Pushed", opts.AnnotatedReference())
return err
}

// OnCompleted is called after the push is completed.
func (p *PushHandler) OnCompleted(root ocispec.Descriptor) error {
_, err := p.Println("Digest:", root.Digest)
_, err := fmt.Fprintln(p.out, "Digest:", root.Digest)
return err
}
Loading

0 comments on commit dbeb38d

Please sign in to comment.