From 3c93938f8a80ea269baa727322d4da012c77730c Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Tue, 13 Jan 2015 16:24:59 -0800 Subject: [PATCH] decode: refactor file decoding into config package --- config/decode.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ system/file.go | 54 +--------------------------------------------- 2 files changed, 57 insertions(+), 53 deletions(-) create mode 100644 config/decode.go diff --git a/config/decode.go b/config/decode.go new file mode 100644 index 0000000..f5847aa --- /dev/null +++ b/config/decode.go @@ -0,0 +1,56 @@ +package config + +import ( + "bytes" + "compress/gzip" + "encoding/base64" + "fmt" +) + +func DecodeBase64Content(content string) ([]byte, error) { + output, err := base64.StdEncoding.DecodeString(content) + + if err != nil { + return nil, fmt.Errorf("Unable to decode base64: %q", err) + } + + return output, nil +} + +func DecodeGzipContent(content string) ([]byte, error) { + gzr, err := gzip.NewReader(bytes.NewReader([]byte(content))) + + if err != nil { + return nil, fmt.Errorf("Unable to decode gzip: %q", err) + } + defer gzr.Close() + + buf := new(bytes.Buffer) + buf.ReadFrom(gzr) + + return buf.Bytes(), nil +} + +func DecodeContent(content string, encoding string) ([]byte, error) { + switch encoding { + case "": + return []byte(content), nil + + case "b64", "base64": + return DecodeBase64Content(content) + + case "gz", "gzip": + return DecodeGzipContent(content) + + case "gz+base64", "gzip+base64", "gz+b64", "gzip+b64": + gz, err := DecodeBase64Content(content) + + if err != nil { + return nil, err + } + + return DecodeGzipContent(string(gz)) + } + + return nil, fmt.Errorf("Unsupported encoding %q", encoding) +} diff --git a/system/file.go b/system/file.go index 22118c1..9454498 100644 --- a/system/file.go +++ b/system/file.go @@ -17,9 +17,6 @@ package system import ( - "bytes" - "compress/gzip" - "encoding/base64" "fmt" "io/ioutil" "log" @@ -50,61 +47,12 @@ func (f *File) Permissions() (os.FileMode, error) { return os.FileMode(perm), nil } -func DecodeBase64Content(content string) ([]byte, error) { - output, err := base64.StdEncoding.DecodeString(content) - - if err != nil { - return nil, fmt.Errorf("Unable to decode base64: %v", err) - } - - return output, nil -} - -func DecodeGzipContent(content string) ([]byte, error) { - gzr, err := gzip.NewReader(bytes.NewReader([]byte(content))) - - if err != nil { - return nil, fmt.Errorf("Unable to decode gzip: %v", err) - } - defer gzr.Close() - - buf := new(bytes.Buffer) - buf.ReadFrom(gzr) - - return buf.Bytes(), nil -} - -func DecodeContent(content string, encoding string) ([]byte, error) { - switch encoding { - case "": - return []byte(content), nil - - case "b64", "base64": - return DecodeBase64Content(content) - - case "gz", "gzip": - return DecodeGzipContent(content) - - case "gz+base64", "gzip+base64", "gz+b64", "gzip+b64": - gz, err := DecodeBase64Content(content) - - if err != nil { - return nil, err - } - - return DecodeGzipContent(string(gz)) - } - - return nil, fmt.Errorf("Unsupported encoding %s", encoding) - -} - func WriteFile(f *File, root string) (string, error) { fullpath := path.Join(root, f.Path) dir := path.Dir(fullpath) log.Printf("Writing file to %q", fullpath) - content, err := DecodeContent(f.Content, f.Encoding) + content, err := config.DecodeContent(f.Content, f.Encoding) if err != nil { return "", fmt.Errorf("Unable to decode %s (%v)", f.Path, err)