Skip to content
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: Move two output functions to the output package #1417

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/content/manifest_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"os"

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

// manifestFetch handles raw content output.
Expand All @@ -41,7 +41,7 @@ func (h *manifestFetch) OnContentFetched(desc ocispec.Descriptor, manifest []byt
defer f.Close()
out = f
}
return utils.PrintJSON(out, manifest, h.pretty)
return output.PrintJSON(out, manifest, h.pretty)
}

// NewManifestFetchHandler creates a new handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"encoding/json"
"fmt"
"io"
"oras.land/oras/cmd/oras/internal/output"

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

// manifestFetchHandler handles metadata descriptor output.
Expand All @@ -37,7 +37,7 @@ func (h *manifestFetchHandler) OnFetched(_ string, desc ocispec.Descriptor, _ []
if err != nil {
return fmt.Errorf("invalid descriptor: %w", err)
}
return utils.PrintJSON(h.out, descBytes, h.pretty)
return output.PrintJSON(h.out, descBytes, h.pretty)
}

// NewManifestFetchHandler creates a new handler.
Expand Down
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ 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"
"oras.land/oras/cmd/oras/internal/output"
)

// AttachHandler handles json metadata output for attach events.
Expand All @@ -39,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 utils.PrintPrettyJSON(ah.out, model.NewAttach(root, opts.Path))
return output.PrintPrettyJSON(ah.out, model.NewAttach(root, opts.Path))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"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"
"oras.land/oras/cmd/oras/internal/output"
)

// discoverHandler handles json metadata output for discover events.
Expand Down Expand Up @@ -59,5 +59,5 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
return utils.PrintPrettyJSON(h.out, model.NewDiscover(h.path, h.referrers))
return output.PrintPrettyJSON(h.out, model.NewDiscover(h.path, h.referrers))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/manifest_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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"
"oras.land/oras/cmd/oras/internal/output"
)

// manifestFetchHandler handles JSON metadata output for manifest fetch events.
Expand All @@ -43,5 +43,5 @@ func (h *manifestFetchHandler) OnFetched(path string, desc ocispec.Descriptor, c
if err := json.Unmarshal(content, &manifest); err != nil {
manifest = nil
}
return utils.PrintPrettyJSON(h.out, model.NewFetched(path, desc, manifest))
return output.PrintPrettyJSON(h.out, model.NewFetched(path, desc, manifest))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ 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"
"oras.land/oras/cmd/oras/internal/output"
)

// PullHandler handles JSON metadata output for pull events.
Expand Down Expand Up @@ -52,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 utils.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
return output.PrintPrettyJSON(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/json/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ 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"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/contentutil"
)

Expand Down Expand Up @@ -57,5 +57,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 utils.PrintPrettyJSON(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()))
return output.PrintPrettyJSON(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()))
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/metadata/table/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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"
"oras.land/oras/cmd/oras/internal/output"
)

