From 76b2f95292806ef7d1430a80a5bb6e094d7f611a Mon Sep 17 00:00:00 2001 From: vdjagilev <2762286+vdjagilev@users.noreply.github.com> Date: Mon, 19 Jul 2021 03:25:10 +0300 Subject: [PATCH] #11: Implement json-pretty print option --- README.md | 3 ++ cmd/root.go | 7 +++-- formatter/formatter_json.go | 7 ++++- formatter/formatter_json_test.go | 53 ++++++++++++++++++++++++++++++++ formatter/output_options.go | 2 ++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b2bbb21..15eb761 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,9 @@ nmap-formatter [path-to-nmap.xml] md > some-markdown.md * `--skip-down-hosts` skips hosts that are down * Applicable in: `html`, `md`, `csv` * Default: `true` +* `--json-pretty` pretty-prints JSON + * Applicable in: `json` + * Default: `true` ## Installation diff --git a/cmd/root.go b/cmd/root.go index 94531e4..00c48e8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,3 +1,5 @@ +package cmd + /* Copyright © 2021 vdjagilev @@ -19,10 +21,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package cmd import ( - _ "embed" "errors" "fmt" "log" @@ -61,7 +61,10 @@ func init() { log.SetOutput(os.Stderr) rootCmd.Flags().StringVarP((*string)(&config.OutputFile), "file", "f", "", "-f output-file (by default \"\" will output to STDOUT)") + + // Some options related to the output rootCmd.Flags().BoolVar(&config.OutputOptions.SkipDownHosts, "skip-down-hosts", true, "--skip-down-hosts=false") + rootCmd.Flags().BoolVar(&config.OutputOptions.JSONPrettyPrint, "json-pretty", true, "--json-pretty=false (pretty prints JSON output)") workflow = &formatter.MainWorkflow{} } diff --git a/formatter/formatter_json.go b/formatter/formatter_json.go index 7c46196..6bdaa6e 100644 --- a/formatter/formatter_json.go +++ b/formatter/formatter_json.go @@ -13,7 +13,12 @@ type JSONFormatter struct { // Format the data and output it to appropriate io.Writer func (f *JSONFormatter) Format(td *TemplateData) (err error) { jsonData := new(bytes.Buffer) - err = json.NewEncoder(jsonData).Encode(td.NMAPRun) + encoder := json.NewEncoder(jsonData) + if td.OutputOptions.JSONPrettyPrint { + // space size = 2 + encoder.SetIndent("", " ") + } + err = encoder.Encode(td.NMAPRun) if err != nil { return err } diff --git a/formatter/formatter_json_test.go b/formatter/formatter_json_test.go index 8f3f080..dfdbdea 100644 --- a/formatter/formatter_json_test.go +++ b/formatter/formatter_json_test.go @@ -34,6 +34,59 @@ func TestJSONFormatter_Format(t *testing.T) { err: nil, 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", }, + { + name: "Empty output (with intend)", + f: &JSONFormatter{ + &Config{ + Writer: &writer, + }, + }, + args: args{ + td: &TemplateData{ + NMAPRun: NMAPRun{}, + OutputOptions: OutputOptions{ + JSONPrettyPrint: true, + }, + }, + }, + wantErr: false, + err: nil, + 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": "" + } + } +} +`, + }, { name: "Error", f: &JSONFormatter{ diff --git a/formatter/output_options.go b/formatter/output_options.go index 64f9c47..9cac69b 100644 --- a/formatter/output_options.go +++ b/formatter/output_options.go @@ -4,4 +4,6 @@ package formatter type OutputOptions struct { // The hosts that are down wont be displayed in the TOC SkipDownHosts bool + // JSONPrettyPrint defines if JSON output would be pretty-printed (human-readable) or not (machine readable) + JSONPrettyPrint bool }