| Conditions | 10 |
| Total Lines | 57 |
| Code Lines | 50 |
| 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 providers.*CLIProfileCredentialsProvider.getCredentialsProvider 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 providers |
||
| 101 | func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *configuration, profileName string) (credentialsProvider CredentialsProvider, err error) { |
||
| 102 | p, err := conf.getProfile(profileName) |
||
| 103 | if err != nil { |
||
| 104 | return |
||
| 105 | } |
||
| 106 | |||
| 107 | switch p.Mode { |
||
| 108 | case "AK": |
||
| 109 | credentialsProvider, err = NewStaticAKCredentialsProviderBuilder(). |
||
| 110 | WithAccessKeyId(p.AccessKeyID). |
||
| 111 | WithAccessKeySecret(p.AccessKeySecret). |
||
| 112 | Build() |
||
| 113 | case "RamRoleArn": |
||
| 114 | previousProvider, err1 := NewStaticAKCredentialsProviderBuilder(). |
||
| 115 | WithAccessKeyId(p.AccessKeyID). |
||
| 116 | WithAccessKeySecret(p.AccessKeySecret). |
||
| 117 | Build() |
||
| 118 | if err1 != nil { |
||
| 119 | return nil, err1 |
||
| 120 | } |
||
| 121 | |||
| 122 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
| 123 | WithCredentialsProvider(previousProvider). |
||
| 124 | WithRoleArn(p.RoleArn). |
||
| 125 | WithRoleSessionName(p.RoleSessionName). |
||
| 126 | WithDurationSeconds(p.DurationSeconds). |
||
| 127 | WithStsRegionId(p.StsRegion). |
||
| 128 | Build() |
||
| 129 | case "EcsRamRole": |
||
| 130 | credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(p.RoleName).Build() |
||
| 131 | case "OIDC": |
||
| 132 | credentialsProvider, err = NewOIDCCredentialsProviderBuilder(). |
||
| 133 | WithOIDCTokenFilePath(p.OIDCTokenFile). |
||
| 134 | WithOIDCProviderARN(p.OIDCProviderARN). |
||
| 135 | WithRoleArn(p.RoleArn). |
||
| 136 | WithStsRegionId(p.StsRegion). |
||
| 137 | WithDurationSeconds(p.DurationSeconds). |
||
| 138 | WithRoleSessionName(p.RoleSessionName). |
||
| 139 | Build() |
||
| 140 | case "ChainableRamRoleArn": |
||
| 141 | previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile) |
||
| 142 | if err1 != nil { |
||
| 143 | err = fmt.Errorf("get source profile failed: %s", err1.Error()) |
||
| 144 | return |
||
| 145 | } |
||
| 146 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
| 147 | WithCredentialsProvider(previousProvider). |
||
| 148 | WithRoleArn(p.RoleArn). |
||
| 149 | WithRoleSessionName(p.RoleSessionName). |
||
| 150 | WithDurationSeconds(p.DurationSeconds). |
||
| 151 | WithStsRegionId(p.StsRegion). |
||
| 152 | Build() |
||
| 153 | default: |
||
| 154 | err = fmt.Errorf("unsupported profile mode '%s'", p.Mode) |
||
| 155 | } |
||
| 156 | |||
| 157 | return |
||
| 158 | } |
||
| 207 |