util.go 3.1 KB

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