config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. alpm "github.com/jguer/go-alpm"
  9. )
  10. // Verbosity settings for search
  11. const (
  12. NumberMenu = iota
  13. Detailed
  14. Minimal
  15. )
  16. // Describes Sorting method for numberdisplay
  17. const (
  18. BottomUp = iota
  19. TopDown
  20. )
  21. // Configuration stores yay's config.
  22. type Configuration struct {
  23. BuildDir string `json:"buildDir"`
  24. Editor string `json:"editor"`
  25. MakepkgBin string `json:"makepkgbin"`
  26. Shell string `json:"-"`
  27. NoConfirm bool `json:"noconfirm"`
  28. Devel bool `json:"devel"`
  29. PacmanBin string `json:"pacmanbin"`
  30. PacmanConf string `json:"pacmanconf"`
  31. RequestSplitN int `json:"requestsplitn"`
  32. SearchMode int `json:"-"`
  33. SortMode int `json:"sortmode"`
  34. TarBin string `json:"tarbin"`
  35. TimeUpdate bool `json:"timeupdate"`
  36. }
  37. var version = "2.116"
  38. // baseURL givers the AUR default address.
  39. const baseURL string = "https://aur.archlinux.org"
  40. var specialDBsauce = false
  41. var savedInfo infos
  42. var configfile string
  43. // Updated returns if database has been updated
  44. var updated bool
  45. // YayConf holds the current config values for yay.
  46. var config Configuration
  47. // AlpmConf holds the current config values for pacman.
  48. var AlpmConf alpm.PacmanConfig
  49. // AlpmHandle is the alpm handle used by yay.
  50. var AlpmHandle *alpm.Handle
  51. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  52. file, err := os.Open(pacmanconf)
  53. if err != nil {
  54. return
  55. }
  56. conf, err = alpm.ParseConfig(file)
  57. if err != nil {
  58. return
  59. }
  60. return
  61. }
  62. // SaveConfig writes yay config to file.
  63. func (config *Configuration) saveConfig() error {
  64. config.NoConfirm = false
  65. configfile := os.Getenv("HOME") + "/.config/yay/config.json"
  66. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  67. in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0755)
  68. if err != nil {
  69. return err
  70. }
  71. defer in.Close()
  72. _, err = in.Write(marshalledinfo)
  73. if err != nil {
  74. return err
  75. }
  76. err = in.Sync()
  77. return err
  78. }
  79. func defaultSettings(config *Configuration) {
  80. config.BuildDir = "/tmp/yaytmp/"
  81. config.Editor = ""
  82. config.Devel = false
  83. config.MakepkgBin = "/usr/bin/makepkg"
  84. config.NoConfirm = false
  85. config.PacmanBin = "/usr/bin/pacman"
  86. config.PacmanConf = "/etc/pacman.conf"
  87. config.SortMode = BottomUp
  88. config.TarBin = "/usr/bin/bsdtar"
  89. config.TimeUpdate = false
  90. config.RequestSplitN = 150
  91. }
  92. // Editor returns the preferred system editor.
  93. func editor() string {
  94. switch {
  95. case config.Editor != "":
  96. editor, err := exec.LookPath(config.Editor)
  97. if err != nil {
  98. fmt.Println(err)
  99. } else {
  100. return editor
  101. }
  102. fallthrough
  103. case os.Getenv("EDITOR") != "":
  104. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  105. if err != nil {
  106. fmt.Println(err)
  107. } else {
  108. return editor
  109. }
  110. fallthrough
  111. case os.Getenv("VISUAL") != "":
  112. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  113. if err != nil {
  114. fmt.Println(err)
  115. } else {
  116. return editor
  117. }
  118. fallthrough
  119. default:
  120. 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")
  121. editorLoop:
  122. fmt.Printf("\x1b[32m%s\x1b[0m ", "Edit PKGBUILD with:")
  123. var editorInput string
  124. _, err := fmt.Scanln(&editorInput)
  125. if err != nil {
  126. fmt.Println(err)
  127. goto editorLoop
  128. }
  129. editor, err := exec.LookPath(editorInput)
  130. if err != nil {
  131. fmt.Println(err)
  132. goto editorLoop
  133. }
  134. return editor
  135. }
  136. }
  137. // ContinueTask prompts if user wants to continue task.
  138. //If NoConfirm is set the action will continue without user input.
  139. func continueTask(s string, def string) (cont bool) {
  140. if config.NoConfirm {
  141. return true
  142. }
  143. var postFix string
  144. if def == "nN" {
  145. postFix = "[Y/n] "
  146. } else {
  147. postFix = "[y/N] "
  148. }
  149. var response string
  150. fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m", s, postFix)
  151. n, err := fmt.Scanln(&response)
  152. if err != nil || n == 0 {
  153. return true
  154. }
  155. if response == string(def[0]) || response == string(def[1]) {
  156. return false
  157. }
  158. return true
  159. }
  160. // PassToPacman outsorces execution to pacman binary without modifications.
  161. func passToPacman(op string, pkgs []string, flags []string) error {
  162. var cmd *exec.Cmd
  163. var args []string
  164. args = append(args, op)
  165. if len(pkgs) != 0 {
  166. args = append(args, pkgs...)
  167. }
  168. if len(flags) != 0 {
  169. args = append(args, flags...)
  170. }
  171. if strings.Contains(op, "-Q") || op == "Si" {
  172. cmd = exec.Command(config.PacmanBin, args...)
  173. } else {
  174. args = append([]string{config.PacmanBin}, args...)
  175. cmd = exec.Command("sudo", args...)
  176. }
  177. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  178. err := cmd.Run()
  179. return err
  180. }