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