config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. alpm "github.com/jguer/go-alpm"
  8. )
  9. // Verbosity settings for search
  10. const (
  11. NumberMenu = iota
  12. Detailed
  13. Minimal
  14. )
  15. // Describes Sorting method for numberdisplay
  16. const (
  17. BottomUp = iota
  18. TopDown
  19. )
  20. // Configuration stores yay's config.
  21. type Configuration struct {
  22. BuildDir string `json:"buildDir"`
  23. Editor string `json:"editor"`
  24. MakepkgBin string `json:"makepkgbin"`
  25. PacmanBin string `json:"pacmanbin"`
  26. PacmanConf string `json:"pacmanconf"`
  27. TarBin string `json:"tarbin"`
  28. RequestSplitN int `json:"requestsplitn"`
  29. SearchMode int `json:"-"`
  30. SortMode int `json:"sortmode"`
  31. TimeUpdate bool `json:"timeupdate"`
  32. NoConfirm bool `json:"noconfirm"`
  33. Devel bool `json:"devel"`
  34. CleanAfter bool `json:"cleanAfter"`
  35. }
  36. var version = "2.297"
  37. // baseURL givers the AUR default address.
  38. const baseURL string = "https://aur.archlinux.org"
  39. var savedInfo infos
  40. // configfile holds yay config file path.
  41. var configFile string
  42. // vcsfile holds yay vcs info file path.
  43. var vcsFile string
  44. //completion file
  45. var completionFile string
  46. // Updated returns if database has been updated
  47. var updated bool
  48. // changedConfig holds whether or not the config has changed
  49. var changedConfig 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. config.BuildDir = fmt.Sprintf("%s/.cache/yay/", os.Getenv("HOME"))
  85. config.CleanAfter = false
  86. config.Editor = ""
  87. config.Devel = false
  88. config.MakepkgBin = "/usr/bin/makepkg"
  89. config.NoConfirm = false
  90. config.PacmanBin = "/usr/bin/pacman"
  91. config.PacmanConf = "/etc/pacman.conf"
  92. config.SortMode = BottomUp
  93. config.TarBin = "/usr/bin/bsdtar"
  94. config.TimeUpdate = false
  95. config.RequestSplitN = 150
  96. }
  97. // Editor returns the preferred system editor.
  98. func editor() string {
  99. switch {
  100. case config.Editor != "":
  101. editor, err := exec.LookPath(config.Editor)
  102. if err != nil {
  103. fmt.Println(err)
  104. } else {
  105. return editor
  106. }
  107. fallthrough
  108. case os.Getenv("EDITOR") != "":
  109. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  110. if err != nil {
  111. fmt.Println(err)
  112. } else {
  113. return editor
  114. }
  115. fallthrough
  116. case os.Getenv("VISUAL") != "":
  117. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  118. if err != nil {
  119. fmt.Println(err)
  120. } else {
  121. return editor
  122. }
  123. fallthrough
  124. default:
  125. fmt.Println(boldRedFgBlackBg("Warning:"),
  126. boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
  127. fmt.Println("Please add $EDITOR or to your environment variables.")
  128. editorLoop:
  129. fmt.Print(greenFg("Edit PKGBUILD with:"))
  130. var editorInput string
  131. _, err := fmt.Scanln(&editorInput)
  132. if err != nil {
  133. fmt.Println(err)
  134. goto editorLoop
  135. }
  136. editor, err := exec.LookPath(editorInput)
  137. if err != nil {
  138. fmt.Println(err)
  139. goto editorLoop
  140. }
  141. return editor
  142. }
  143. }
  144. // ContinueTask prompts if user wants to continue task.
  145. //If NoConfirm is set the action will continue without user input.
  146. func continueTask(s string, def string) (cont bool) {
  147. if config.NoConfirm {
  148. return true
  149. }
  150. var postFix string
  151. if def == "nN" {
  152. postFix = "[Y/n] "
  153. } else {
  154. postFix = "[y/N] "
  155. }
  156. var response string
  157. fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
  158. n, err := fmt.Scanln(&response)
  159. if err != nil || n == 0 {
  160. return true
  161. }
  162. if response == string(def[0]) || response == string(def[1]) {
  163. return false
  164. }
  165. return true
  166. }