main.go 2.0 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. base64Chars string
  18. hexChars string
  19. opts *Options
  20. assignRegex *regexp.Regexp
  21. )
  22. // config
  23. type conf struct {
  24. Regexes []string `yaml:"regexes"`
  25. StopWords []string `yaml:"stopwords"`
  26. }
  27. // RepoElem used for parsing json from github api
  28. type RepoElem struct {
  29. RepoURL string `json:"html_url"`
  30. }
  31. func init() {
  32. var (
  33. err error
  34. c conf
  35. )
  36. appRoot, err = os.Getwd()
  37. if err != nil {
  38. log.Fatalf("Can't get working dir: %s", err)
  39. }
  40. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  41. hexChars = "1234567890abcdefABCDEF"
  42. // read config
  43. ymlFile, err := ioutil.ReadFile("config.yml")
  44. if err != nil {
  45. log.Printf("could not load config.yml #%v ", err)
  46. }
  47. err = yaml.Unmarshal(ymlFile, &c)
  48. if err != nil {
  49. log.Fatalf("Unmarshal: %v", err)
  50. }
  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. }