Conditions | 64 |
Total Lines | 258 |
Code Lines | 175 |
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 config.TOMLLoader.Load 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 |
||
35 | func (c TOMLLoader) Load(pathToToml, keyPass string) error { |
||
36 | var conf Config |
||
37 | if _, err := toml.DecodeFile(pathToToml, &conf); err != nil { |
||
38 | return err |
||
39 | } |
||
40 | Conf.EMail = conf.EMail |
||
41 | Conf.Slack = conf.Slack |
||
42 | Conf.Stride = conf.Stride |
||
43 | Conf.HipChat = conf.HipChat |
||
44 | Conf.ChatWork = conf.ChatWork |
||
45 | Conf.Telegram = conf.Telegram |
||
46 | Conf.Saas = conf.Saas |
||
47 | Conf.Syslog = conf.Syslog |
||
48 | Conf.HTTP = conf.HTTP |
||
49 | Conf.AWS = conf.AWS |
||
50 | Conf.Azure = conf.Azure |
||
51 | |||
52 | Conf.CveDict = conf.CveDict |
||
53 | Conf.OvalDict = conf.OvalDict |
||
54 | Conf.Gost = conf.Gost |
||
55 | Conf.Exploit = conf.Exploit |
||
56 | |||
57 | d := conf.Default |
||
58 | Conf.Default = d |
||
59 | servers := make(map[string]ServerInfo) |
||
60 | |||
61 | if keyPass != "" { |
||
62 | d.KeyPassword = keyPass |
||
63 | } |
||
64 | |||
65 | i := 0 |
||
66 | for serverName, v := range conf.Servers { |
||
67 | if 0 < len(v.KeyPassword) { |
||
68 | return xerrors.Errorf("[Deprecated] KEYPASSWORD IN CONFIG FILE ARE UNSECURE. REMOVE THEM IMMEDIATELY FOR A SECURITY REASONS. THEY WILL BE REMOVED IN A FUTURE RELEASE: %s", serverName) |
||
69 | } |
||
70 | |||
71 | s := ServerInfo{ServerName: serverName} |
||
72 | |||
73 | if v.Type == ServerTypeStaticContainer { |
||
74 | if err := IsValidStaticContainerConf(v.StaticContainer); err != nil { |
||
75 | return err |
||
76 | } |
||
77 | s.StaticContainer = v.StaticContainer |
||
78 | } |
||
79 | |||
80 | if v.Type != ServerTypePseudo && v.Type != ServerTypeStaticContainer { |
||
81 | s.Host = v.Host |
||
82 | if len(s.Host) == 0 { |
||
83 | return xerrors.Errorf("%s is invalid. host is empty", serverName) |
||
84 | } |
||
85 | |||
86 | switch { |
||
87 | case v.Port != "": |
||
88 | s.Port = v.Port |
||
89 | case d.Port != "": |
||
90 | s.Port = d.Port |
||
91 | default: |
||
92 | s.Port = "22" |
||
93 | } |
||
94 | |||
95 | switch { |
||
96 | case v.User != "": |
||
97 | s.User = v.User |
||
98 | case d.User != "": |
||
99 | s.User = d.User |
||
100 | default: |
||
101 | if s.Port != "local" { |
||
102 | return xerrors.Errorf("%s is invalid. User is empty", serverName) |
||
103 | } |
||
104 | } |
||
105 | |||
106 | s.KeyPath = v.KeyPath |
||
107 | if len(s.KeyPath) == 0 { |
||
108 | s.KeyPath = d.KeyPath |
||
109 | } |
||
110 | if s.KeyPath != "" { |
||
111 | if _, err := os.Stat(s.KeyPath); err != nil { |
||
112 | return xerrors.Errorf( |
||
113 | "%s is invalid. keypath: %s not exists", serverName, s.KeyPath) |
||
114 | } |
||
115 | } |
||
116 | |||
117 | s.KeyPassword = v.KeyPassword |
||
118 | if len(s.KeyPassword) == 0 { |
||
119 | s.KeyPassword = d.KeyPassword |
||
120 | } |
||
121 | } |
||
122 | |||
123 | s.ScanMode = v.ScanMode |
||
124 | if len(s.ScanMode) == 0 { |
||
125 | s.ScanMode = d.ScanMode |
||
126 | if len(s.ScanMode) == 0 { |
||
127 | s.ScanMode = []string{"fast"} |
||
128 | } |
||
129 | } |
||
130 | for _, m := range s.ScanMode { |
||
131 | switch m { |
||
132 | case "fast": |
||
133 | s.Mode.Set(Fast) |
||
134 | case "fast-root": |
||
135 | s.Mode.Set(FastRoot) |
||
136 | case "deep": |
||
137 | s.Mode.Set(Deep) |
||
138 | case "offline": |
||
139 | s.Mode.Set(Offline) |
||
140 | default: |
||
141 | return xerrors.Errorf("scanMode: %s of %s is invalie. Specify -fast, -fast-root, -deep or offline", m, serverName) |
||
142 | } |
||
143 | } |
||
144 | if err := s.Mode.validate(); err != nil { |
||
145 | return xerrors.Errorf("%s in %s", err, serverName) |
||
146 | } |
||
147 | |||
148 | s.CpeNames = v.CpeNames |
||
149 | if len(s.CpeNames) == 0 { |
||
150 | s.CpeNames = d.CpeNames |
||
151 | } |
||
152 | |||
153 | for i, n := range s.CpeNames { |
||
154 | uri, err := toCpeURI(n) |
||
155 | if err != nil { |
||
156 | return xerrors.Errorf("Failed to parse CPENames %s in %s, err: %w", n, serverName, err) |
||
|
|||
157 | } |
||
158 | s.CpeNames[i] = uri |
||
159 | } |
||
160 | |||
161 | s.ContainersIncluded = v.ContainersIncluded |
||
162 | if len(s.ContainersIncluded) == 0 { |
||
163 | s.ContainersIncluded = d.ContainersIncluded |
||
164 | } |
||
165 | |||
166 | s.ContainersExcluded = v.ContainersExcluded |
||
167 | if len(s.ContainersExcluded) == 0 { |
||
168 | s.ContainersExcluded = d.ContainersExcluded |
||
169 | } |
||
170 | |||
171 | s.ContainerType = v.ContainerType |
||
172 | if len(s.ContainerType) == 0 { |
||
173 | s.ContainerType = d.ContainerType |
||
174 | } |
||
175 | |||
176 | s.Containers = v.Containers |
||
177 | for contName, cont := range s.Containers { |
||
178 | cont.IgnoreCves = append(cont.IgnoreCves, d.IgnoreCves...) |
||
179 | s.Containers[contName] = cont |
||
180 | } |
||
181 | |||
182 | if len(v.DependencyCheckXMLPath) != 0 || len(d.DependencyCheckXMLPath) != 0 { |
||
183 | return xerrors.Errorf("[DEPRECATED] dependencyCheckXMLPath IS DEPRECATED. USE owaspDCXMLPath INSTEAD: %s", serverName) |
||
184 | } |
||
185 | |||
186 | s.OwaspDCXMLPath = v.OwaspDCXMLPath |
||
187 | if len(s.OwaspDCXMLPath) == 0 { |
||
188 | s.OwaspDCXMLPath = d.OwaspDCXMLPath |
||
189 | } |
||
190 | |||
191 | s.Memo = v.Memo |
||
192 | if s.Memo == "" { |
||
193 | s.Memo = d.Memo |
||
194 | } |
||
195 | |||
196 | s.IgnoreCves = v.IgnoreCves |
||
197 | for _, cve := range d.IgnoreCves { |
||
198 | found := false |
||
199 | for _, c := range s.IgnoreCves { |
||
200 | if cve == c { |
||
201 | found = true |
||
202 | break |
||
203 | } |
||
204 | } |
||
205 | if !found { |
||
206 | s.IgnoreCves = append(s.IgnoreCves, cve) |
||
207 | } |
||
208 | } |
||
209 | |||
210 | s.IgnorePkgsRegexp = v.IgnorePkgsRegexp |
||
211 | for _, pkg := range d.IgnorePkgsRegexp { |
||
212 | found := false |
||
213 | for _, p := range s.IgnorePkgsRegexp { |
||
214 | if pkg == p { |
||
215 | found = true |
||
216 | break |
||
217 | } |
||
218 | } |
||
219 | if !found { |
||
220 | s.IgnorePkgsRegexp = append(s.IgnorePkgsRegexp, pkg) |
||
221 | } |
||
222 | } |
||
223 | for _, reg := range s.IgnorePkgsRegexp { |
||
224 | _, err := regexp.Compile(reg) |
||
225 | if err != nil { |
||
226 | return xerrors.Errorf("Faild to parse %s in %s. err: %w", reg, serverName, err) |
||
227 | } |
||
228 | } |
||
229 | for contName, cont := range s.Containers { |
||
230 | for _, reg := range cont.IgnorePkgsRegexp { |
||
231 | _, err := regexp.Compile(reg) |
||
232 | if err != nil { |
||
233 | return xerrors.Errorf("Faild to parse %s in %s@%s. err: %w", |
||
234 | reg, contName, serverName, err) |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | opt := map[string]interface{}{} |
||
240 | for k, v := range d.Optional { |
||
241 | opt[k] = v |
||
242 | } |
||
243 | for k, v := range v.Optional { |
||
244 | opt[k] = v |
||
245 | } |
||
246 | s.Optional = opt |
||
247 | |||
248 | s.Enablerepo = v.Enablerepo |
||
249 | if len(s.Enablerepo) == 0 { |
||
250 | s.Enablerepo = d.Enablerepo |
||
251 | } |
||
252 | if len(s.Enablerepo) != 0 { |
||
253 | for _, repo := range s.Enablerepo { |
||
254 | switch repo { |
||
255 | case "base", "updates": |
||
256 | // nop |
||
257 | default: |
||
258 | return xerrors.Errorf( |
||
259 | "For now, enablerepo have to be base or updates: %s, servername: %s", |
||
260 | s.Enablerepo, serverName) |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | s.GitHubRepos = v.GitHubRepos |
||
266 | for ownerRepo, githubSetting := range s.GitHubRepos { |
||
267 | if ss := strings.Split(ownerRepo, "/"); len(ss) != 2 { |
||
268 | return xerrors.Errorf("Failed to parse GitHub owner/repo: %s in %s", |
||
269 | ownerRepo, serverName) |
||
270 | } |
||
271 | if githubSetting.Token == "" { |
||
272 | return xerrors.Errorf("GitHub owner/repo: %s in %s token is empty", |
||
273 | ownerRepo, serverName) |
||
274 | } |
||
275 | } |
||
276 | |||
277 | s.UUIDs = v.UUIDs |
||
278 | s.Type = v.Type |
||
279 | |||
280 | s.WordPress.WPVulnDBToken = v.WordPress.WPVulnDBToken |
||
281 | s.WordPress.CmdPath = v.WordPress.CmdPath |
||
282 | s.WordPress.DocRoot = v.WordPress.DocRoot |
||
283 | s.WordPress.OSUser = v.WordPress.OSUser |
||
284 | s.WordPress.IgnoreInactive = v.WordPress.IgnoreInactive |
||
285 | |||
286 | s.LogMsgAnsiColor = Colors[i%len(Colors)] |
||
287 | i++ |
||
288 | |||
289 | servers[serverName] = s |
||
290 | } |
||
291 | Conf.Servers = servers |
||
292 | return nil |
||
293 | } |
||
322 |