2020-12-20 18:34:11 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fileWriter is a concurrent file based output writer.
|
|
|
|
type fileWriter struct {
|
2021-02-26 09:15:48 +00:00
|
|
|
file *os.File
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileOutputWriter creates a new buffered writer for a file
|
|
|
|
func newFileOutputWriter(file string) (*fileWriter, error) {
|
|
|
|
output, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-26 09:15:48 +00:00
|
|
|
return &fileWriter{file: output}, nil
|
2020-12-20 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteString writes an output to the underlying file
|
|
|
|
func (w *fileWriter) Write(data []byte) error {
|
2021-08-31 09:55:52 +00:00
|
|
|
if _, err := w.file.Write(data); err != nil {
|
2020-12-20 18:34:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-31 09:55:52 +00:00
|
|
|
_, err := w.file.Write([]byte("\n"))
|
2020-12-20 18:34:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying writer flushing everything to disk
|
|
|
|
func (w *fileWriter) Close() error {
|
|
|
|
//nolint:errcheck // we don't care whether sync failed or succeeded.
|
|
|
|
w.file.Sync()
|
|
|
|
return w.file.Close()
|
|
|
|
}
|