Skip to content

Commit a1f4598

Browse files
committed
Tweak docs and improve error handling
1 parent 2cba91c commit a1f4598

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The rewriter package is designed to make it easy to write filters that operate o
44

55
It creates an object that reads from the input file and writes to a temporary file.
66

7-
When you call Close(), both files are closed, the input is moved to the temp directory, and the output replaces it.
7+
When you call Close(), both files are closed and the output replaces the input.
88

99
If you call Abort(), the output file is deleted and the input file is unaffected.
1010

pkg/rewriter/rewriter.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"io/ioutil"
77
"os"
88
"path"
9+
10+
"github.com/pkg/errors"
911
)
1012

1113
// File is the base type of an object that reads from the input
1214
// file and writes to a temporary file.
1315
// When you call Close(), both files are closed,
14-
// the input is moved to the temp directory, and the output
15-
// replaces it.
16+
// and the output replaces the input.
1617
// If you call Abort(), the output file is deleted and the
1718
// input file is unaffected.
1819
// The object returned implements io.ReadWriteCloser.
@@ -34,11 +35,6 @@ var _ Aborter = (*File)(nil)
3435

3536
// New returns an object that reads from the input
3637
// file and writes to a temporary file.
37-
// When you call Close(), both files are closed,
38-
// the input is moved to the temp directory, and the output
39-
// replaces it.
40-
// If you call Abort(), the output file is deleted and the
41-
// input file is unaffected.
4238
// The File object returned implements io.ReadWriteCloser.
4339
func New(inputFile string) (*File, error) {
4440
input, err := os.Open(inputFile)
@@ -74,16 +70,24 @@ func (f *File) Write(b []byte) (int, error) {
7470
// Because people might call it twice (once each for in/out)
7571
// we want it to be harmless after the first time.
7672
func (f *File) Close() error {
73+
// close both files
7774
if f.input != nil {
78-
_ = f.input.Close()
75+
if err := f.input.Close(); err != nil {
76+
return errors.Wrap(err, "closing rewriter input")
77+
}
7978
f.input = nil
8079
}
81-
if f.output == nil {
82-
_ = f.output.Close()
80+
if f.output != nil {
81+
if err := f.output.Close(); err != nil {
82+
return errors.Wrap(err, "closing rewriter output")
83+
}
8384
f.output = nil
8485
}
86+
// and now move the tempfile over the original
8587
if f.tempfile != "" {
86-
os.Rename(f.tempfile, f.filename)
88+
if err := os.Rename(f.tempfile, f.filename); err != nil {
89+
return errors.Wrap(err, "renaming rewriter files")
90+
}
8791
f.tempfile = ""
8892
}
8993
return nil

0 commit comments

Comments
 (0)