Passed
Pull Request — master (#31)
by Stefano
02:08
created

cmd.NewScanCommand   B

Complexity

Conditions 3

Size

Total Lines 65
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 46
nop 1
dl 0
loc 65
rs 8.7672
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package cmd
2
3
import (
4
	"net/url"
5
6
	"github.com/chuckpreslar/emission"
7
8
	"github.com/pkg/errors"
9
	"github.com/sirupsen/logrus"
10
	"github.com/spf13/cobra"
11
	"github.com/stefanoj3/dirstalk/pkg/scan"
12
)
13
14
func NewScanCommand(logger *logrus.Logger) (*cobra.Command, error) {
0 ignored issues
show
introduced by
exported function NewScanCommand should have comment or be unexported
Loading history...
15
	cmd := &cobra.Command{
16
		Use:   "scan [url]",
17
		Short: "Scan the given URL",
18
		RunE:  buildScanFunction(logger),
19
	}
20
21
	cmd.Flags().StringP(
22
		flagDictionary,
23
		flagDictionaryShort,
24
		"",
25
		"dictionary to use for the scan (path to local file or remote url)",
26
	)
27
	err := cmd.MarkFlagFilename(flagDictionary)
28
	if err != nil {
29
		return nil, err
30
	}
31
32
	err = cmd.MarkFlagRequired(flagDictionary)
33
	if err != nil {
34
		return nil, err
35
	}
36
37
	cmd.Flags().StringSlice(
38
		flagHTTPMethods,
39
		[]string{"GET"},
40
		"comma separated list of http methods to use; eg: GET,POST,PUT",
41
	)
42
43
	cmd.Flags().IntP(
44
		flagThreads,
45
		flagThreadsShort,
46
		3,
47
		"amount of threads for concurrent requests",
48
	)
49
50
	cmd.Flags().IntP(
51
		flagHTTPTimeout,
52
		"",
53
		5000,
54
		"timeout in milliseconds",
55
	)
56
57
	cmd.Flags().IntP(
58
		flagScanDepth,
59
		"",
60
		3,
61
		"scan depth",
62
	)
63
64
	cmd.Flags().StringP(
65
		flagSocks5Host,
66
		"",
67
		"",
68
		"socks5 host to use",
69
	)
70
71
	cmd.Flags().StringP(
72
		flagUserAgent,
73
		"",
74
		"",
75
		"user agent to use for http requests",
76
	)
77
78
	return cmd, nil
79
}
80
81
func buildScanFunction(logger *logrus.Logger) func(cmd *cobra.Command, args []string) error {
82
	f := func(cmd *cobra.Command, args []string) error {
83
		u, err := getURL(args)
84
		if err != nil {
85
			return err
86
		}
87
88
		cnf, err := scanConfigFromCmd(cmd)
89
		if err != nil {
90
			return errors.Wrap(err, "failed to build config")
91
		}
92
93
		eventManager := emission.NewEmitter()
94
		printer := scan.NewResultLogger(logger)
95
		eventManager.On(scan.EventResultFound, printer.Log)
96
97
		summarizer := scan.NewResultSummarizer(logger.Out)
98
		eventManager.On(scan.EventResultFound, summarizer.Add)
99
100
		defer summarizer.Summarize()
101
102
		return scan.StartScan(logger, eventManager, cnf, u)
103
	}
104
105
	return f
106
}
107
108
func getURL(args []string) (*url.URL, error) {
109
	if len(args) == 0 {
110
		return nil, errors.New("no URL provided")
111
	}
112
113
	arg := args[0]
114
115
	u, err := url.ParseRequestURI(arg)
116
	if err != nil {
117
		return nil, errors.Wrap(err, "the first argument must be a valid url")
118
	}
119
120
	return u, nil
121
}
122