File tree Expand file tree Collapse file tree 3 files changed +18
-22
lines changed
Expand file tree Collapse file tree 3 files changed +18
-22
lines changed Original file line number Diff line number Diff line change 11package formatter
22
33import (
4- "bufio"
5- "errors"
64 "io"
75 "os"
86)
@@ -20,21 +18,6 @@ type InputFileConfig struct {
2018 Source io.ReadCloser
2119}
2220
23- // ReadContents reads content from stdin or provided file-path
24- func (i * InputFileConfig ) ReadContents () ([]byte , error ) {
25- var err error
26- var content []byte
27- if i .Source == nil {
28- return nil , errors .New ("no reading source is defined" )
29- }
30- scanner := bufio .NewScanner (i .Source )
31- for scanner .Scan () {
32- content = append (content , scanner .Bytes ()... )
33- }
34- err = scanner .Err ()
35- return content , err
36- }
37-
3821// ExistsOpen tries to open a file for reading, returning an error if it fails
3922func (i * InputFileConfig ) ExistsOpen () error {
4023 f , err := os .Open (i .Path )
Original file line number Diff line number Diff line change @@ -86,12 +86,15 @@ func (w *MainWorkflow) Execute() (err error) {
8686
8787// parse reads & unmarshalles the input file into NMAPRun struct
8888func (w * MainWorkflow ) parse () (run NMAPRun , err error ) {
89- input , err := w .Config .InputFileConfig .ReadContents ()
90- if err != nil {
91- return
89+ if w .Config .InputFileConfig .Source == nil {
90+ return run , fmt .Errorf ("no input file is defined" )
9291 }
93- if err = xml .Unmarshal (input , & run ); err != nil {
92+ d := xml .NewDecoder (w .Config .InputFileConfig .Source )
93+ _ , err = d .Token ()
94+ if err != nil {
9495 return
9596 }
96- return run , nil
97+
98+ err = d .Decode (& run )
99+ return
97100}
Original file line number Diff line number Diff line change @@ -50,6 +50,16 @@ func TestMainWorkflow_parse(t *testing.T) {
5050 <nmaprun></nmaprun>` ,
5151 fileName : "main_workflow_parse_3_test" ,
5252 },
53+ {
54+ name : "Bad XML file" ,
55+ w : & MainWorkflow {
56+ Config : & Config {},
57+ },
58+ wantNMAPRun : NMAPRun {},
59+ wantErr : true ,
60+ fileContent : "<?x< version=" ,
61+ fileName : "main_workflow_parse_4_test_wrong_xml" ,
62+ },
5363 {
5464 name : "XML file with some matching output" ,
5565 w : & MainWorkflow {
You can’t perform that action at this time.
0 commit comments