| Conditions | 26 |
| Total Lines | 110 |
| Code Lines | 88 |
| 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 credentials.NewCredential 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 credentials |
||
| 58 | func NewCredential(config *Configuration) (credential Credential, err error) { |
||
| 59 | if config == nil { |
||
| 60 | config, err = defaultChain.resolve() |
||
| 61 | if err != nil { |
||
| 62 | return |
||
| 63 | } |
||
| 64 | return NewCredential(config) |
||
| 65 | } |
||
| 66 | switch config.Type { |
||
| 67 | case "access_key": |
||
| 68 | if config.AccessKeyID == "" { |
||
| 69 | err = errors.New("AccessKeyID cannot be empty") |
||
| 70 | return |
||
| 71 | } |
||
| 72 | if config.AccessKeySecret == "" { |
||
| 73 | err = errors.New("AccessKeySecret cannot be empty") |
||
| 74 | return |
||
| 75 | } |
||
| 76 | credential = newAccessKeyCredential(config.AccessKeyID, config.AccessKeySecret) |
||
| 77 | case "sts": |
||
| 78 | if config.AccessKeyID == "" { |
||
| 79 | err = errors.New("AccessKeyID cannot be empty") |
||
| 80 | return |
||
| 81 | } |
||
| 82 | if config.AccessKeySecret == "" { |
||
| 83 | err = errors.New("AccessKeySecret cannot be empty") |
||
| 84 | return |
||
| 85 | } |
||
| 86 | if config.SecurityToken == "" { |
||
| 87 | err = errors.New("SecurityToken cannot be empty") |
||
| 88 | return |
||
| 89 | } |
||
| 90 | credential = newStsTokenCredential(config.AccessKeyID, config.AccessKeySecret, config.SecurityToken) |
||
| 91 | case "ecs_ram_role": |
||
| 92 | if config.RoleName == "" { |
||
| 93 | err = errors.New("RoleName cannot be empty") |
||
| 94 | return |
||
| 95 | } |
||
| 96 | runtime := &utils.Runtime{ |
||
| 97 | Host: config.Host, |
||
| 98 | Proxy: config.Proxy, |
||
| 99 | ReadTimeout: config.Timeout, |
||
| 100 | ConnectTimeout: config.ConnectTimeout, |
||
| 101 | } |
||
| 102 | credential = newEcsRAMRoleCredential(config.RoleName, runtime) |
||
| 103 | case "ram_role_arn": |
||
| 104 | if config.AccessKeySecret == "" { |
||
| 105 | err = errors.New("AccessKeySecret cannot be empty") |
||
| 106 | return |
||
| 107 | } |
||
| 108 | if config.RoleArn == "" { |
||
| 109 | err = errors.New("RoleArn cannot be empty") |
||
| 110 | return |
||
| 111 | } |
||
| 112 | if config.RoleSessionName == "" { |
||
| 113 | err = errors.New("RoleSessionName cannot be empty") |
||
| 114 | return |
||
| 115 | } |
||
| 116 | if config.AccessKeyID == "" { |
||
| 117 | err = errors.New("AccessKeyID cannot be empty") |
||
| 118 | return |
||
| 119 | } |
||
| 120 | runtime := &utils.Runtime{ |
||
| 121 | Host: config.Host, |
||
| 122 | Proxy: config.Proxy, |
||
| 123 | ReadTimeout: config.Timeout, |
||
| 124 | ConnectTimeout: config.ConnectTimeout, |
||
| 125 | } |
||
| 126 | credential = newRAMRoleArnCredential(config.AccessKeyID, config.AccessKeySecret, config.RoleArn, config.RoleSessionName, config.Policy, config.RoleSessionExpiration, runtime) |
||
| 127 | case "rsa_key_pair": |
||
| 128 | if config.PrivateKeyFile == "" { |
||
| 129 | err = errors.New("PrivateKeyFile cannot be empty") |
||
| 130 | return |
||
| 131 | } |
||
| 132 | if config.PublicKeyID == "" { |
||
| 133 | err = errors.New("PublicKeyID cannot be empty") |
||
| 134 | return |
||
| 135 | } |
||
| 136 | file, err1 := os.Open(config.PrivateKeyFile) |
||
| 137 | if err1 != nil { |
||
| 138 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
| 139 | return |
||
| 140 | } |
||
| 141 | defer file.Close() |
||
| 142 | var privateKey string |
||
| 143 | scan := bufio.NewScanner(file) |
||
| 144 | for scan.Scan() { |
||
| 145 | if strings.HasPrefix(scan.Text(), "----") { |
||
| 146 | continue |
||
| 147 | } |
||
| 148 | privateKey += scan.Text() + "\n" |
||
| 149 | } |
||
| 150 | runtime := &utils.Runtime{ |
||
| 151 | Host: config.Host, |
||
| 152 | Proxy: config.Proxy, |
||
| 153 | ReadTimeout: config.Timeout, |
||
| 154 | ConnectTimeout: config.ConnectTimeout, |
||
| 155 | } |
||
| 156 | credential = newRsaKeyPairCredential(privateKey, config.PublicKeyID, config.SessionExpiration, runtime) |
||
| 157 | case "bearer": |
||
| 158 | if config.BearerToken == "" { |
||
| 159 | err = errors.New("BearerToken cannot be empty") |
||
| 160 | return |
||
| 161 | } |
||
| 162 | credential = newBearerTokenCredential(config.BearerToken) |
||
| 163 | default: |
||
| 164 | err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
||
| 165 | return |
||
| 166 | } |
||
| 167 | return credential, nil |
||
| 168 | } |
||
| 223 |