config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. alpm "github.com/jguer/go-alpm"
  11. )
  12. // Configuration stores yay's config
  13. type Configuration struct {
  14. BuildDir string
  15. Editor string
  16. MakepkgBin string
  17. NoConfirm bool
  18. PacmanBin string
  19. PacmanConf string
  20. SortMode string
  21. TarBin string
  22. }
  23. // YayConf holds the current config values for yay.
  24. var YayConf Configuration
  25. // AlpmConf holds the current config values for pacman.
  26. var AlpmConf alpm.PacmanConfig
  27. // AlpmHandle is the alpm handle used by yay
  28. var AlpmHandle alpm.Handle
  29. func init() {
  30. configfile := os.Getenv("HOME") + "/.config/yay/config.json"
  31. if _, err := os.Stat(configfile); os.IsNotExist(err) {
  32. _ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
  33. }
  34. file, err := os.Open(configfile)
  35. if err != nil {
  36. fmt.Println("Error reading config:", err)
  37. return
  38. }
  39. decoder := json.NewDecoder(file)
  40. err = decoder.Decode(&YayConf)
  41. if err != nil {
  42. fmt.Println("Error reading config:", err)
  43. }
  44. AlpmConf, err = readAlpmConfig(YayConf.PacmanConf)
  45. if err != nil {
  46. fmt.Println("Unable to read Pacman conf", err)
  47. }
  48. }
  49. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  50. file, err := os.Open(pacmanconf)
  51. if err != nil {
  52. return
  53. }
  54. conf, err = alpm.ParseConfig(file)
  55. if err != nil {
  56. return
  57. }
  58. return
  59. }
  60. func defaultSettings(config *Configuration) {
  61. config.BuildDir = "/tmp/yaytmp/"
  62. config.Editor = ""
  63. config.MakepkgBin = "/usr/bin/makepkg"
  64. config.NoConfirm = false
  65. config.PacmanBin = "/usr/bin/pacman"
  66. config.PacmanConf = "/etc/pacman.conf"
  67. config.SortMode = "BottomUp"
  68. config.TarBin = "/usr/bin/bsdtar"
  69. }
  70. // Editor returns the preferred system editor.
  71. func Editor() string {
  72. switch {
  73. case YayConf.Editor != "":
  74. editor, err := exec.LookPath(YayConf.Editor)
  75. if err != nil {
  76. fmt.Println(err)
  77. } else {
  78. return editor
  79. }
  80. fallthrough
  81. case os.Getenv("EDITOR") != "":
  82. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  83. if err != nil {
  84. fmt.Println(err)
  85. } else {
  86. return editor
  87. }
  88. fallthrough
  89. case os.Getenv("VISUAL") != "":
  90. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  91. if err != nil {
  92. fmt.Println(err)
  93. } else {
  94. return editor
  95. }
  96. fallthrough
  97. default:
  98. fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m$EDITOR\x1b[0;37;40m is not set.\x1b[0m\nPlease add $EDITOR or to your environment variables.\n")
  99. editorLoop:
  100. fmt.Printf("\x1b[32m%s\x1b[0m ", "Edit PKGBUILD with:")
  101. var editorInput string
  102. _, err := fmt.Scanln(&editorInput)
  103. if err != nil {
  104. fmt.Println(err)
  105. goto editorLoop
  106. }
  107. editor, err := exec.LookPath(editorInput)
  108. if err != nil {
  109. fmt.Println(err)
  110. goto editorLoop
  111. }
  112. return editor
  113. }
  114. }
  115. // ContinueTask prompts if user wants to continue task.
  116. //If NoConfirm is set the action will continue without user input.
  117. func ContinueTask(s string, def string) (cont bool) {
  118. if YayConf.NoConfirm {
  119. return true
  120. }
  121. var postFix string
  122. if def == "nN" {
  123. postFix = "[Y/n] "
  124. } else {
  125. postFix = "[y/N] "
  126. }
  127. var response string
  128. fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m", s, postFix)
  129. n, err := fmt.Scanln(&response)
  130. if err != nil || n == 0 {
  131. return true
  132. }
  133. if response == string(def[0]) || response == string(def[1]) {
  134. return false
  135. }
  136. return true
  137. }
  138. func downloadFile(path string, url string) (err error) {
  139. // Create the file
  140. out, err := os.Create(path)
  141. if err != nil {
  142. return err
  143. }
  144. defer out.Close()
  145. // Get the data
  146. resp, err := http.Get(url)
  147. if err != nil {
  148. return err
  149. }
  150. defer resp.Body.Close()
  151. // Writer the body to file
  152. _, err = io.Copy(out, resp.Body)
  153. return err
  154. }
  155. // DownloadAndUnpack downloads url tgz and extracts to path.
  156. func DownloadAndUnpack(url string, path string, trim bool) (err error) {
  157. err = os.MkdirAll(path, 0755)
  158. if err != nil {
  159. return
  160. }
  161. tokens := strings.Split(url, "/")
  162. fileName := tokens[len(tokens)-1]
  163. tarLocation := path + fileName
  164. defer os.Remove(tarLocation)
  165. err = downloadFile(tarLocation, url)
  166. if err != nil {
  167. return
  168. }
  169. if trim {
  170. err = exec.Command("/bin/sh", "-c",
  171. YayConf.TarBin+" --strip-components 2 --include='*/"+fileName[:len(fileName)-7]+"/trunk/' -xf "+tarLocation+" -C "+path).Run()
  172. os.Rename(path+"trunk", path+fileName[:len(fileName)-7]) // kurwa
  173. } else {
  174. err = exec.Command(YayConf.TarBin, "-xf", tarLocation, "-C", path).Run()
  175. }
  176. if err != nil {
  177. return
  178. }
  179. return
  180. }