Skip to content

Commit

Permalink
Merge pull request #9 from opcr-io/179824081/save_to_stdout
Browse files Browse the repository at this point in the history
179824081/save to stdout
  • Loading branch information
viovanov authored Oct 8, 2021
2 parents 50ec30c + 11ae9e9 commit 9d8e57e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/policy/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

type SaveCmd struct {
Policy string `arg:"" name:"policy" help:"Policy to save."`
File string `name:"file" short:"f" help:"Output file path" default:"bundle.tar.gz"`
File string `name:"file" short:"f" help:"Output file path, '-' is accepted for stdout" default:"bundle.tar.gz"`
}

func (c *SaveCmd) Run(g *Globals) error {
Expand Down
47 changes: 31 additions & 16 deletions pkg/app/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"io"
"os"

v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"oras.land/oras-go/pkg/content"
)

func (c *PolicyApp) Save(userRef, outputFile string) error {
defer c.Cancel()
var o *os.File

ref, err := c.calculatePolicyRef(userRef)
if err != nil {
Expand All @@ -33,30 +35,44 @@ func (c *PolicyApp) Save(userRef, outputFile string) error {
return errors.Errorf("policy [%s] not found in the local store", ref)
}

c.UI.Normal().
WithStringValue("digest", refDescriptor.Digest.String()).
Msgf("Resolved ref [%s].", ref)
if outputFile == "-" {
o = os.Stdout
} else {
c.UI.Normal().
WithStringValue("digest", refDescriptor.Digest.String()).
Msgf("Resolved ref [%s].", ref)
o, err = os.Create(outputFile)

reader, err := ociStore.ReaderAt(c.Context, refDescriptor)
if err != nil {
return errors.Wrapf(err, "failed to create output file [%s]", outputFile)
}

defer func() {
err := o.Close()
if err != nil {
c.UI.Problem().WithErr(err).Msg("Failed to close policy bundle tarball.")
}
}()
}

err = c.writePolicy(ociStore, &refDescriptor, o)
if err != nil {
return errors.Wrap(err, "failed to open store reader")
return err
}

defer func() {
err := reader.Close()
if err != nil {
c.UI.Problem().WithErr(err).Msg("Failed to close OCI policy reader.")
}
}()
return nil
}

o, err := os.Create(outputFile)
func (c *PolicyApp) writePolicy(ociStore *content.OCIStore, refDescriptor *v1.Descriptor, o *os.File) error {
reader, err := ociStore.ReaderAt(c.Context, *refDescriptor)
if err != nil {
return errors.Wrapf(err, "failed to create output file [%s]", outputFile)
return errors.Wrap(err, "failed to open store reader")
}

defer func() {
err := o.Close()
err := reader.Close()
if err != nil {
c.UI.Problem().WithErr(err).Msg("Failed to close policy bundle tarball.")
c.UI.Problem().WithErr(err).Msg("Failed to close OCI policy reader.")
}
}()

Expand Down Expand Up @@ -84,6 +100,5 @@ func (c *PolicyApp) Save(userRef, outputFile string) error {

i += chunkSize
}

return nil
}

0 comments on commit 9d8e57e

Please sign in to comment.