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