config.go 4.8 KB

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