Skip to content

Commit 1a55492

Browse files
committed
Refactor Execute() function to avoid many return statements
1 parent 83f5532 commit 1a55492

12 files changed

+119
-78
lines changed

cmd/root.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,20 @@ func run(cmd *cobra.Command, args []string) error {
153153
}
154154

155155
workflow.SetConfig(&config)
156+
workflow.SetInputFile()
157+
workflow.SetOutputFile()
156158

157159
err = workflow.Execute()
158160
if err != nil {
159161
return err
160162
}
161-
return nil
162-
}
163163

164-
// validate is checking input from the command line
165-
func validate(config formatter.Config) error {
166-
if !config.OutputFormat.IsValid() {
167-
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv", config.OutputFormat)
164+
if config.Writer != nil {
165+
config.Writer.Close()
168166
}
169-
170-
// Checking if xml file is readable
171-
if !config.InputFileConfig.IsStdin {
172-
err := config.InputFileConfig.ExistsOpen()
173-
if err != nil {
174-
return fmt.Errorf("could not open XML file: %v", err)
175-
}
167+
if config.InputFileConfig.Source != nil {
168+
config.InputFileConfig.Source.Close()
176169
}
177170

178-
// Checking if custom template is existing and readable and
179-
if config.TemplatePath != "" {
180-
switch config.OutputFormat {
181-
case formatter.CSVOutput:
182-
case formatter.JSONOutput:
183-
return fmt.Errorf("cannot set templates for the formats other than HTML or Markdown")
184-
}
185-
file, err := os.Open(config.TemplatePath)
186-
if err != nil {
187-
return fmt.Errorf("could not read template file: %v", err)
188-
}
189-
defer file.Close()
190-
}
191171
return nil
192172
}

cmd/root_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ func Test_run(t *testing.T) {
228228
config: formatter.Config{
229229
OutputFormat: "html",
230230
ShowVersion: false, // false by default
231+
Writer: os.Stdout,
232+
InputFileConfig: formatter.InputFileConfig{
233+
Source: os.Stdin,
234+
},
231235
},
232236
args: args{},
233237
wantErr: false,
@@ -258,6 +262,14 @@ func (w *testWorkflow) SetConfig(c *formatter.Config) {
258262
log.Println("testWorkflow -> SetConfig")
259263
}
260264

265+
func (w *testWorkflow) SetInputFile() {
266+
log.Println("testWorkflow -> SetInputFile")
267+
}
268+
269+
func (w *testWorkflow) SetOutputFile() {
270+
log.Println("testWorkflow -> SetOutputFile")
271+
}
272+
261273
func Test_shouldShowVersion(t *testing.T) {
262274
type args struct {
263275
c *formatter.Config

cmd/validation.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/vdjagilev/nmap-formatter/formatter"
8+
)
9+
10+
// validate is checking input from the command line
11+
func validate(config formatter.Config) error {
12+
if !config.OutputFormat.IsValid() {
13+
return fmt.Errorf("not valid format: %s, please choose html/json/md/csv", config.OutputFormat)
14+
}
15+
16+
err := validateIOFiles(config)
17+
if err != nil {
18+
return err
19+
}
20+
21+
return validateTemplateConfig(config)
22+
}
23+
24+
// validateIOFiles validates whether Input files and output files exists/have permissions to be created
25+
func validateIOFiles(config formatter.Config) error {
26+
// Checking if xml file is readable
27+
if !config.InputFileConfig.IsStdin {
28+
err := config.InputFileConfig.ExistsOpen()
29+
if err != nil {
30+
return fmt.Errorf("could not open XML file: %v", err)
31+
}
32+
}
33+
// Checking if output file can be created and does not exist already
34+
// If OutputFile == "", it means that all output goes to stdout, no check needed
35+
if config.OutputFile != "" {
36+
outputFile, err := os.OpenFile(string(config.OutputFile), os.O_EXCL|os.O_WRONLY|os.O_CREATE, os.ModePerm)
37+
if err != nil {
38+
return fmt.Errorf("unable to create output file: %s", err)
39+
}
40+
outputFile.Close()
41+
os.Remove(string(config.OutputFile))
42+
}
43+
return nil
44+
}
45+
46+
// validateTemplateConfig validates template config, if it has adequate output format configs and is readable
47+
func validateTemplateConfig(config formatter.Config) error {
48+
// Checking if custom template is existing and readable and
49+
if config.TemplatePath != "" {
50+
switch config.OutputFormat {
51+
case formatter.CSVOutput:
52+
case formatter.JSONOutput:
53+
return fmt.Errorf("cannot set templates for the formats other than HTML or Markdown")
54+
}
55+
file, err := os.Open(config.TemplatePath)
56+
if err != nil {
57+
return fmt.Errorf("could not read template file: %v", err)
58+
}
59+
defer file.Close()
60+
}
61+
return nil
62+
}

formatter/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// where output will be delivered, desired output format, input file path, output file path
1111
// and different output options
1212
type Config struct {
13-
Writer io.Writer
13+
Writer io.WriteCloser
1414
OutputFormat OutputFormat
1515
InputFileConfig InputFileConfig
1616
OutputFile OutputFile

formatter/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestConfig_CustomOptionsMap(t *testing.T) {
1010
type fields struct {
11-
Writer io.Writer
11+
Writer io.WriteCloser
1212
OutputFormat OutputFormat
1313
InputFileConfig InputFileConfig
1414
OutputFile OutputFile

formatter/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type InputFile string
1616
type InputFileConfig struct {
1717
Path string
1818
IsStdin bool
19-
Source io.Reader
19+
Source io.ReadCloser
2020
}
2121

2222
// ReadContents reads content from stdin or provided file-path

formatter/formatter_csv_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,7 @@ func (w *csvMockedWriter) Write(p []byte) (n int, err error) {
617617
w.data = p
618618
return len(p), nil
619619
}
620+
621+
func (w *csvMockedWriter) Close() error {
622+
return nil
623+
}

formatter/formatter_html_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (f *testHTMLMockedFormatterWriter) Write(p []byte) (n int, err error) {
2222
return len(p), nil
2323
}
2424

25+
func (f *testHTMLMockedFormatterWriter) Close() error {
26+
return nil
27+
}
28+
2529
func TestHTMLFormatter_Format(t *testing.T) {
2630
type args struct {
2731
td *TemplateData

formatter/formatter_json_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ func (w *jsonMockedWriter) Write(p []byte) (n int, err error) {
131131
w.data = p
132132
return len(p), nil
133133
}
134+
135+
func (w *jsonMockedWriter) Close() error {
136+
return nil
137+
}

formatter/formatter_md_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func (w *markdownMockedWriter) Write(p []byte) (n int, err error) {
130130
return len(p), nil
131131
}
132132

133+
func (w *markdownMockedWriter) Close() error {
134+
return nil
135+
}
136+
133137
func TestMarkdownFormatter_Format(t *testing.T) {
134138
type args struct {
135139
td *TemplateData

0 commit comments

Comments
 (0)