config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "os/user"
  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. PacmanBin string `json:"pacmanbin"`
  27. PacmanConf string `json:"pacmanconf"`
  28. TarBin string `json:"tarbin"`
  29. RequestSplitN int `json:"requestsplitn"`
  30. SearchMode int `json:"-"`
  31. SortMode int `json:"sortmode"`
  32. TimeUpdate bool `json:"timeupdate"`
  33. NoConfirm bool `json:"noconfirm"`
  34. Devel bool `json:"devel"`
  35. CleanAfter bool `json:"cleanAfter"`
  36. }
  37. const version = "2.219"
  38. // baseURL givers the AUR default address.
  39. const baseURL string = "https://aur.archlinux.org"
  40. var specialDBsauce = false
  41. var savedInfo infos
  42. // configfile holds yay config file path.
  43. var configFile string
  44. // vcsfile holds yay vcs info file path.
  45. var vcsFile string
  46. //completion file
  47. var completionFile string
  48. // Updated returns if database has been updated
  49. var updated bool
  50. // YayConf holds the current config values for yay.
  51. var config Configuration
  52. // AlpmConf holds the current config values for pacman.
  53. var alpmConf alpm.PacmanConfig
  54. // AlpmHandle is the alpm handle used by yay.
  55. var alpmHandle *alpm.Handle
  56. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  57. file, err := os.Open(pacmanconf)
  58. if err != nil {
  59. return
  60. }
  61. conf, err = alpm.ParseConfig(file)
  62. if err != nil {
  63. return
  64. }
  65. return
  66. }
  67. // SaveConfig writes yay config to file.
  68. func (config *Configuration) saveConfig() error {
  69. config.NoConfirm = false
  70. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  71. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  72. if err != nil {
  73. return err
  74. }
  75. defer in.Close()
  76. _, err = in.Write(marshalledinfo)
  77. if err != nil {
  78. return err
  79. }
  80. err = in.Sync()
  81. return err
  82. }
  83. func defaultSettings(config *Configuration) {
  84. u, err := user.Current()
  85. if err != nil {
  86. panic(err)
  87. }
  88. config.BuildDir = fmt.Sprintf("/tmp/yaytmp-%s/", u.Uid)
  89. config.CleanAfter = false
  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. }