Skip to content

Commit 76b2f95

Browse files
committed
#11: Implement json-pretty print option
1 parent e5beda4 commit 76b2f95

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ nmap-formatter [path-to-nmap.xml] md > some-markdown.md
4949
* `--skip-down-hosts` skips hosts that are down
5050
* Applicable in: `html`, `md`, `csv`
5151
* Default: `true`
52+
* `--json-pretty` pretty-prints JSON
53+
* Applicable in: `json`
54+
* Default: `true`
5255

5356
## Installation
5457

cmd/root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package cmd
2+
13
/*
24
Copyright © 2021 vdjagilev
35
@@ -19,10 +21,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1921
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2022
THE SOFTWARE.
2123
*/
22-
package cmd
2324

2425
import (
25-
_ "embed"
2626
"errors"
2727
"fmt"
2828
"log"
@@ -61,7 +61,10 @@ func init() {
6161
log.SetOutput(os.Stderr)
6262

6363
rootCmd.Flags().StringVarP((*string)(&config.OutputFile), "file", "f", "", "-f output-file (by default \"\" will output to STDOUT)")
64+
65+
// Some options related to the output
6466
rootCmd.Flags().BoolVar(&config.OutputOptions.SkipDownHosts, "skip-down-hosts", true, "--skip-down-hosts=false")
67+
rootCmd.Flags().BoolVar(&config.OutputOptions.JSONPrettyPrint, "json-pretty", true, "--json-pretty=false (pretty prints JSON output)")
6568

6669
workflow = &formatter.MainWorkflow{}
6770
}

formatter/formatter_json.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ type JSONFormatter struct {
1313
// Format the data and output it to appropriate io.Writer
1414
func (f *JSONFormatter) Format(td *TemplateData) (err error) {
1515
jsonData := new(bytes.Buffer)
16-
err = json.NewEncoder(jsonData).Encode(td.NMAPRun)
16+
encoder := json.NewEncoder(jsonData)
17+
if td.OutputOptions.JSONPrettyPrint {
18+
// space size = 2
19+
encoder.SetIndent("", " ")
20+
}
21+
err = encoder.Encode(td.NMAPRun)
1722
if err != nil {
1823
return err
1924
}

formatter/formatter_json_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,59 @@ func TestJSONFormatter_Format(t *testing.T) {
3434
err: nil,
3535
wantOutput: "{\"Scanner\":\"\",\"Args\":\"\",\"Start\":\"\",\"StartStr\":\"\",\"Version\":\"\",\"ScanInfo\":{\"Type\":\"\",\"Protocol\":\"\",\"NumServices\":\"\",\"Services\":\"\"},\"Host\":null,\"Verbose\":{\"Level\":\"\"},\"Debugging\":{\"Level\":\"\"},\"RunStats\":{\"Finished\":{\"Time\":\"\",\"TimeStr\":\"\",\"Elapsed\":\"\",\"Summary\":\"\",\"Exit\":\"\"},\"Hosts\":{\"Up\":\"\",\"Down\":\"\",\"Total\":\"\"}}}\n",
3636
},
37+
{
38+
name: "Empty output (with intend)",
39+
f: &JSONFormatter{
40+
&Config{
41+
Writer: &writer,
42+
},
43+
},
44+
args: args{
45+
td: &TemplateData{
46+
NMAPRun: NMAPRun{},
47+
OutputOptions: OutputOptions{
48+
JSONPrettyPrint: true,
49+
},
50+
},
51+
},
52+
wantErr: false,
53+
err: nil,
54+
wantOutput: `{
55+
"Scanner": "",
56+
"Args": "",
57+
"Start": "",
58+
"StartStr": "",
59+
"Version": "",
60+
"ScanInfo": {
61+
"Type": "",
62+
"Protocol": "",
63+
"NumServices": "",
64+
"Services": ""
65+
},
66+
"Host": null,
67+
"Verbose": {
68+
"Level": ""
69+
},
70+
"Debugging": {
71+
"Level": ""
72+
},
73+
"RunStats": {
74+
"Finished": {
75+
"Time": "",
76+
"TimeStr": "",
77+
"Elapsed": "",
78+
"Summary": "",
79+
"Exit": ""
80+
},
81+
"Hosts": {
82+
"Up": "",
83+
"Down": "",
84+
"Total": ""
85+
}
86+
}
87+
}
88+
`,
89+
},
3790
{
3891
name: "Error",
3992
f: &JSONFormatter{

formatter/output_options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ package formatter
44
type OutputOptions struct {
55
// The hosts that are down wont be displayed in the TOC
66
SkipDownHosts bool
7+
// JSONPrettyPrint defines if JSON output would be pretty-printed (human-readable) or not (machine readable)
8+
JSONPrettyPrint bool
79
}

0 commit comments

Comments
 (0)