Conditions | 12 |
Total Lines | 62 |
Code Lines | 43 |
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.*ECSRAMRoleCredentialsProvider.getCredentials 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 |
||
110 | func (provider *ECSRAMRoleCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
111 | roleName := provider.roleName |
||
112 | if roleName == "" { |
||
113 | roleName, err = provider.getRoleName() |
||
114 | if err != nil { |
||
115 | return |
||
116 | } |
||
117 | } |
||
118 | |||
119 | req := &httputil.Request{ |
||
120 | Method: "GET", |
||
121 | Protocol: "http", |
||
122 | Host: "100.100.100.200", |
||
123 | Path: "/latest/meta-data/ram/security-credentials/" + roleName, |
||
124 | ConnectTimeout: 5 * time.Second, |
||
125 | ReadTimeout: 5 * time.Second, |
||
126 | Headers: map[string]string{}, |
||
127 | } |
||
128 | |||
129 | metadataToken, err := provider.getMetadataToken() |
||
130 | if err != nil { |
||
131 | return nil, err |
||
132 | } |
||
133 | if metadataToken != "" { |
||
134 | req.Headers["x-aliyun-ecs-metadata-token"] = metadataToken |
||
135 | } |
||
136 | |||
137 | res, err := httpDo(req) |
||
138 | if err != nil { |
||
139 | err = fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
||
140 | return |
||
141 | } |
||
142 | |||
143 | if res.StatusCode != 200 { |
||
144 | err = fmt.Errorf("refresh Ecs sts token err, httpStatus: %d, message = %s", res.StatusCode, string(res.Body)) |
||
145 | return |
||
146 | } |
||
147 | |||
148 | var data ecsRAMRoleResponse |
||
149 | err = json.Unmarshal(res.Body, &data) |
||
150 | if err != nil { |
||
151 | err = fmt.Errorf("refresh Ecs sts token err, json.Unmarshal fail: %s", err.Error()) |
||
152 | return |
||
153 | } |
||
154 | |||
155 | if data.AccessKeyId == nil || data.AccessKeySecret == nil || data.SecurityToken == nil { |
||
156 | err = fmt.Errorf("refresh Ecs sts token err, fail to get credentials") |
||
157 | return |
||
158 | } |
||
159 | |||
160 | if *data.Code != "Success" { |
||
161 | err = fmt.Errorf("refresh Ecs sts token err, Code is not Success") |
||
162 | return |
||
163 | } |
||
164 | |||
165 | session = &sessionCredentials{ |
||
166 | AccessKeyId: *data.AccessKeyId, |
||
167 | AccessKeySecret: *data.AccessKeySecret, |
||
168 | SecurityToken: *data.SecurityToken, |
||
169 | Expiration: *data.Expiration, |
||
170 | } |
||
171 | return |
||
172 | } |
||
231 |