Conditions | 18 |
Total Lines | 79 |
Code Lines | 65 |
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 credentials.NewCredential 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 credentials |
||
152 | func NewCredential(config *Config) (credential Credential, err error) { |
||
153 | if config == nil { |
||
154 | config, err = defaultChain.resolve() |
||
155 | if err != nil { |
||
156 | return |
||
157 | } |
||
158 | return NewCredential(config) |
||
159 | } |
||
160 | switch tea.StringValue(config.Type) { |
||
161 | case "access_key": |
||
162 | err = checkAccessKey(config) |
||
163 | if err != nil { |
||
164 | return |
||
165 | } |
||
166 | credential = newAccessKeyCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret)) |
||
167 | case "sts": |
||
168 | err = checkSTS(config) |
||
169 | if err != nil { |
||
170 | return |
||
171 | } |
||
172 | credential = newStsTokenCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.SecurityToken)) |
||
173 | case "ecs_ram_role": |
||
174 | checkEcsRAMRole(config) |
||
175 | runtime := &utils.Runtime{ |
||
176 | Host: tea.StringValue(config.Host), |
||
177 | Proxy: tea.StringValue(config.Proxy), |
||
178 | ReadTimeout: tea.IntValue(config.Timeout), |
||
179 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
180 | } |
||
181 | credential = newEcsRAMRoleCredential(tea.StringValue(config.RoleName), runtime) |
||
182 | case "ram_role_arn": |
||
183 | err = checkRAMRoleArn(config) |
||
184 | if err != nil { |
||
185 | return |
||
186 | } |
||
187 | runtime := &utils.Runtime{ |
||
188 | Host: tea.StringValue(config.Host), |
||
189 | Proxy: tea.StringValue(config.Proxy), |
||
190 | ReadTimeout: tea.IntValue(config.Timeout), |
||
191 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
192 | } |
||
193 | credential = newRAMRoleArnCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.RoleArn), tea.StringValue(config.RoleSessionName), tea.StringValue(config.Policy), tea.IntValue(config.RoleSessionExpiration), runtime) |
||
194 | case "rsa_key_pair": |
||
195 | err = checkRSAKeyPair(config) |
||
196 | if err != nil { |
||
197 | return |
||
198 | } |
||
199 | file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile)) |
||
200 | if err1 != nil { |
||
201 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
202 | return |
||
203 | } |
||
204 | defer file.Close() |
||
205 | var privateKey string |
||
206 | scan := bufio.NewScanner(file) |
||
207 | for scan.Scan() { |
||
208 | if strings.HasPrefix(scan.Text(), "----") { |
||
209 | continue |
||
210 | } |
||
211 | privateKey += scan.Text() + "\n" |
||
212 | } |
||
213 | runtime := &utils.Runtime{ |
||
214 | Host: tea.StringValue(config.Host), |
||
215 | Proxy: tea.StringValue(config.Proxy), |
||
216 | ReadTimeout: tea.IntValue(config.Timeout), |
||
217 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
218 | } |
||
219 | credential = newRsaKeyPairCredential(privateKey, tea.StringValue(config.PublicKeyId), tea.IntValue(config.SessionExpiration), runtime) |
||
220 | case "bearer": |
||
221 | if tea.StringValue(config.BearerToken) == "" { |
||
222 | err = errors.New("BearerToken cannot be empty") |
||
223 | return |
||
224 | } |
||
225 | credential = newBearerTokenCredential(tea.StringValue(config.BearerToken)) |
||
226 | default: |
||
227 | err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
||
228 | return |
||
229 | } |
||
230 | return credential, nil |
||
231 | } |
||
350 |