vcs.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "strings"
  9. )
  10. // branch contains the information of a repository branch
  11. type branch struct {
  12. Name string `json:"name"`
  13. Commit struct {
  14. SHA string `json:"sha"`
  15. URL string `json:"url"`
  16. } `json:"commit"`
  17. }
  18. type branches []branch
  19. // Info contains the last commit sha of a repo
  20. type Info struct {
  21. Package string `json:"pkgname"`
  22. URL string `json:"url"`
  23. SHA string `json:"sha"`
  24. }
  25. type infos []Info
  26. // createDevelDB forces yay to create a DB of the existing development packages
  27. func createDevelDB() error {
  28. _, _, _, remoteNames, err := filterPackages()
  29. if err != nil {
  30. return err
  31. }
  32. config.NoConfirm = true
  33. arguments := makeArguments()
  34. arguments.addArg("gendb")
  35. arguments.addTarget(remoteNames...)
  36. err = install(arguments)
  37. return err
  38. }
  39. // parseSource returns owner and repo from source
  40. func parseSource(source string) (owner string, repo string) {
  41. if !(strings.Contains(source, "git://") ||
  42. strings.Contains(source, ".git") ||
  43. strings.Contains(source, "git+https://")) {
  44. return
  45. }
  46. split := strings.Split(source, "github.com/")
  47. if len(split) > 1 {
  48. secondSplit := strings.Split(split[1], "/")
  49. if len(secondSplit) > 1 {
  50. owner = secondSplit[0]
  51. thirdSplit := strings.Split(secondSplit[1], ".git")
  52. if len(thirdSplit) > 0 {
  53. repo = thirdSplit[0]
  54. }
  55. }
  56. }
  57. return
  58. }
  59. func (info *Info) needsUpdate() bool {
  60. var newRepo branches
  61. r, err := http.Get(info.URL)
  62. if err != nil {
  63. fmt.Println(err)
  64. return false
  65. }
  66. defer r.Body.Close()
  67. body, _ := ioutil.ReadAll(r.Body)
  68. err = json.Unmarshal(body, &newRepo)
  69. if err != nil {
  70. fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
  71. info.Package, err, r.StatusCode, string(body))
  72. return false
  73. }
  74. for _, e := range newRepo {
  75. if e.Name == "master" {
  76. return e.Commit.SHA != info.SHA
  77. }
  78. }
  79. return false
  80. }
  81. func inStore(pkgName string) *Info {
  82. for i, e := range savedInfo {
  83. if pkgName == e.Package {
  84. return &savedInfo[i]
  85. }
  86. }
  87. return nil
  88. }
  89. // branchInfo updates saved information
  90. func branchInfo(pkgName string, owner string, repo string) (err error) {
  91. updated = true
  92. var newRepo branches
  93. url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches"
  94. r, err := http.Get(url)
  95. if err != nil {
  96. return
  97. }
  98. defer r.Body.Close()
  99. _ = json.NewDecoder(r.Body).Decode(&newRepo)
  100. packinfo := inStore(pkgName)
  101. for _, e := range newRepo {
  102. if e.Name == "master" {
  103. if packinfo != nil {
  104. packinfo.Package = pkgName
  105. packinfo.URL = url
  106. packinfo.SHA = e.Commit.SHA
  107. } else {
  108. savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
  109. }
  110. }
  111. }
  112. return
  113. }
  114. func saveVCSInfo() error {
  115. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  116. if err != nil || string(marshalledinfo) == "null" {
  117. return err
  118. }
  119. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  120. if err != nil {
  121. return err
  122. }
  123. defer in.Close()
  124. _, err = in.Write(marshalledinfo)
  125. if err != nil {
  126. return err
  127. }
  128. err = in.Sync()
  129. return err
  130. }