Remove dependency on config in output.go (#250)
Refactor the output handlers to take the necessary writers as arguments. https://github.com/zmap/zgrab2/pull/250
This commit is contained in:
parent
ef33737c05
commit
b2bf9cbc41
@ -89,7 +89,8 @@ func validateFrameworkConfiguration() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
SetOutputFunc(OutputResultsFile)
|
||||
outputFunc := OutputResultsWriterFunc(config.outputFile)
|
||||
SetOutputFunc(outputFunc)
|
||||
|
||||
if config.MetaFileName == "-" {
|
||||
config.metaFile = os.Stderr
|
||||
|
22
output.go
22
output.go
@ -3,6 +3,7 @@ package zgrab2
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// FlagMap is a function that maps a single-bit bitmask (i.e. a number of the
|
||||
@ -133,16 +134,23 @@ func WidenMapKeys(input map[int]string) map[uint64]string {
|
||||
// results or error.
|
||||
type OutputResultsFunc func(results <-chan []byte) error
|
||||
|
||||
// OutputResultsFile is an OutputResultsFunc that write results to
|
||||
// a filename provided on the command line.
|
||||
func OutputResultsFile(results <-chan []byte) error {
|
||||
out := bufio.NewWriter(config.outputFile)
|
||||
defer out.Flush()
|
||||
// OutputResultsWriterFunc returns an OutputResultsFunc that wraps an io.Writer
|
||||
// in a buffered writer, and uses OutputResults.
|
||||
func OutputResultsWriterFunc(w io.Writer) OutputResultsFunc {
|
||||
buf := bufio.NewWriter(w)
|
||||
return func(result <-chan []byte) error {
|
||||
defer buf.Flush()
|
||||
return OutputResults(buf, result)
|
||||
}
|
||||
}
|
||||
|
||||
// OutputResults writes results to a buffered Writer from a channel.
|
||||
func OutputResults(w *bufio.Writer, results <-chan []byte) error {
|
||||
for result := range results {
|
||||
if _, err := out.Write(result); err != nil {
|
||||
if _, err := w.Write(result); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := out.WriteByte('\n'); err != nil {
|
||||
if err := w.WriteByte('\n'); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user