Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active October 3, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/034761375c618b67457fd09e49555c4b to your computer and use it in GitHub Desktop.
Save jmervine/034761375c618b67457fd09e49555c4b to your computer and use it in GitHub Desktop.
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