Skip to content

Instantly share code, notes, and snippets.

@rjz
Created July 7, 2016 15:15
Show Gist options
  • Save rjz/ede1eab52d73bde7ec3e1ba597c3ccdd to your computer and use it in GitHub Desktop.
Save rjz/ede1eab52d73bde7ec3e1ba597c3ccdd to your computer and use it in GitHub Desktop.
Extract base64-encoded github.Blob
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