util.go 3.1 KB

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