config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. var version = "2.297"
  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. // changedConfig holds whether or not the config has changed
  51. var changedConfig 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.Println(boldRedFgBlackBg("Warning:"),
  132. boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
  133. fmt.Println("Please add $EDITOR or to your environment variables.")
  134. editorLoop:
  135. fmt.Print(greenFg("Edit PKGBUILD with:"))
  136. var editorInput string
  137. _, err := fmt.Scanln(&editorInput)
  138. if err != nil {
  139. fmt.Println(err)
  140. goto editorLoop
  141. }
  142. editor, err := exec.LookPath(editorInput)
  143. if err != nil {
  144. fmt.Println(err)
  145. goto editorLoop
  146. }
  147. return editor
  148. }
  149. }
  150. // ContinueTask prompts if user wants to continue task.
  151. //If NoConfirm is set the action will continue without user input.
  152. func continueTask(s string, def string) (cont bool) {
  153. if config.NoConfirm {
  154. return true
  155. }
  156. var postFix string
  157. if def == "nN" {
  158. postFix = "[Y/n] "
  159. } else {
  160. postFix = "[y/N] "
  161. }
  162. var response string
  163. fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
  164. n, err := fmt.Scanln(&response)
  165. if err != nil || n == 0 {
  166. return true
  167. }
  168. if response == string(def[0]) || response == string(def[1]) {
  169. return false
  170. }
  171. return true
  172. }