config.go 4.7 KB

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