util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package util
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. alpm "github.com/jguer/go-alpm"
  10. )
  11. // TarBin describes the default installation point of tar command.
  12. const TarBin string = "/usr/bin/bsdtar"
  13. // MakepkgBin describes the default installation point of makepkg command.
  14. const MakepkgBin string = "/usr/bin/makepkg"
  15. // SearchVerbosity determines print method used in PrintSearch
  16. var SearchVerbosity = NumberMenu
  17. // Verbosity settings for search
  18. const (
  19. NumberMenu = iota
  20. Detailed
  21. Minimal
  22. )
  23. // Shell describes the default user shell
  24. var Shell = "fish"
  25. // Build controls if packages will be built from ABS.
  26. var Build = false
  27. // NoConfirm ignores prompts.
  28. var NoConfirm = false
  29. // SortMode determines top down package or down top package display
  30. var SortMode = BottomUp
  31. // BaseDir is the default building directory for yay
  32. var BaseDir = "/tmp/yaytmp/"
  33. // Describes Sorting method for numberdisplay
  34. const (
  35. BottomUp = iota
  36. TopDown
  37. )
  38. // PacmanConf describes the default pacman config file
  39. const PacmanConf string = "/etc/pacman.conf"
  40. // Conf describes the default alpm config
  41. var Conf alpm.PacmanConfig
  42. func init() {
  43. Conf, _ = readConfig(PacmanConf)
  44. }
  45. // ContinueTask prompts if user wants to continue task.
  46. //If NoConfirm is set the action will continue without user input.
  47. func ContinueTask(s string, def string) (cont bool) {
  48. if NoConfirm {
  49. return true
  50. }
  51. var postFix string
  52. if def == "nN" {
  53. postFix = "(Y/n)"
  54. } else {
  55. postFix = "(y/N)"
  56. }
  57. var response string
  58. fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix)
  59. fmt.Scanln(&response)
  60. if response == string(def[0]) || response == string(def[1]) {
  61. return false
  62. }
  63. return true
  64. }
  65. func downloadFile(path string, url string) (err error) {
  66. // Create the file
  67. out, err := os.Create(path)
  68. if err != nil {
  69. return err
  70. }
  71. defer out.Close()
  72. // Get the data
  73. resp, err := http.Get(url)
  74. if err != nil {
  75. return err
  76. }
  77. defer resp.Body.Close()
  78. // Writer the body to file
  79. _, err = io.Copy(out, resp.Body)
  80. return err
  81. }
  82. // DownloadAndUnpack downloads url tgz and extracts to path.
  83. func DownloadAndUnpack(url string, path string, trim bool) (err error) {
  84. err = os.MkdirAll(path, 0755)
  85. if err != nil {
  86. return
  87. }
  88. tokens := strings.Split(url, "/")
  89. fileName := tokens[len(tokens)-1]
  90. tarLocation := path + fileName
  91. defer os.Remove(tarLocation)
  92. err = downloadFile(tarLocation, url)
  93. if err != nil {
  94. return
  95. }
  96. if trim {
  97. err = exec.Command("/bin/sh", "-c",
  98. TarBin+" --strip-components 2 --include='*/"+fileName[:len(fileName)-7]+"/trunk/' -xf "+tarLocation+" -C "+path).Run()
  99. os.Rename(path+"trunk", path+fileName[:len(fileName)-7]) // kurwa
  100. } else {
  101. err = exec.Command(TarBin, "-xf", tarLocation, "-C", path).Run()
  102. }
  103. if err != nil {
  104. return
  105. }
  106. return
  107. }
  108. // Editor returns the preferred system editor.
  109. func Editor() string {
  110. if os.Getenv("EDITOR") != "" {
  111. return os.Getenv("EDITOR")
  112. } else if os.Getenv("VISUAL") != "" {
  113. return os.Getenv("VISUAL")
  114. } else {
  115. 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")
  116. editorLoop:
  117. fmt.Printf("\x1b[32m%s\x1b[0m ", "Edit PKGBUILD with:")
  118. var editorInput string
  119. _, err := fmt.Scanln(&editorInput)
  120. if err != nil {
  121. fmt.Println(err)
  122. goto editorLoop
  123. }
  124. editor, err := exec.LookPath(editorInput)
  125. if err != nil {
  126. fmt.Println(err)
  127. goto editorLoop
  128. }
  129. return editor
  130. }
  131. }
  132. func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  133. file, err := os.Open(pacmanconf)
  134. if err != nil {
  135. return
  136. }
  137. conf, err = alpm.ParseConfig(file)
  138. if err != nil {
  139. return
  140. }
  141. return
  142. }