go.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package buildpacks
  2. import (
  3. "sync"
  4. "github.com/google/go-github/v41/github"
  5. )
  6. type goRuntime struct {
  7. wg sync.WaitGroup
  8. }
  9. func NewGoRuntime() Runtime {
  10. return &goRuntime{}
  11. }
  12. func (runtime *goRuntime) detectMod(results chan struct {
  13. string
  14. bool
  15. }, directoryContent []*github.RepositoryContent) {
  16. goModFound := false
  17. for i := 0; i < len(directoryContent); i++ {
  18. name := directoryContent[i].GetName()
  19. if name == "go.mod" {
  20. goModFound = true
  21. break
  22. }
  23. }
  24. if goModFound {
  25. results <- struct {
  26. string
  27. bool
  28. }{mod, true}
  29. }
  30. runtime.wg.Done()
  31. }
  32. func (runtime *goRuntime) detectDep(results chan struct {
  33. string
  34. bool
  35. }, directoryContent []*github.RepositoryContent) {
  36. gopkgFound := false
  37. vendorFound := false
  38. for i := 0; i < len(directoryContent); i++ {
  39. name := directoryContent[i].GetName()
  40. if name == "Gopkg.toml" {
  41. gopkgFound = true
  42. } else if name == "vendor" && directoryContent[i].GetType() == "dir" {
  43. vendorFound = true
  44. }
  45. if gopkgFound && vendorFound {
  46. break
  47. }
  48. }
  49. if gopkgFound && vendorFound {
  50. results <- struct {
  51. string
  52. bool
  53. }{dep, true}
  54. }
  55. runtime.wg.Done()
  56. }
  57. func (runtime *goRuntime) Detect(
  58. client *github.Client,
  59. directoryContent []*github.RepositoryContent,
  60. owner, name, path string,
  61. repoContentOptions github.RepositoryContentGetOptions,
  62. paketo, heroku *BuilderInfo,
  63. ) error {
  64. results := make(chan struct {
  65. string
  66. bool
  67. }, 2)
  68. runtime.wg.Add(2)
  69. go runtime.detectMod(results, directoryContent)
  70. go runtime.detectDep(results, directoryContent)
  71. runtime.wg.Wait()
  72. close(results)
  73. paketoBuildpackInfo := BuildpackInfo{
  74. Name: "Go",
  75. Buildpack: "gcr.io/paketo-buildpacks/go",
  76. }
  77. herokuBuildpackInfo := BuildpackInfo{
  78. Name: "Go",
  79. Buildpack: "heroku/go",
  80. }
  81. if len(results) == 0 {
  82. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  83. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  84. return nil
  85. }
  86. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  87. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  88. return nil
  89. }