-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #77: Possibility to read xml content from stdin * Remove commented out code * Remove commented out code
- Loading branch information
Showing
8 changed files
with
185 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
package formatter | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"io" | ||
"os" | ||
) | ||
|
||
// OutputFile describes output file name (full path) | ||
type OutputFile string | ||
|
||
// InputFile describes input file (nmap XML full path) | ||
type InputFile string | ||
|
||
type InputFileConfig struct { | ||
Path string | ||
IsStdin bool | ||
Source io.Reader | ||
} | ||
|
||
// 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) | ||
f.Close() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package formatter | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func TestInputFileConfig_ExistsOpen(t *testing.T) { | ||
type fields struct { | ||
Path string | ||
IsStdin bool | ||
} | ||
beforeFunc := func(path string, t *testing.T) { | ||
f, err := os.Create(path) | ||
if err != nil { | ||
t.Errorf("error creating temporary file: %s", err) | ||
} | ||
defer f.Close() | ||
} | ||
afterFunc := func(name string) { | ||
os.Remove(name) | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantErr bool | ||
file string | ||
runBefore bool | ||
runAfter bool | ||
before func(path string, t *testing.T) | ||
after func(path string) | ||
}{ | ||
{ | ||
name: "File does not exist", | ||
fields: fields{ | ||
Path: "", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "File exists", | ||
fields: fields{ | ||
Path: path.Join(os.TempDir(), "inputfile_config_test_exists_2.txt"), | ||
}, | ||
wantErr: false, | ||
file: path.Join(os.TempDir(), "inputfile_config_test_exists_2.txt"), | ||
before: beforeFunc, | ||
after: afterFunc, | ||
runBefore: true, | ||
runAfter: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.runBefore { | ||
tt.before(tt.file, t) | ||
} | ||
if tt.runAfter { | ||
defer tt.after(tt.file) | ||
} | ||
i := &InputFileConfig{ | ||
Path: tt.fields.Path, | ||
IsStdin: tt.fields.IsStdin, | ||
} | ||
if err := i.ExistsOpen(); (err != nil) != tt.wantErr { | ||
t.Errorf("InputFileConfig.ExistsOpen() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters