Make fs.CheckClose public to stop duplication

This commit is contained in:
Nick Craig-Wood 2015-11-28 18:13:08 +00:00
parent 7a24532224
commit ac65d8369e
4 changed files with 6 additions and 25 deletions

View file

@ -305,7 +305,7 @@ func forEachLine(path string, fn func(string) error) (err error) {
if err != nil { if err != nil {
return err return err
} }
defer checkClose(in, &err) defer CheckClose(in, &err)
scanner := bufio.NewScanner(in) scanner := bufio.NewScanner(in)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()

View file

@ -305,9 +305,9 @@ func ErrorLog(o interface{}, text string, args ...interface{}) {
OutputLog(o, text, args...) OutputLog(o, text, args...)
} }
// checkClose is a utility function used to check the return from // CheckClose is a utility function used to check the return from
// Close in a defer statement. // Close in a defer statement.
func checkClose(c io.Closer, err *error) { func CheckClose(c io.Closer, err *error) {
cerr := c.Close() cerr := c.Close()
if *err == nil { if *err == nil {
*err = cerr *err = cerr

View file

@ -9,7 +9,6 @@ package hubic
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"time" "time"
@ -103,15 +102,6 @@ func (f *Fs) String() string {
return fmt.Sprintf("Hubic %s", f.Fs.String()) return fmt.Sprintf("Hubic %s", f.Fs.String())
} }
// checkClose is a utility function used to check the return from
// Close in a defer statement.
func checkClose(c io.Closer, err *error) {
cerr := c.Close()
if *err == nil {
*err = cerr
}
}
// getCredentials reads the OpenStack Credentials using the Hubic API // getCredentials reads the OpenStack Credentials using the Hubic API
// //
// The credentials are read into the Fs // The credentials are read into the Fs
@ -125,7 +115,7 @@ func (f *Fs) getCredentials() (err error) {
if err != nil { if err != nil {
return err return err
} }
defer checkClose(resp.Body, &err) defer fs.CheckClose(resp.Body, &err)
if resp.StatusCode < 200 || resp.StatusCode > 299 { if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Failed to get credentials: %s", resp.Status) return fmt.Errorf("Failed to get credentials: %s", resp.Status)
} }

View file

@ -29,7 +29,7 @@ func NewClient(c *http.Client, rootURL string) *Client {
// defaultErrorHandler doesn't attempt to parse the http body // defaultErrorHandler doesn't attempt to parse the http body
func defaultErrorHandler(resp *http.Response) (err error) { func defaultErrorHandler(resp *http.Response) (err error) {
defer checkClose(resp.Body, &err) defer fs.CheckClose(resp.Body, &err)
return fmt.Errorf("HTTP error %v (%v) returned", resp.StatusCode, resp.Status) return fmt.Errorf("HTTP error %v (%v) returned", resp.StatusCode, resp.Status)
} }
@ -52,18 +52,9 @@ type Opts struct {
ExtraHeaders map[string]string ExtraHeaders map[string]string
} }
// checkClose is a utility function used to check the return from
// Close in a defer statement.
func checkClose(c io.Closer, err *error) {
cerr := c.Close()
if *err == nil {
*err = cerr
}
}
// DecodeJSON decodes resp.Body into result // DecodeJSON decodes resp.Body into result
func DecodeJSON(resp *http.Response, result interface{}) (err error) { func DecodeJSON(resp *http.Response, result interface{}) (err error) {
defer checkClose(resp.Body, &err) defer fs.CheckClose(resp.Body, &err)
decoder := json.NewDecoder(resp.Body) decoder := json.NewDecoder(resp.Body)
return decoder.Decode(result) return decoder.Decode(result)
} }