main.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. )
  12. var (
  13. appRoot string
  14. regexes map[string]*regexp.Regexp
  15. assignRegex *regexp.Regexp
  16. base64Chars string
  17. hexChars string
  18. )
  19. func init() {
  20. var err error
  21. appRoot, err = os.Getwd()
  22. if err != nil {
  23. log.Fatalf("Can't get working dir: %s", err)
  24. }
  25. // TODO update regex to look for things like:
  26. // TODO ability to add/filter regex
  27. // client("AKAI32fJ334...",
  28. regexes = map[string]*regexp.Regexp{
  29. "github": regexp.MustCompile(`[g|G][i|I][t|T][h|H][u|U][b|B].*(=|:=|<-).*\w+.*`),
  30. "aws": regexp.MustCompile(`[a|A][w|W][s|S].*(=|:=|:|<-).*\w+.*`),
  31. "heroku": regexp.MustCompile(`[h|H][e|E][r|R][o|O][k|K][u|U].*(=|:=|<-).*\w+.*`),
  32. "facebook": regexp.MustCompile(`[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*(=|:=|<-).*\w+.*`),
  33. "twitter": regexp.MustCompile(`[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*(=|:=|<-).*\w+.*`),
  34. "reddit": regexp.MustCompile(`[r|R][e|E][d|D][d|D][i|I][t|T].*(=|:=|<-).*\w+.*`),
  35. "twilio": regexp.MustCompile(`[t|T][w|W][i|I][l|L][i|I][o|O].*(=|:=|<-).*\w+.*`),
  36. }
  37. assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
  38. base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  39. hexChars = "1234567890abcdefABCDEF"
  40. }
  41. func main() {
  42. args := os.Args[1:]
  43. opts := parseOptions(args)
  44. if opts.RepoURL != "" {
  45. start(opts)
  46. } else if opts.UserURL != "" || opts.OrgURL != "" {
  47. repoList := repoScan(opts)
  48. for _, repo := range repoList {
  49. opts.RepoURL = repo.RepoURL
  50. start(opts)
  51. }
  52. }
  53. }
  54. // RepoElem used for parsing json from github api
  55. type RepoElem struct {
  56. RepoURL string `json:"html_url"`
  57. }
  58. // repoScan attempts to parse all repo urls from an organization or user
  59. func repoScan(opts *Options) []RepoElem {
  60. var (
  61. targetURL string
  62. target string
  63. targetType string
  64. repoList []RepoElem
  65. )
  66. if opts.UserURL != "" {
  67. targetURL = opts.UserURL
  68. targetType = "users"
  69. } else {
  70. targetURL = opts.OrgURL
  71. targetType = "orgs"
  72. }
  73. splitTargetURL := strings.Split(targetURL, "/")
  74. target = splitTargetURL[len(splitTargetURL)-1]
  75. resp, err := http.Get(fmt.Sprintf("https://api.github.com/%s/%s/repos", targetType, target))
  76. if err != nil {
  77. log.Fatal(err)
  78. }
  79. defer resp.Body.Close()
  80. json.NewDecoder(resp.Body).Decode(&repoList)
  81. return repoList
  82. }