Last active
October 3, 2017 18:32
-
-
Save jmervine/034761375c618b67457fd09e49555c4b to your computer and use it in GitHub Desktop.
Fetch a file from a private repo on github.
This file contains 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
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