File tree 3 files changed +18
-22
lines changed
3 files changed +18
-22
lines changed Original file line number Diff line number Diff line change 1
1
package formatter
2
2
3
3
import (
4
- "bufio"
5
- "errors"
6
4
"io"
7
5
"os"
8
6
)
@@ -20,21 +18,6 @@ type InputFileConfig struct {
20
18
Source io.ReadCloser
21
19
}
22
20
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
-
38
21
// ExistsOpen tries to open a file for reading, returning an error if it fails
39
22
func (i * InputFileConfig ) ExistsOpen () error {
40
23
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) {
86
86
87
87
// parse reads & unmarshalles the input file into NMAPRun struct
88
88
func (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" )
92
91
}
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 {
94
95
return
95
96
}
96
- return run , nil
97
+
98
+ err = d .Decode (& run )
99
+ return
97
100
}
Original file line number Diff line number Diff line change @@ -50,6 +50,16 @@ func TestMainWorkflow_parse(t *testing.T) {
50
50
<nmaprun></nmaprun>` ,
51
51
fileName : "main_workflow_parse_3_test" ,
52
52
},
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
+ },
53
63
{
54
64
name : "XML file with some matching output" ,
55
65
w : & MainWorkflow {
You can’t perform that action at this time.
0 commit comments