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 |
||
112 | func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *configuration, profileName string) (credentialsProvider CredentialsProvider, err error) { |
||
113 | p, err := conf.getProfile(profileName) |
||
114 | if err != nil { |
||
115 | return |
||
116 | } |
||
117 | |||
118 | switch p.Mode { |
||
119 | case "AK": |
||
120 | credentialsProvider, err = NewStaticAKCredentialsProviderBuilder(). |
||
121 | WithAccessKeyId(p.AccessKeyID). |
||
122 | WithAccessKeySecret(p.AccessKeySecret). |
||
123 | Build() |
||
124 | case "RamRoleArn": |
||
125 | previousProvider, err1 := NewStaticAKCredentialsProviderBuilder(). |
||
126 | WithAccessKeyId(p.AccessKeyID). |
||
127 | WithAccessKeySecret(p.AccessKeySecret). |
||
128 | Build() |
||
129 | if err1 != nil { |
||
130 | return nil, err1 |
||
131 | } |
||
132 | |||
133 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
134 | WithCredentialsProvider(previousProvider). |
||
135 | WithRoleArn(p.RoleArn). |
||
136 | WithRoleSessionName(p.RoleSessionName). |
||
137 | WithDurationSeconds(p.DurationSeconds). |
||
138 | WithStsRegionId(p.StsRegion). |
||
139 | WithEnableVpc(p.EnableVpc). |
||
140 | WithPolicy(p.Policy). |
||
141 | WithExternalId(p.ExternalId). |
||
142 | Build() |
||
143 | case "EcsRamRole": |
||
144 | credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(p.RoleName).Build() |
||
145 | case "OIDC": |
||
146 | credentialsProvider, err = NewOIDCCredentialsProviderBuilder(). |
||
147 | WithOIDCTokenFilePath(p.OIDCTokenFile). |
||
148 | WithOIDCProviderARN(p.OIDCProviderARN). |
||
149 | WithRoleArn(p.RoleArn). |
||
150 | WithStsRegionId(p.StsRegion). |
||
151 | WithEnableVpc(p.EnableVpc). |
||
152 | WithDurationSeconds(p.DurationSeconds). |
||
153 | WithRoleSessionName(p.RoleSessionName). |
||
154 | WithPolicy(p.Policy). |
||
155 | Build() |
||
156 | case "ChainableRamRoleArn": |
||
157 | previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile) |
||
158 | if err1 != nil { |
||
159 | err = fmt.Errorf("get source profile failed: %s", err1.Error()) |
||
160 | return |
||
161 | } |
||
162 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). |
||
163 | WithCredentialsProvider(previousProvider). |
||
164 | WithRoleArn(p.RoleArn). |
||
165 | WithRoleSessionName(p.RoleSessionName). |
||
166 | WithDurationSeconds(p.DurationSeconds). |
||
167 | WithStsRegionId(p.StsRegion). |
||
168 | WithEnableVpc(p.EnableVpc). |
||
169 | WithPolicy(p.Policy). |
||
170 | WithExternalId(p.ExternalId). |
||
171 | Build() |
||
172 | default: |
||
173 | err = fmt.Errorf("unsupported profile mode '%s'", p.Mode) |
||
174 | } |
||
175 | |||
176 | return |
||
177 | } |
||
234 |