main.go: remove file if on Flatten(..) failure

When a Flatten() fails, the main program used to leave an incomplete
file (usually zero-length). Now it will clean itself up on failure.
This commit is contained in:
2021-08-25 08:49:30 +03:00
parent 6f40bc91be
commit 4568429a69
2 changed files with 60 additions and 16 deletions

16
main.go
View File

@@ -53,15 +53,15 @@ type command struct {
Stdout io.Writer
}
func (c *command) execute(infile string, outfile string) (err error) {
func (c *command) execute(infile string, outfile string) (_err error) {
rd, err := os.Open(infile)
if err != nil {
return err
}
defer func() {
err1 := rd.Close()
if err == nil {
err = err1
err := rd.Close()
if _err == nil {
_err = err
}
}()
@@ -74,9 +74,11 @@ func (c *command) execute(infile string, outfile string) (err error) {
return fmt.Errorf("create: %w", err)
}
defer func() {
err1 := outf.Close()
if err == nil {
err = err1
err := outf.Close()
if _err != nil {
os.Remove(outfile)
} else {
_err = err
}
}()
out = outf