main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "sort"
  8. "time"
  9. "github.com/cryptix/go/logging"
  10. "github.com/dustin/go-humanize"
  11. "github.com/jinzhu/configor"
  12. "github.com/kr/pretty"
  13. "github.com/shurcooL/githubql"
  14. "golang.org/x/oauth2"
  15. )
  16. var (
  17. log logging.Interface
  18. check = logging.CheckFatal
  19. )
  20. var cfgFile = flag.String("config", "my.cfg", "which config file to use")
  21. type Config struct {
  22. Github struct {
  23. AccessToken string
  24. }
  25. Config struct {
  26. Email string
  27. UpdateInterval string
  28. Debug bool
  29. Update string
  30. }
  31. }
  32. func main() {
  33. logging.SetupLogging(os.Stdout)
  34. log = logging.Logger(os.Args[0])
  35. // setup config
  36. var cfg Config
  37. check(configor.Load(&cfg, *cfgFile))
  38. pretty.Println(cfg)
  39. src := oauth2.StaticTokenSource(
  40. &oauth2.Token{AccessToken: cfg.Github.AccessToken},
  41. )
  42. httpClient := oauth2.NewClient(context.Background(), src)
  43. client := githubql.NewClient(httpClient)
  44. var query struct {
  45. Viewer struct {
  46. StarredRepositories struct {
  47. PageInfo struct {
  48. EndCursor githubql.String
  49. HasNextPage githubql.Boolean
  50. }
  51. TotalCount githubql.Int
  52. Nodes []repo
  53. } `graphql:"starredRepositories(first: 100, after: $starCursor)"`
  54. }
  55. }
  56. variables := map[string]interface{}{
  57. "starCursor": (*githubql.String)(nil),
  58. }
  59. var allStars []repo
  60. var i = 0
  61. for {
  62. start := time.Now()
  63. err := client.Query(context.Background(), &query, variables)
  64. check(err)
  65. allStars = append(allStars, query.Viewer.StarredRepositories.Nodes...)
  66. if !query.Viewer.StarredRepositories.PageInfo.HasNextPage {
  67. break
  68. }
  69. log.Log("page", i, "took", fmt.Sprintf("%v", time.Since(start)))
  70. i++
  71. variables["starCursor"] = githubql.NewString(query.Viewer.StarredRepositories.PageInfo.EndCursor)
  72. }
  73. kbytes := 0
  74. sort.Sort(BySize(allStars))
  75. for i, r := range allStars {
  76. log.Log("star", i,
  77. "name", r.NameWithOwner,
  78. "size", humanize.Bytes(uint64(r.DiskUsage)*1024))
  79. kbytes += int(r.DiskUsage)
  80. }
  81. log.Log("total", len(allStars), "size", humanize.Bytes(uint64(kbytes*1024)))
  82. }
  83. type repo struct {
  84. Id githubql.ID
  85. NameWithOwner githubql.String
  86. Description githubql.String
  87. Url githubql.URI
  88. Ref struct {
  89. Id githubql.ID
  90. Target struct {
  91. Oid githubql.GitObjectID
  92. }
  93. } `graphql:"ref(qualifiedName:\"master\")"`
  94. DiskUsage githubql.Int
  95. LicenseInfo struct {
  96. Id githubql.ID
  97. Name githubql.String
  98. SpdxId githubql.String
  99. }
  100. }
  101. type BySize []repo
  102. func (a BySize) Len() int { return len(a) }
  103. func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  104. func (a BySize) Less(i, j int) bool { return a[i].DiskUsage < a[j].DiskUsage }