Skip to content

Instantly share code, notes, and snippets.

@bign8
Last active August 19, 2016 16:17
Show Gist options
  • Save bign8/0300a421e1e255b955029b0e404d1a70 to your computer and use it in GitHub Desktop.
Save bign8/0300a421e1e255b955029b0e404d1a70 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
)
var token = flag.String("token", os.Getenv("GITHUB_TOKEN"), "Github Token (see: https://github.com/settings/tokens)")
var slug = flag.String("slug", "", "Github Repo Slug (ex: bign8/games)")
var file = flag.String("file", "", "File to fetch")
func main() {
flag.Parse()
if *slug == "" {
fmt.Println("no repo slug provided")
}
if *file == "" {
fmt.Println("no file provided")
}
if *slug == "" || *file == "" {
flag.Usage()
os.Exit(1)
}
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.github.com/repos/"+*slug+"/contents/"+*file, nil)
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "token "+*token)
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
obj := struct {
Content []byte `json:"content"`
}{}
if err = json.NewDecoder(res.Body).Decode(&obj); err != nil {
panic(err)
}
fmt.Println(string(obj.Content))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment