Conditions | 18 |
Total Lines | 98 |
Code Lines | 68 |
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 commands.*ScanCmd.Execute 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 | /* Vuls - Vulnerability Scanner |
||
124 | func (p *ScanCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { |
||
125 | // Setup Logger |
||
126 | util.Log = util.NewCustomLogger(c.ServerInfo{}) |
||
127 | |||
128 | if err := mkdirDotVuls(); err != nil { |
||
129 | util.Log.Errorf("Failed to create .vuls. err: %+v", err) |
||
130 | return subcommands.ExitUsageError |
||
131 | } |
||
132 | |||
133 | var keyPass string |
||
134 | var err error |
||
135 | if p.askKeyPassword { |
||
136 | prompt := "SSH key password: " |
||
137 | if keyPass, err = getPasswd(prompt); err != nil { |
||
138 | util.Log.Error(err) |
||
139 | return subcommands.ExitFailure |
||
140 | } |
||
141 | } |
||
142 | |||
143 | err = c.Load(p.configPath, keyPass) |
||
144 | if err != nil { |
||
145 | msg := []string{ |
||
146 | fmt.Sprintf("Error loading %s", p.configPath), |
||
147 | "If you update Vuls and get this error, there may be incompatible changes in config.toml", |
||
148 | "Please check config.toml template : https://vuls.io/docs/en/usage-settings.html", |
||
149 | } |
||
150 | util.Log.Errorf("%s\n%+v", strings.Join(msg, "\n"), err) |
||
151 | return subcommands.ExitUsageError |
||
152 | } |
||
153 | |||
154 | util.Log.Info("Start scanning") |
||
155 | util.Log.Infof("config: %s", p.configPath) |
||
156 | |||
157 | var servernames []string |
||
158 | if 0 < len(f.Args()) { |
||
159 | servernames = f.Args() |
||
160 | } else if c.Conf.Pipe { |
||
161 | bytes, err := ioutil.ReadAll(os.Stdin) |
||
162 | if err != nil { |
||
163 | util.Log.Errorf("Failed to read stdin. err: %+v", err) |
||
164 | return subcommands.ExitFailure |
||
165 | } |
||
166 | fields := strings.Fields(string(bytes)) |
||
167 | if 0 < len(fields) { |
||
168 | servernames = fields |
||
169 | } |
||
170 | } |
||
171 | |||
172 | target := make(map[string]c.ServerInfo) |
||
173 | for _, arg := range servernames { |
||
174 | found := false |
||
175 | for servername, info := range c.Conf.Servers { |
||
176 | if servername == arg { |
||
177 | target[servername] = info |
||
178 | found = true |
||
179 | break |
||
180 | } |
||
181 | } |
||
182 | if !found { |
||
183 | util.Log.Errorf("%s is not in config", arg) |
||
184 | return subcommands.ExitUsageError |
||
185 | } |
||
186 | } |
||
187 | if 0 < len(servernames) { |
||
188 | c.Conf.Servers = target |
||
189 | } |
||
190 | util.Log.Debugf("%s", pp.Sprintf("%v", target)) |
||
191 | |||
192 | util.Log.Info("Validating config...") |
||
193 | if !c.Conf.ValidateOnScan() { |
||
194 | return subcommands.ExitUsageError |
||
195 | } |
||
196 | |||
197 | util.Log.Info("Detecting Server/Container OS... ") |
||
198 | if err := scan.InitServers(p.timeoutSec); err != nil { |
||
199 | util.Log.Errorf("Failed to init servers: %+v", err) |
||
200 | return subcommands.ExitFailure |
||
201 | } |
||
202 | |||
203 | util.Log.Info("Checking Scan Modes... ") |
||
204 | if err := scan.CheckScanModes(); err != nil { |
||
205 | util.Log.Errorf("Fix config.toml. err: %+v", err) |
||
206 | return subcommands.ExitFailure |
||
207 | } |
||
208 | |||
209 | util.Log.Info("Detecting Platforms... ") |
||
210 | scan.DetectPlatforms(p.timeoutSec) |
||
211 | |||
212 | util.Log.Info("Scanning vulnerabilities... ") |
||
213 | if err := scan.Scan(p.scanTimeoutSec); err != nil { |
||
214 | util.Log.Errorf("Failed to scan. err: %+v", err) |
||
215 | return subcommands.ExitFailure |
||
216 | } |
||
217 | fmt.Printf("\n\n\n") |
||
218 | fmt.Println("To view the detail, vuls tui is useful.") |
||
219 | fmt.Println("To send a report, run vuls report -h.") |
||
220 | |||
221 | return subcommands.ExitSuccess |
||
222 | } |
||
223 |