| Conditions | 18 |
| Total Lines | 105 |
| Code Lines | 72 |
| 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.*TuiCmd.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 |
||
| 144 | func (p *TuiCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { |
||
| 145 | c.Conf.Lang = "en" |
||
| 146 | |||
| 147 | // Setup Logger |
||
| 148 | util.Log = util.NewCustomLogger(c.ServerInfo{}) |
||
| 149 | cvelog.SetLogger(c.Conf.LogDir, false, c.Conf.Debug, false) |
||
| 150 | |||
| 151 | if err := c.Load(p.configPath, ""); err != nil { |
||
| 152 | util.Log.Errorf("Error loading %s, err: %+v", p.configPath, err) |
||
| 153 | return subcommands.ExitUsageError |
||
| 154 | } |
||
| 155 | |||
| 156 | c.Conf.CveDict.Overwrite(p.cveDict) |
||
| 157 | c.Conf.OvalDict.Overwrite(p.ovalDict) |
||
| 158 | c.Conf.Gost.Overwrite(p.gostConf) |
||
| 159 | c.Conf.Exploit.Overwrite(p.exploitConf) |
||
| 160 | |||
| 161 | var dir string |
||
| 162 | var err error |
||
| 163 | if c.Conf.Diff { |
||
| 164 | dir, err = report.JSONDir([]string{}) |
||
| 165 | } else { |
||
| 166 | dir, err = report.JSONDir(f.Args()) |
||
| 167 | } |
||
| 168 | if err != nil { |
||
| 169 | util.Log.Errorf("Failed to read from JSON. err: %+v", err) |
||
| 170 | return subcommands.ExitFailure |
||
| 171 | } |
||
| 172 | |||
| 173 | util.Log.Info("Validating config...") |
||
| 174 | if !c.Conf.ValidateOnTui() { |
||
| 175 | return subcommands.ExitUsageError |
||
| 176 | } |
||
| 177 | |||
| 178 | var res models.ScanResults |
||
| 179 | if res, err = report.LoadScanResults(dir); err != nil { |
||
| 180 | util.Log.Error(err) |
||
| 181 | return subcommands.ExitFailure |
||
| 182 | } |
||
| 183 | util.Log.Infof("Loaded: %s", dir) |
||
| 184 | |||
| 185 | util.Log.Info("Validating db config...") |
||
| 186 | if !c.Conf.ValidateOnReportDB() { |
||
| 187 | return subcommands.ExitUsageError |
||
| 188 | } |
||
| 189 | |||
| 190 | if c.Conf.CveDict.URL != "" { |
||
| 191 | if err := report.CveClient.CheckHealth(); err != nil { |
||
| 192 | util.Log.Errorf("CVE HTTP server is not running. err: %+v", err) |
||
| 193 | 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") |
||
| 194 | return subcommands.ExitFailure |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if c.Conf.OvalDict.URL != "" { |
||
| 199 | err := oval.Base{}.CheckHTTPHealth() |
||
| 200 | if err != nil { |
||
| 201 | util.Log.Errorf("OVAL HTTP server is not running. err: %+v", err) |
||
| 202 | 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") |
||
| 203 | return subcommands.ExitFailure |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if c.Conf.Gost.URL != "" { |
||
| 208 | util.Log.Infof("gost: %s", c.Conf.Gost.URL) |
||
| 209 | err := gost.Base{}.CheckHTTPHealth() |
||
| 210 | if err != nil { |
||
| 211 | util.Log.Errorf("gost HTTP server is not running. err: %+v", err) |
||
| 212 | util.Log.Errorf("Run gost as server mode before reporting or run with `-gostdb-type=sqlite3 -gostdb-sqlite3-path` option instead of -gostdb-url") |
||
| 213 | return subcommands.ExitFailure |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if c.Conf.Exploit.URL != "" { |
||
| 218 | err := exploit.CheckHTTPHealth() |
||
| 219 | if err != nil { |
||
| 220 | util.Log.Errorf("exploit HTTP server is not running. err: %+v", err) |
||
| 221 | util.Log.Errorf("Run go-exploitdb as server mode before reporting") |
||
| 222 | return subcommands.ExitFailure |
||
| 223 | } |
||
| 224 | } |
||
| 225 | dbclient, locked, err := report.NewDBClient(report.DBClientConf{ |
||
| 226 | CveDictCnf: c.Conf.CveDict, |
||
| 227 | OvalDictCnf: c.Conf.OvalDict, |
||
| 228 | GostCnf: c.Conf.Gost, |
||
| 229 | ExploitCnf: c.Conf.Exploit, |
||
| 230 | DebugSQL: c.Conf.DebugSQL, |
||
| 231 | }) |
||
| 232 | if locked { |
||
| 233 | util.Log.Errorf("SQLite3 is locked. Close other DB connections and try again: %+v", err) |
||
| 234 | return subcommands.ExitFailure |
||
| 235 | } |
||
| 236 | |||
| 237 | if err != nil { |
||
| 238 | util.Log.Errorf("Failed to init DB Clients. err: %+v", err) |
||
| 239 | return subcommands.ExitFailure |
||
| 240 | } |
||
| 241 | |||
| 242 | defer dbclient.CloseDB() |
||
| 243 | |||
| 244 | if res, err = report.FillCveInfos(*dbclient, res, dir); err != nil { |
||
| 245 | util.Log.Error(err) |
||
| 246 | return subcommands.ExitFailure |
||
| 247 | } |
||
| 248 | return report.RunTui(res) |
||
| 249 | } |
||
| 250 |