util.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // NoConfirm ignores prompts.
  23. var NoConfirm = false
  24. // SortMode determines top down package or down top package display
  25. var SortMode = BottomUp
  26. // BaseDir is the default building directory for yay
  27. var BaseDir = "/tmp/yaytmp/"
  28. // Describes Sorting method for numberdisplay
  29. const (
  30. BottomUp = iota
  31. TopDown
  32. )
  33. // ContinueTask prompts if user wants to continue task.
  34. //If NoConfirm is set the action will continue without user input.
  35. func ContinueTask(s string, def string) (cont bool) {
  36. if NoConfirm {
  37. return true
  38. }
  39. var postFix string
  40. if def == "nN" {
  41. postFix = "(Y/n)"
  42. } else {
  43. postFix = "(y/N)"
  44. }
  45. var response string
  46. fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix)
  47. fmt.Scanln(&response)
  48. if response == string(def[0]) || response == string(def[1]) {
  49. return false
  50. }
  51. return true
  52. }
  53. func downloadFile(path string, url string) (err error) {
  54. // Create the file
  55. out, err := os.Create(path)
  56. if err != nil {
  57. return err
  58. }
  59. defer out.Close()
  60. // Get the data
  61. resp, err := http.Get(url)
  62. if err != nil {
  63. return err
  64. }
  65. defer resp.Body.Close()
  66. // Writer the body to file
  67. _, err = io.Copy(out, resp.Body)
  68. return err
  69. }
  70. // DownloadAndUnpack downloads url tgz and extracts to path.
  71. func DownloadAndUnpack(url string, path string, trim bool) (err error) {
  72. err = os.MkdirAll(path, 0755)
  73. if err != nil {
  74. return
  75. }
  76. tokens := strings.Split(url, "/")
  77. fileName := tokens[len(tokens)-1]
  78. tarLocation := path + fileName
  79. defer os.Remove(tarLocation)
  80. err = downloadFile(tarLocation, url)
  81. if err != nil {
  82. return
  83. }
  84. if trim {
  85. err = exec.Command("/bin/sh", "-c",
  86. TarBin+" --strip-components 2 --include='*/"+fileName[:len(fileName)-7]+"/trunk/' -xf "+tarLocation+" -C "+path).Run()
  87. os.Rename(path+"trunk", path+fileName[:len(fileName)-7]) // kurwa
  88. } else {
  89. err = exec.Command(TarBin, "-xf", tarLocation, "-C", path).Run()
  90. }
  91. if err != nil {
  92. return
  93. }
  94. return
  95. }