diff --git a/formatter/file.go b/formatter/file.go index 261dc07..f8ad7b9 100644 --- a/formatter/file.go +++ b/formatter/file.go @@ -1,8 +1,6 @@ package formatter import ( - "bufio" - "errors" "io" "os" ) @@ -20,21 +18,6 @@ type InputFileConfig struct { Source io.ReadCloser } -// ReadContents reads content from stdin or provided file-path -func (i *InputFileConfig) ReadContents() ([]byte, error) { - var err error - var content []byte - if i.Source == nil { - return nil, errors.New("no reading source is defined") - } - scanner := bufio.NewScanner(i.Source) - for scanner.Scan() { - content = append(content, scanner.Bytes()...) - } - err = scanner.Err() - return content, err -} - // ExistsOpen tries to open a file for reading, returning an error if it fails func (i *InputFileConfig) ExistsOpen() error { f, err := os.Open(i.Path) diff --git a/formatter/workflow.go b/formatter/workflow.go index 5a46253..82322f6 100644 --- a/formatter/workflow.go +++ b/formatter/workflow.go @@ -86,12 +86,15 @@ func (w *MainWorkflow) Execute() (err error) { // parse reads & unmarshalles the input file into NMAPRun struct func (w *MainWorkflow) parse() (run NMAPRun, err error) { - input, err := w.Config.InputFileConfig.ReadContents() - if err != nil { - return + if w.Config.InputFileConfig.Source == nil { + return run, fmt.Errorf("no input file is defined") } - if err = xml.Unmarshal(input, &run); err != nil { + d := xml.NewDecoder(w.Config.InputFileConfig.Source) + _, err = d.Token() + if err != nil { return } - return run, nil + + err = d.Decode(&run) + return } diff --git a/formatter/workflow_test.go b/formatter/workflow_test.go index b396b68..b1ff97a 100644 --- a/formatter/workflow_test.go +++ b/formatter/workflow_test.go @@ -50,6 +50,16 @@ func TestMainWorkflow_parse(t *testing.T) { `, fileName: "main_workflow_parse_3_test", }, + { + name: "Bad XML file", + w: &MainWorkflow{ + Config: &Config{}, + }, + wantNMAPRun: NMAPRun{}, + wantErr: true, + fileContent: "