diff --git a/formatter/formatter.go b/formatter/formatter.go index 20870de..ba2a300 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -1,11 +1,5 @@ package formatter -import ( - "encoding/xml" - "fmt" - "os" -) - // New returns new instance of formatter the exact struct // of formatter would depend on provided config func New(config *Config) Formatter { @@ -30,73 +24,6 @@ func New(config *Config) Formatter { return nil } -type Workflow interface { - Execute() (err error) - SetConfig(c *Config) -} - -type MainWorkflow struct { - Config *Config -} - -func (w *MainWorkflow) SetConfig(c *Config) { - w.Config = c -} - -// Execute is the core of the application which executes required steps -// one-by-one to achieve formatting from input -> output. -func (w *MainWorkflow) Execute() (err error) { - // If no output file has been provided all content - // goes to the STDOUT - if w.Config.OutputFile == "" { - w.Config.Writer = os.Stdout - } else { - // Open output file for writing, produces an error if file already exists - // This won't work if user redirects output to some file using ">" or ">>" - f, err := os.OpenFile(string(w.Config.OutputFile), os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm) - if err != nil { - return err - } - defer f.Close() - w.Config.Writer = f - } - - // Reading & parsing the input file - NMAPRun, err := w.parse() - if err != nil { - return - } - - // Build template data with NMAPRun entry & various output options - templateData := TemplateData{ - NMAPRun: NMAPRun, - OutputOptions: w.Config.OutputOptions, - } - - // Getting new instance of formatter based on provided config - formatter := New(w.Config) - - // This part usually should not happen - if formatter == nil { - return fmt.Errorf("no formatter is defined") - } - - err = formatter.Format(&templateData) - return -} - -// parse reads & unmarshalles the input file into NMAPRun struct -func (w *MainWorkflow) parse() (NMAPRun NMAPRun, err error) { - input, err := os.ReadFile(string(w.Config.InputFile)) - if err != nil { - return - } - if err = xml.Unmarshal(input, &NMAPRun); err != nil { - return - } - return NMAPRun, nil -} - type Formatter interface { // Format the data and output it to appropriate io.Writer Format(td *TemplateData) error diff --git a/formatter/workflow.go b/formatter/workflow.go new file mode 100644 index 0000000..0ce05ed --- /dev/null +++ b/formatter/workflow.go @@ -0,0 +1,74 @@ +package formatter + +import ( + "encoding/xml" + "fmt" + "os" +) + +type Workflow interface { + Execute() (err error) + SetConfig(c *Config) +} + +type MainWorkflow struct { + Config *Config +} + +func (w *MainWorkflow) SetConfig(c *Config) { + w.Config = c +} + +// Execute is the core of the application which executes required steps +// one-by-one to achieve formatting from input -> output. +func (w *MainWorkflow) Execute() (err error) { + // If no output file has been provided all content + // goes to the STDOUT + if w.Config.OutputFile == "" { + w.Config.Writer = os.Stdout + } else { + // Open output file for writing, produces an error if file already exists + // This won't work if user redirects output to some file using ">" or ">>" + f, err := os.OpenFile(string(w.Config.OutputFile), os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm) + if err != nil { + return err + } + defer f.Close() + w.Config.Writer = f + } + + // Reading & parsing the input file + NMAPRun, err := w.parse() + if err != nil { + return + } + + // Build template data with NMAPRun entry & various output options + templateData := TemplateData{ + NMAPRun: NMAPRun, + OutputOptions: w.Config.OutputOptions, + } + + // Getting new instance of formatter based on provided config + formatter := New(w.Config) + + // This part usually should not happen + if formatter == nil { + return fmt.Errorf("no formatter is defined") + } + + err = formatter.Format(&templateData) + return +} + +// parse reads & unmarshalles the input file into NMAPRun struct +func (w *MainWorkflow) parse() (NMAPRun NMAPRun, err error) { + input, err := os.ReadFile(string(w.Config.InputFile)) + if err != nil { + return + } + if err = xml.Unmarshal(input, &NMAPRun); err != nil { + return + } + return NMAPRun, nil +}