nuclei/v2/pkg/output/file_output_writer.go

37 lines
801 B
Go
Raw Normal View History

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
}
// 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
}
// WriteString writes an output to the underlying file
func (w *fileWriter) Write(data []byte) error {
2021-02-26 09:15:48 +00:00
_, err := w.file.Write(data)
if err != nil {
return err
}
2021-02-26 09:15:48 +00:00
_, err = w.file.Write([]byte("\n"))
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()
}