// discoverHandler handles json metadata output for discover events.
Expand Down Expand Up @@ -91,7 +91,7 @@
for _, ref := range h.referrers {
print(ref.ArtifactType, ref.Digest)
if h.verbose {
if err := utils.PrintPrettyJSON(h.out, ref); err != nil {
if err := output.PrintPrettyJSON(h.out, ref); err != nil {

Check warning on line 94 in cmd/oras/internal/display/metadata/table/discover.go

View check run for this annotation

Codecov / codecov/patch

cmd/oras/internal/display/metadata/table/discover.go#L94

Added line #L94 was not covered by tests
return fmt.Errorf("error printing JSON: %w", err)
}
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/template/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
)

// AttachHandler handles go-template metadata output for attach events.
Expand All @@ -40,5 +41,5 @@ func NewAttachHandler(out io.Writer, template string) metadata.AttachHandler {

// OnCompleted formats the metadata of attach command.
func (ah *AttachHandler) OnCompleted(opts *option.Target, root, subject ocispec.Descriptor) error {
return parseAndWrite(ah.out, model.NewAttach(root, opts.Path), ah.template)
return output.ParseAndWrite(ah.out, model.NewAttach(root, opts.Path), ah.template)
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/template/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"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/output"
)

// discoverHandler handles json metadata output for discover events.
Expand Down Expand Up @@ -60,5 +61,5 @@ func (h *discoverHandler) OnDiscovered(referrer, subject ocispec.Descriptor) err

// OnCompleted implements metadata.DiscoverHandler.
func (h *discoverHandler) OnCompleted() error {
return parseAndWrite(h.out, model.NewDiscover(h.path, h.referrers), h.template)
return output.ParseAndWrite(h.out, model.NewDiscover(h.path, h.referrers), h.template)
}
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/output"
)

// manifestFetchHandler handles JSON metadata output for manifest fetch events.
Expand All @@ -44,5 +45,5 @@ func (h *manifestFetchHandler) OnFetched(path string, desc ocispec.Descriptor, c
if err := json.Unmarshal(content, &manifest); err != nil {
manifest = nil
}
return parseAndWrite(h.out, model.NewFetched(path, desc, manifest), h.template)
return output.ParseAndWrite(h.out, model.NewFetched(path, desc, manifest), h.template)
}
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/metadata/template/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
)

// PullHandler handles text metadata output for pull events.
Expand All @@ -34,7 +35,7 @@ type PullHandler struct {

// OnCompleted implements metadata.PullHandler.
func (ph *PullHandler) OnCompleted(opts *option.Target, desc ocispec.Descriptor) error {
return parseAndWrite(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()), ph.template)
return output.ParseAndWrite(ph.out, model.NewPull(ph.path+"@"+desc.Digest.String(), ph.pulled.Files()), ph.template)
}

// OnFilePulled implements metadata.PullHandler.
Expand Down
5 changes: 3 additions & 2 deletions cmd/oras/internal/display/metadata/template/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/metadata/model"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/contentutil"
)

Expand All @@ -47,7 +48,7 @@ func (ph *PushHandler) OnTagged(desc ocispec.Descriptor, tag string) error {
return nil
}

// OnStarted is called after files are copied.
// OnCopied is called after files are copied.
func (ph *PushHandler) OnCopied(opts *option.Target) error {
if opts.RawReference != "" && !contentutil.IsDigest(opts.Reference) {
ph.tagged.AddTag(opts.Reference)
Expand All @@ -58,5 +59,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 parseAndWrite(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()), ph.template)
return output.ParseAndWrite(ph.out, model.NewPush(root, ph.path, ph.tagged.Tags()), ph.template)
}
4 changes: 2 additions & 2 deletions cmd/oras/internal/option/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"io"

"github.com/spf13/pflag"
"oras.land/oras/cmd/oras/internal/display/utils"
"oras.land/oras/cmd/oras/internal/output"
)

// Pretty option struct.
Expand All @@ -35,5 +35,5 @@ func (opts *Pretty) ApplyFlags(fs *pflag.FlagSet) {
// Output outputs the prettified content if `--pretty` flag is used. Otherwise
// outputs the original content.
func (opts *Pretty) Output(w io.Writer, content []byte) error {
return utils.PrintJSON(w, content, opts.Pretty)
return output.PrintJSON(w, content, opts.Pretty)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package utils
package output

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package template
package output

import (
"io"
"text/template"

"github.com/Masterminds/sprig/v3"
"oras.land/oras/cmd/oras/internal/display/utils"
)

func parseAndWrite(out io.Writer, object any, templateStr string) error {
func ParseAndWrite(out io.Writer, object any, templateStr string) error {
// parse template
t, err := template.New("format output").Funcs(sprig.FuncMap()).Parse(templateStr)
if err != nil {
return err
}
// convert object to map[string]any
converted, err := utils.ToMap(object)
converted, err := ToMap(object)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package template
package output

import (
"os"
"testing"
)

func Test_parseAndWrite_err(t *testing.T) {
if err := parseAndWrite(os.Stdout, func() {}, ""); err == nil {
if err := ParseAndWrite(os.Stdout, func() {}, ""); err == nil {
t.Errorf("should return error")
}
}
Loading