Skip to content

Instantly share code, notes, and snippets.

@jmervine jmervine/main.go
Last active Oct 3, 2017

Embed
What would you like to do?
Fetch a file from a private repo on github.
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
)
var (
githubToken = os.Getenv("GITHUB_TOKEN")
splunkUsers = os.Getenv("GITHUB_FILE")
repoName = os.Getenv("GITHUB_REPO")
fileType = "application/vnd.github.v3.raw"
)
func main() {
endpoint := fmt.Sprintf("https://api.github.com/repos/%s/contents/%s",
repoName, splunkUsers)
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", fileType)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", githubToken))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
panic(errors.New(resp.Status))
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
panic(err)
}
fmt.Printf(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.