config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Println(boldRedFgBlackBg("Warning:"),
  130. boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
  131. fmt.Println("Please add $EDITOR or to your environment variables.")
  132. editorLoop:
  133. fmt.Print(greenFg("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.Println(boldGreenFg(arrow+" "+s), boldWhiteFg(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. }