handle stream closing better

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 7f9d150a33
commit 8ea717e6a9
2 changed files with 11 additions and 9 deletions

View File

@ -40,23 +40,26 @@ func (c *Command) Execute(args []string) (err error) {
} }
defer func() { err = multierr.Append(err, rd.Close()) }() defer func() { err = multierr.Append(err, rd.Close()) }()
var out io.WriteCloser var out io.Writer
outf := string(c.PositionalArgs.Outfile) var outf *os.File
if outf == "-" { if fname := string(c.PositionalArgs.Outfile); fname == "-" {
out = os.Stdout outf = os.Stdout
} else { } else {
out, err = os.Create(outf) outf, err = os.Create(fname)
if err != nil { if err != nil {
return err return err
} }
} }
defer func() { err = multierr.Append(err, out.Close()) }() out = outf
defer func() { err = multierr.Append(err, outf.Close()) }()
if c.Xz { if c.Xz {
if out, err = xz.NewWriter(out); err != nil { outz, err := xz.NewWriter(out)
if err != nil {
return err return err
} }
defer func() { err = multierr.Append(err, out.Close()) }() defer func() { err = multierr.Append(err, outz.Close()) }()
out = outz
} }
if _, err := c.rootfsNew(rd).WriteTo(out); err != nil { if _, err := c.rootfsNew(rd).WriteTo(out); err != nil {

View File

@ -3,5 +3,4 @@ package cmdrootfs
import "testing" import "testing"
func TestExecute(t *testing.T) { func TestExecute(t *testing.T) {
} }