Conditions | 19 |
Total Lines | 82 |
Code Lines | 67 |
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 | err = checkAccessKey(config) |
||
69 | if err != nil { |
||
70 | return |
||
71 | } |
||
72 | credential = newAccessKeyCredential(config.AccessKeyID, config.AccessKeySecret) |
||
73 | case "sts": |
||
74 | err = checkSTS(config) |
||
75 | if err != nil { |
||
76 | return |
||
77 | } |
||
78 | credential = newStsTokenCredential(config.AccessKeyID, config.AccessKeySecret, config.SecurityToken) |
||
79 | case "ecs_ram_role": |
||
80 | err = checkEcsRAMRole(config) |
||
81 | if err != nil { |
||
82 | return |
||
83 | } |
||
84 | runtime := &utils.Runtime{ |
||
85 | Host: config.Host, |
||
86 | Proxy: config.Proxy, |
||
87 | ReadTimeout: config.Timeout, |
||
88 | ConnectTimeout: config.ConnectTimeout, |
||
89 | } |
||
90 | credential = newEcsRAMRoleCredential(config.RoleName, runtime) |
||
91 | case "ram_role_arn": |
||
92 | err = checkRAMRoleArn(config) |
||
93 | if err != nil { |
||
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 = newRAMRoleArnCredential(config.AccessKeyID, config.AccessKeySecret, config.RoleArn, config.RoleSessionName, config.Policy, config.RoleSessionExpiration, runtime) |
||
103 | case "rsa_key_pair": |
||
104 | err = checkRSAKeyPair(config) |
||
105 | if err != nil { |
||
106 | return |
||
107 | } |
||
108 | file, err1 := os.Open(config.PrivateKeyFile) |
||
109 | if err1 != nil { |
||
110 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
111 | return |
||
112 | } |
||
113 | defer file.Close() |
||
114 | var privateKey string |
||
115 | scan := bufio.NewScanner(file) |
||
116 | for scan.Scan() { |
||
117 | if strings.HasPrefix(scan.Text(), "----") { |
||
118 | continue |
||
119 | } |
||
120 | privateKey += scan.Text() + "\n" |
||
121 | } |
||
122 | runtime := &utils.Runtime{ |
||
123 | Host: config.Host, |
||
124 | Proxy: config.Proxy, |
||
125 | ReadTimeout: config.Timeout, |
||
126 | ConnectTimeout: config.ConnectTimeout, |
||
127 | } |
||
128 | credential = newRsaKeyPairCredential(privateKey, config.PublicKeyID, config.SessionExpiration, runtime) |
||
129 | case "bearer": |
||
130 | if config.BearerToken == "" { |
||
131 | err = errors.New("BearerToken cannot be empty") |
||
132 | return |
||
133 | } |
||
134 | credential = newBearerTokenCredential(config.BearerToken) |
||
135 | default: |
||
136 | err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
||
137 | return |
||
138 | } |
||
139 | return credential, nil |
||
140 | } |
||
263 |