config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  67. in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0644)
  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. u, err := user.Current()
  81. if err != nil {
  82. panic(err)
  83. }
  84. config.BuildDir = fmt.Sprintf("/tmp/yaytmp-%s/", u.Uid)
  85. config.Editor = ""
  86. config.Devel = false
  87. config.MakepkgBin = "/usr/bin/makepkg"
  88. config.NoConfirm = false
  89. config.PacmanBin = "/usr/bin/pacman"
  90. config.PacmanConf = "/etc/pacman.conf"
  91. config.SortMode = BottomUp
  92. config.TarBin = "/usr/bin/bsdtar"
  93. config.TimeUpdate = false
  94. config.RequestSplitN = 150
  95. }
  96. // Editor returns the preferred system editor.
  97. func editor() string {
  98. switch {
  99. case config.Editor != "":
  100. editor, err := exec.LookPath(config.Editor)
  101. if err != nil {
  102. fmt.Println(err)
  103. } else {
  104. return editor
  105. }
  106. fallthrough
  107. case os.Getenv("EDITOR") != "":
  108. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  109. if err != nil {
  110. fmt.Println(err)
  111. } else {
  112. return editor
  113. }
  114. fallthrough
  115. case os.Getenv("VISUAL") != "":
  116. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  117. if err != nil {
  118. fmt.Println(err)
  119. } else {
  120. return editor
  121. }
  122. fallthrough
  123. default:
  124. 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")
  125. editorLoop:
  126. fmt.Printf("\x1b[32m%s\x1b[0m ", "Edit PKGBUILD with:")
  127. var editorInput string
  128. _, err := fmt.Scanln(&editorInput)
  129. if err != nil {
  130. fmt.Println(err)
  131. goto editorLoop
  132. }
  133. editor, err := exec.LookPath(editorInput)
  134. if err != nil {
  135. fmt.Println(err)
  136. goto editorLoop
  137. }
  138. return editor
  139. }
  140. }
  141. // ContinueTask prompts if user wants to continue task.
  142. //If NoConfirm is set the action will continue without user input.
  143. func continueTask(s string, def string) (cont bool) {
  144. if config.NoConfirm {
  145. return true
  146. }
  147. var postFix string
  148. if def == "nN" {
  149. postFix = "[Y/n] "
  150. } else {
  151. postFix = "[y/N] "
  152. }
  153. var response string
  154. fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m", s, postFix)
  155. n, err := fmt.Scanln(&response)
  156. if err != nil || n == 0 {
  157. return true
  158. }
  159. if response == string(def[0]) || response == string(def[1]) {
  160. return false
  161. }
  162. return true
  163. }
  164. // PassToPacman outsorces execution to pacman binary without modifications.
  165. func passToPacman(op string, pkgs []string, flags []string) error {
  166. var cmd *exec.Cmd
  167. var args []string
  168. args = append(args, op)
  169. if len(pkgs) != 0 {
  170. args = append(args, pkgs...)
  171. }
  172. if len(flags) != 0 {
  173. args = append(args, flags...)
  174. }
  175. if strings.Contains(op, "-Q") || op == "Si" {
  176. cmd = exec.Command(config.PacmanBin, args...)
  177. } else {
  178. args = append([]string{config.PacmanBin}, args...)
  179. cmd = exec.Command("sudo", args...)
  180. }
  181. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  182. err := cmd.Run()
  183. return err
  184. }