config.go 4.6 KB

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