| Conditions | 21 |
| Total Lines | 96 |
| Code Lines | 80 |
| 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 |
||
| 180 | func NewCredential(config *Config) (credential Credential, err error) { |
||
| 181 | if config == nil { |
||
| 182 | config, err = defaultChain.resolve() |
||
| 183 | if err != nil { |
||
| 184 | return |
||
| 185 | } |
||
| 186 | return NewCredential(config) |
||
| 187 | } |
||
| 188 | switch tea.StringValue(config.Type) { |
||
| 189 | case "credentials_uri": |
||
| 190 | credential = newURLCredential(tea.StringValue(config.Url)) |
||
| 191 | case "oidc_role_arn": |
||
| 192 | err = checkoutAssumeRamoidc(config) |
||
| 193 | if err != nil { |
||
| 194 | return |
||
| 195 | } |
||
| 196 | runtime := &utils.Runtime{ |
||
| 197 | Host: tea.StringValue(config.Host), |
||
| 198 | Proxy: tea.StringValue(config.Proxy), |
||
| 199 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 200 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 201 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
| 202 | } |
||
| 203 | credential = newOIDCRoleArnCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.RoleArn), tea.StringValue(config.OIDCProviderArn), tea.StringValue(config.OIDCTokenFilePath), tea.StringValue(config.RoleSessionName), tea.StringValue(config.Policy), tea.IntValue(config.RoleSessionExpiration), runtime) |
||
| 204 | case "access_key": |
||
| 205 | err = checkAccessKey(config) |
||
| 206 | if err != nil { |
||
| 207 | return |
||
| 208 | } |
||
| 209 | credential = newAccessKeyCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret)) |
||
| 210 | case "sts": |
||
| 211 | err = checkSTS(config) |
||
| 212 | if err != nil { |
||
| 213 | return |
||
| 214 | } |
||
| 215 | credential = newStsTokenCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.SecurityToken)) |
||
| 216 | case "ecs_ram_role": |
||
| 217 | checkEcsRAMRole(config) |
||
| 218 | runtime := &utils.Runtime{ |
||
| 219 | Host: tea.StringValue(config.Host), |
||
| 220 | Proxy: tea.StringValue(config.Proxy), |
||
| 221 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 222 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 223 | } |
||
| 224 | credential = newEcsRAMRoleCredential(tea.StringValue(config.RoleName), tea.Float64Value(config.InAdvanceScale), runtime) |
||
| 225 | case "ram_role_arn": |
||
| 226 | err = checkRAMRoleArn(config) |
||
| 227 | if err != nil { |
||
| 228 | return |
||
| 229 | } |
||
| 230 | runtime := &utils.Runtime{ |
||
| 231 | Host: tea.StringValue(config.Host), |
||
| 232 | Proxy: tea.StringValue(config.Proxy), |
||
| 233 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 234 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 235 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
| 236 | } |
||
| 237 | 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) |
||
| 238 | case "rsa_key_pair": |
||
| 239 | err = checkRSAKeyPair(config) |
||
| 240 | if err != nil { |
||
| 241 | return |
||
| 242 | } |
||
| 243 | file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile)) |
||
| 244 | if err1 != nil { |
||
| 245 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
| 246 | return |
||
| 247 | } |
||
| 248 | defer file.Close() |
||
| 249 | var privateKey string |
||
| 250 | scan := bufio.NewScanner(file) |
||
| 251 | for scan.Scan() { |
||
| 252 | if strings.HasPrefix(scan.Text(), "----") { |
||
| 253 | continue |
||
| 254 | } |
||
| 255 | privateKey += scan.Text() + "\n" |
||
| 256 | } |
||
| 257 | runtime := &utils.Runtime{ |
||
| 258 | Host: tea.StringValue(config.Host), |
||
| 259 | Proxy: tea.StringValue(config.Proxy), |
||
| 260 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 261 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 262 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
| 263 | } |
||
| 264 | credential = newRsaKeyPairCredential(privateKey, tea.StringValue(config.PublicKeyId), tea.IntValue(config.SessionExpiration), runtime) |
||
| 265 | case "bearer": |
||
| 266 | if tea.StringValue(config.BearerToken) == "" { |
||
| 267 | err = errors.New("BearerToken cannot be empty") |
||
| 268 | return |
||
| 269 | } |
||
| 270 | credential = newBearerTokenCredential(tea.StringValue(config.BearerToken)) |
||
| 271 | default: |
||
| 272 | err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
||
| 273 | return |
||
| 274 | } |
||
| 275 | return credential, nil |
||
| 276 | } |
||
| 411 |