github.com/AngusLu/go-swagger@v0.28.0/hack/download-stats.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "log" 8 "net/http" 9 "os" 10 ) 11 12 func main() { 13 req, err := http.NewRequest("GET", "https://api.github.com/repos/go-swagger/go-swagger/releases", nil) 14 // req, err := http.NewRequest("GET", "https://api.github.com/repos/go-swagger/go-swagger/releases/latest", nil) 15 if err != nil { 16 log.Fatalln(err) 17 } 18 19 req.Header.Add("Authorization", "token "+os.Getenv("GITHUB_TOKEN")) 20 21 resp, err := http.DefaultClient.Do(req) 22 if err != nil { 23 log.Fatalln(err) 24 } 25 defer resp.Body.Close() 26 27 if resp.StatusCode/100 != 2 { 28 b, err := ioutil.ReadAll(resp.Body) 29 if err != nil { 30 log.Fatalln(err) //nolint: gocritic 31 } 32 log.Fatalf("%s: %s", resp.Status, b) //nolint: gocritic 33 } 34 35 var results []struct { 36 TagName string `json:"tag_name"` 37 Assets []struct { 38 Name string `json:"name"` 39 Downloads int64 `json:"download_count"` 40 } `json:"assets"` 41 } 42 43 dec := json.NewDecoder(resp.Body) 44 if err := dec.Decode(&results); err != nil { 45 log.Fatalln(err) 46 } 47 48 for i, result := range results { 49 if i > 0 { 50 fmt.Println() 51 } 52 53 fmt.Println("Stats for release:", result.TagName) 54 for _, asset := range result.Assets { 55 fmt.Printf("%25s: %d\n", asset.Name, asset.Downloads) 56 } 57 } 58 }