Conditions | 16 |
Total Lines | 89 |
Code Lines | 61 |
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.*ServerCmd.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 |
||
150 | func (p *ServerCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { |
||
151 | util.Log = util.NewCustomLogger(c.ServerInfo{}) |
||
152 | cvelog.SetLogger(c.Conf.LogDir, false, c.Conf.Debug, false) |
||
153 | |||
154 | if err := c.Load(p.configPath, ""); err != nil { |
||
155 | util.Log.Errorf("Error loading %s, %s", p.configPath, err) |
||
156 | return subcommands.ExitUsageError |
||
157 | } |
||
158 | |||
159 | c.Conf.CveDict.Overwrite(p.cveDict) |
||
160 | c.Conf.OvalDict.Overwrite(p.ovalDict) |
||
161 | c.Conf.Gost.Overwrite(p.gostConf) |
||
162 | c.Conf.Exploit.Overwrite(p.exploitConf) |
||
163 | |||
164 | util.Log.Info("Validating config...") |
||
165 | if !c.Conf.ValidateOnReport() { |
||
166 | return subcommands.ExitUsageError |
||
167 | } |
||
168 | |||
169 | util.Log.Info("Validating db config...") |
||
170 | if !c.Conf.ValidateOnReportDB() { |
||
171 | return subcommands.ExitUsageError |
||
172 | } |
||
173 | |||
174 | if c.Conf.CveDict.URL != "" { |
||
175 | if err := report.CveClient.CheckHealth(); err != nil { |
||
176 | util.Log.Errorf("CVE HTTP server is not running. err: %s", err) |
||
177 | util.Log.Errorf("Run go-cve-dictionary as server mode before reporting or run with `-cvedb-type=sqlite3 -cvedb-sqlite3-path` option instead of -cvedb-url") |
||
178 | return subcommands.ExitFailure |
||
179 | } |
||
180 | } |
||
181 | |||
182 | if c.Conf.OvalDict.URL != "" { |
||
183 | err := oval.Base{}.CheckHTTPHealth() |
||
184 | if err != nil { |
||
185 | util.Log.Errorf("OVAL HTTP server is not running. err: %s", err) |
||
186 | util.Log.Errorf("Run goval-dictionary as server mode before reporting or run with `-ovaldb-type=sqlite3 -ovaldb-sqlite3-path` option instead of -ovaldb-url") |
||
187 | return subcommands.ExitFailure |
||
188 | } |
||
189 | } |
||
190 | |||
191 | if c.Conf.Gost.URL != "" { |
||
192 | util.Log.Infof("gost: %s", c.Conf.Gost.URL) |
||
193 | err := gost.Base{}.CheckHTTPHealth() |
||
194 | if err != nil { |
||
195 | util.Log.Errorf("gost HTTP server is not running. err: %s", err) |
||
196 | util.Log.Errorf("Run gost as server mode before reporting or run with `-gostdb-type=sqlite3 -gostdb-sqlite3-path` option instead of -gostdb-url") |
||
197 | return subcommands.ExitFailure |
||
198 | } |
||
199 | } |
||
200 | |||
201 | if c.Conf.Exploit.URL != "" { |
||
202 | err := exploit.CheckHTTPHealth() |
||
203 | if err != nil { |
||
204 | util.Log.Errorf("exploit HTTP server is not running. err: %s", err) |
||
205 | util.Log.Errorf("Run go-exploitdb as server mode before reporting") |
||
206 | return subcommands.ExitFailure |
||
207 | } |
||
208 | } |
||
209 | |||
210 | dbclient, locked, err := report.NewDBClient(report.DBClientConf{ |
||
211 | CveDictCnf: c.Conf.CveDict, |
||
212 | OvalDictCnf: c.Conf.OvalDict, |
||
213 | GostCnf: c.Conf.Gost, |
||
214 | ExploitCnf: c.Conf.Exploit, |
||
215 | DebugSQL: c.Conf.DebugSQL, |
||
216 | }) |
||
217 | if locked { |
||
218 | util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %s", err) |
||
219 | return subcommands.ExitFailure |
||
220 | } |
||
221 | |||
222 | if err != nil { |
||
223 | util.Log.Errorf("Failed to init DB Clients: %s", err) |
||
224 | return subcommands.ExitFailure |
||
225 | } |
||
226 | |||
227 | defer dbclient.CloseDB() |
||
228 | |||
229 | http.Handle("/vuls", server.VulsHandler{DBclient: *dbclient}) |
||
230 | http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
||
231 | fmt.Fprintf(w, "ok") |
||
232 | }) |
||
233 | util.Log.Infof("Listening on %s", p.listen) |
||
234 | if err := http.ListenAndServe(p.listen, nil); err != nil { |
||
235 | util.Log.Errorf("Failed to start server: %s", err) |
||
236 | return subcommands.ExitFailure |
||
237 | } |
||
238 | return subcommands.ExitSuccess |
||
239 | } |
||
240 |