| Conditions | 22 | 
| Total Lines | 76 | 
| Code Lines | 61 | 
| 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.*ProfileCredentialsProvider.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 | ||
| 44 | func (provider *ProfileCredentialsProvider) getCredentialsProvider(ini *ini.File) (credentialsProvider CredentialsProvider, err error) { | ||
| 45 | section, err := ini.GetSection(provider.profileName) | ||
| 46 | 	if err != nil { | ||
| 47 | 		err = errors.New("ERROR: Can not load section" + err.Error()) | ||
| 48 | return | ||
| 49 | } | ||
| 50 | |||
| 51 | 	value, err := section.GetKey("type") | ||
| 52 | 	if err != nil { | ||
| 53 | 		err = errors.New("ERROR: Can not find credential type" + err.Error()) | ||
| 54 | return | ||
| 55 | } | ||
| 56 | |||
| 57 | 	switch value.String() { | ||
| 58 | case "access_key": | ||
| 59 | 		value1, err1 := section.GetKey("access_key_id") | ||
| 60 | 		value2, err2 := section.GetKey("access_key_secret") | ||
| 61 | 		if err1 != nil || err2 != nil { | ||
| 62 | 			err = errors.New("ERROR: Failed to get value") | ||
| 63 | return | ||
| 64 | } | ||
| 65 | |||
| 66 | 		if value1.String() == "" || value2.String() == "" { | ||
| 67 | 			err = errors.New("ERROR: Value can't be empty") | ||
| 68 | return | ||
| 69 | } | ||
| 70 | |||
| 71 | credentialsProvider, err = NewStaticAKCredentialsProviderBuilder(). | ||
| 72 | WithAccessKeyId(value1.String()). | ||
| 73 | WithAccessKeySecret(value2.String()). | ||
| 74 | Build() | ||
| 75 | case "ecs_ram_role": | ||
| 76 | 		value1, err1 := section.GetKey("role_name") | ||
| 77 | 		if err1 != nil { | ||
| 78 | 			err = errors.New("ERROR: Failed to get value") | ||
| 79 | return | ||
| 80 | } | ||
| 81 | credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(value1.String()).Build() | ||
| 82 | case "ram_role_arn": | ||
| 83 | 		value1, err1 := section.GetKey("access_key_id") | ||
| 84 | 		value2, err2 := section.GetKey("access_key_secret") | ||
| 85 | 		value3, err3 := section.GetKey("role_arn") | ||
| 86 | 		value4, err4 := section.GetKey("role_session_name") | ||
| 87 | 		if err1 != nil || err2 != nil || err3 != nil || err4 != nil { | ||
| 88 | 			err = errors.New("ERROR: Failed to get value") | ||
| 89 | return | ||
| 90 | } | ||
| 91 | 		if value1.String() == "" || value2.String() == "" || value3.String() == "" || value4.String() == "" { | ||
| 92 | 			err = errors.New("ERROR: Value can't be empty") | ||
| 93 | return | ||
| 94 | } | ||
| 95 | previous, err5 := NewStaticAKCredentialsProviderBuilder(). | ||
| 96 | WithAccessKeyId(value1.String()). | ||
| 97 | WithAccessKeySecret(value2.String()). | ||
| 98 | Build() | ||
| 99 | 		if err5 != nil { | ||
| 100 | 			err = errors.New("get previous credentials provider failed") | ||
| 101 | return | ||
| 102 | } | ||
| 103 | 		rawPolicy, _ := section.GetKey("policy") | ||
| 104 | policy := "" | ||
| 105 | 		if rawPolicy != nil { | ||
| 106 | policy = rawPolicy.String() | ||
| 107 | } | ||
| 108 | |||
| 109 | credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder(). | ||
| 110 | WithCredentialsProvider(previous). | ||
| 111 | WithRoleArn(value3.String()). | ||
| 112 | WithRoleSessionName(value4.String()). | ||
| 113 | WithPolicy(policy). | ||
| 114 | WithDurationSeconds(3600). | ||
| 115 | Build() | ||
| 116 | default: | ||
| 117 | 		err = errors.New("ERROR: Failed to get credential") | ||
| 118 | } | ||
| 119 | return | ||
| 120 | } | ||
| 170 |