Skip to content

Commit

Permalink
#11: Implement json-pretty print option
Browse files Browse the repository at this point in the history
  • Loading branch information
vdjagilev committed Jul 19, 2021
1 parent e5beda4 commit 76b2f95
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package cmd

/*
Copyright © 2021 vdjagilev
Expand All @@ -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"
Expand Down Expand Up @@ -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{}
}
Expand Down
7 changes: 6 additions & 1 deletion formatter/formatter_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
53 changes: 53 additions & 0 deletions formatter/formatter_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions formatter/output_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 76b2f95

Please # to comment.