main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "os"
  9. "regexp"
  10. "strings"
  11. "gopkg.in/yaml.v2"
  12. )
  13. var (
  14. appRoot string
  15. regexes []*regexp.Regexp
  16. stopWords []string
  17. assignRegex *regexp.Regexp
  18. base64Chars string
  19. hexChars string
  20. )
  21. // config
  22. type conf struct {
  23. Regexes []string `yaml:"regexes"`
  24. StopWords []string `yaml:"stopwords"`
  25. }
  26. // RepoElem used for parsing json from github api
  27. type RepoElem struct {
  28. RepoURL string `json:"html_url"`
  29. }
  30. func init() {
  31. var (
  32. err error
  33. c conf
  34. )
  35. appRoot, err = os.Getwd()
  36. if err != nil {
  37. log.Fatalf("Can't get working dir: %s", err)
  38. }
  39. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  40. hexChars = "1234567890abcdefABCDEF"
  41. // read config
  42. ymlFile, err := ioutil.ReadFile("config.yml")
  43. if err != nil {
  44. log.Printf("could not load config.yml #%v ", err)
  45. }
  46. err = yaml.Unmarshal(ymlFile, &c)
  47. if err != nil {
  48. log.Fatalf("Unmarshal: %v", err)
  49. }
  50. // regex from config
  51. stopWords = c.StopWords
  52. for _, re := range c.Regexes {
  53. regexes = append(regexes, regexp.MustCompile(re))
  54. }
  55. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  56. }
  57. func main() {
  58. args := os.Args[1:]
  59. opts := parseOptions(args)
  60. if opts.RepoURL != "" {
  61. start(opts)
  62. } else if opts.UserURL != "" || opts.OrgURL != "" {
  63. repoList := repoScan(opts)
  64. for _, repo := range repoList {
  65. opts.RepoURL = repo.RepoURL
  66. start(opts)
  67. }
  68. }
  69. }
  70. // repoScan attempts to parse all repo urls from an organization or user
  71. func repoScan(opts *Options) []RepoElem {
  72. var (
  73. targetURL string
  74. target string
  75. targetType string
  76. repoList []RepoElem
  77. )
  78. if opts.UserURL != "" {
  79. targetURL = opts.UserURL
  80. targetType = "users"
  81. } else {
  82. targetURL = opts.OrgURL
  83. targetType = "orgs"
  84. }
  85. splitTargetURL := strings.Split(targetURL, "/")
  86. target = splitTargetURL[len(splitTargetURL)-1]
  87. resp, err := http.Get(fmt.Sprintf("https://api.github.com/%s/%s/repos", targetType, target))
  88. if err != nil {
  89. log.Fatal(err)
  90. }
  91. defer resp.Body.Close()
  92. json.NewDecoder(resp.Body).Decode(&repoList)
  93. return repoList
  94. }