| Conditions | 17 |
| Total Lines | 79 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
Complex classes like cmd.scanConfigFromCmd often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package cmd |
||
| 16 | func scanConfigFromCmd(cmd *cobra.Command) (*scan.Config, error) { |
||
| 17 | c := &scan.Config{} |
||
| 18 | |||
| 19 | var err error |
||
| 20 | |||
| 21 | c.DictionaryPath = cmd.Flag(flagScanDictionary).Value.String() |
||
| 22 | |||
| 23 | if c.DictionaryTimeoutInMilliseconds, err = cmd.Flags().GetInt(flagScanDictionaryGetTimeout); err != nil { |
||
| 24 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanDictionaryGetTimeout) |
||
| 25 | } |
||
| 26 | |||
| 27 | if c.HTTPMethods, err = cmd.Flags().GetStringSlice(flagScanHTTPMethods); err != nil { |
||
| 28 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanHTTPMethods) |
||
| 29 | } |
||
| 30 | |||
| 31 | if c.HTTPStatusesToIgnore, err = cmd.Flags().GetIntSlice(flagScanHTTPStatusesToIgnore); err != nil { |
||
| 32 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanHTTPStatusesToIgnore) |
||
| 33 | } |
||
| 34 | |||
| 35 | if c.Threads, err = cmd.Flags().GetInt(flagScanThreads); err != nil { |
||
| 36 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanThreads) |
||
| 37 | } |
||
| 38 | |||
| 39 | if c.TimeoutInMilliseconds, err = cmd.Flags().GetInt(flagScanHTTPTimeout); err != nil { |
||
| 40 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanHTTPTimeout) |
||
| 41 | } |
||
| 42 | |||
| 43 | if c.CacheRequests, err = cmd.Flags().GetBool(flagScanHTTPCacheRequests); err != nil { |
||
| 44 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanHTTPCacheRequests) |
||
| 45 | } |
||
| 46 | |||
| 47 | if c.ScanDepth, err = cmd.Flags().GetInt(flagScanScanDepth); err != nil { |
||
| 48 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanScanDepth) |
||
| 49 | } |
||
| 50 | |||
| 51 | socks5Host := cmd.Flag(flagScanSocks5Host).Value.String() |
||
| 52 | if len(socks5Host) > 0 { |
||
| 53 | if c.Socks5Url, err = url.Parse("socks5://" + socks5Host); err != nil { |
||
| 54 | return nil, errors.Wrapf(err, "invalid value for %s", flagScanSocks5Host) |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | c.UserAgent = cmd.Flag(flagScanUserAgent).Value.String() |
||
| 59 | |||
| 60 | if c.UseCookieJar, err = cmd.Flags().GetBool(flagScanCookieJar); err != nil { |
||
| 61 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanCookieJar) |
||
| 62 | } |
||
| 63 | |||
| 64 | rawCookies, err := cmd.Flags().GetStringArray(flagScanCookie) |
||
| 65 | if err != nil { |
||
| 66 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanCookie) |
||
| 67 | } |
||
| 68 | |||
| 69 | if c.Cookies, err = rawCookiesToCookies(rawCookies); err != nil { |
||
| 70 | return nil, errors.Wrap(err, "failed to convert rawCookies to objects") |
||
| 71 | } |
||
| 72 | |||
| 73 | rawHeaders, err := cmd.Flags().GetStringArray(flagScanHeader) |
||
| 74 | if err != nil { |
||
| 75 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagScanHeader) |
||
| 76 | } |
||
| 77 | |||
| 78 | if c.Headers, err = rawHeadersToHeaders(rawHeaders); err != nil { |
||
| 79 | return nil, errors.Wrapf(err, "failed to convert rawHeaders (%v)", rawHeaders) |
||
| 80 | } |
||
| 81 | |||
| 82 | c.Out = cmd.Flag(flagScanResultOutput).Value.String() |
||
| 83 | |||
| 84 | c.ShouldSkipSSLCertificatesValidation, err = cmd.Flags().GetBool(flagShouldSkipSSLCertificatesValidation) |
||
| 85 | if err != nil { |
||
| 86 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagShouldSkipSSLCertificatesValidation) |
||
| 87 | } |
||
| 88 | |||
| 89 | c.IgnoreEmpty20xResponses, err = cmd.Flags().GetBool(flagIgnore20xWithEmptyBody) |
||
| 90 | if err != nil { |
||
| 91 | return nil, errors.Wrapf(err, failedToReadPropertyError, flagIgnore20xWithEmptyBody) |
||
| 92 | } |
||
| 93 | |||
| 94 | return c, nil |
||
| 95 | } |
||
| 133 |