Conditions | 11 |
Total Lines | 71 |
Code Lines | 64 |
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 |
||
126 | func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *configuration, profileName string) (credentialsProvider CredentialsProvider, err error) { |
||
127 | p, err := conf.getProfile(profileName) |
||
128 | if err != nil { |
||
129 | return |
||
130 | } |
||
131 | |||
132 | switch p.Mode { |
||
133 | case "AK": |
||
134 | credentialsProvider, err = NewStaticAKCredentialsProviderBuilder(). |
||
135 | WithAccessKeyId(p.AccessKeyID). |
||
136 | WithAccessKeySecret(p.AccessKeySecret). |
||
137 | Build() |
||
138 | case "StsToken": |
||
139 | credentialsProvider, err = NewStaticSTSCredentialsProviderBuilder(). |
||
140 | WithAccessKeyId(p.AccessKeyID). |
||
141 | WithAccessKeySecret(p.AccessKeySecret). |
||
142 | WithSecurityToken(p.SecurityToken). |
||
143 | Build() |
||
144 | case "RamRoleArn": |
||
145 | previousProvider, err1 := NewStaticAKCredentialsProviderBuilder(). |
||
146 | WithAccessKeyId(p.AccessKeyID). |
||
147 | WithAccessKeySecret(p.AccessKeySecret). |
||
148 | Build() |
||
149 | if err1 != nil { |
||
150 | return nil, err1 |
||
151 | } |
||
152 | |||
153 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
154 | WithCredentialsProvider(previousProvider). |
||
155 | WithRoleArn(p.RoleArn). |
||
156 | WithRoleSessionName(p.RoleSessionName). |
||
157 | WithDurationSeconds(p.DurationSeconds). |
||
158 | WithStsRegionId(p.StsRegion). |
||
159 | WithEnableVpc(p.EnableVpc). |
||
160 | WithPolicy(p.Policy). |
||
161 | WithExternalId(p.ExternalId). |
||
162 | Build() |
||
163 | case "EcsRamRole": |
||
164 | credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(p.RoleName).Build() |
||
165 | case "OIDC": |
||
166 | credentialsProvider, err = NewOIDCCredentialsProviderBuilder(). |
||
167 | WithOIDCTokenFilePath(p.OIDCTokenFile). |
||
168 | WithOIDCProviderARN(p.OIDCProviderARN). |
||
169 | WithRoleArn(p.RoleArn). |
||
170 | WithStsRegionId(p.StsRegion). |
||
171 | WithEnableVpc(p.EnableVpc). |
||
172 | WithDurationSeconds(p.DurationSeconds). |
||
173 | WithRoleSessionName(p.RoleSessionName). |
||
174 | WithPolicy(p.Policy). |
||
175 | Build() |
||
176 | case "ChainableRamRoleArn": |
||
177 | previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile) |
||
178 | if err1 != nil { |
||
179 | err = fmt.Errorf("get source profile failed: %s", err1.Error()) |
||
180 | return |
||
181 | } |
||
182 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
183 | WithCredentialsProvider(previousProvider). |
||
184 | WithRoleArn(p.RoleArn). |
||
185 | WithRoleSessionName(p.RoleSessionName). |
||
186 | WithDurationSeconds(p.DurationSeconds). |
||
187 | WithStsRegionId(p.StsRegion). |
||
188 | WithEnableVpc(p.EnableVpc). |
||
189 | WithPolicy(p.Policy). |
||
190 | WithExternalId(p.ExternalId). |
||
191 | Build() |
||
192 | default: |
||
193 | err = fmt.Errorf("unsupported profile mode '%s'", p.Mode) |
||
194 | } |
||
195 | |||
196 | return |
||
197 | } |
||
254 |