Created
July 7, 2016 15:15
-
-
Save rjz/ede1eab52d73bde7ec3e1ba597c3ccdd to your computer and use it in GitHub Desktop.
Extract base64-encoded github.Blob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/base64" | |
"errors" | |
"fmt" | |
"github.com/google/go-github/github" | |
) | |
// Extract a Blob with arbitrary encoding into a byte array | |
func ExtractBlob(blob *github.Blob) (*[]byte, error) { | |
bytes := []byte(*blob.Content) | |
switch *blob.Encoding { | |
case "utf-8": | |
return &bytes, nil | |
case "base64": | |
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(bytes))) | |
base64.StdEncoding.Decode(decoded, bytes) | |
unpadded := decoded[:*blob.Size] | |
return &unpadded, nil | |
default: | |
return nil, errors.New(fmt.Sprintf("Unknown encoding for github blob'%s'", *blob.Encoding)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment