config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "os/exec"
  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. SudoLoop bool `json:"sudoloop"`
  33. TimeUpdate bool `json:"timeupdate"`
  34. NoConfirm bool `json:"-"`
  35. Devel bool `json:"devel"`
  36. CleanAfter bool `json:"cleanAfter"`
  37. }
  38. var version = "3.373"
  39. // baseURL givers the AUR default address.
  40. const baseURL string = "https://aur.archlinux.org"
  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. config.BuildDir = fmt.Sprintf("%s/.cache/yay/", os.Getenv("HOME"))
  87. config.CleanAfter = false
  88. config.Editor = ""
  89. config.Devel = false
  90. config.MakepkgBin = "/usr/bin/makepkg"
  91. config.NoConfirm = false
  92. config.PacmanBin = "/usr/bin/pacman"
  93. config.PacmanConf = "/etc/pacman.conf"
  94. config.SortMode = BottomUp
  95. config.SudoLoop = false
  96. config.TarBin = "/usr/bin/bsdtar"
  97. config.TimeUpdate = false
  98. config.RequestSplitN = 150
  99. }
  100. // Editor returns the preferred system editor.
  101. func editor() string {
  102. switch {
  103. case config.Editor != "":
  104. editor, err := exec.LookPath(config.Editor)
  105. if err != nil {
  106. fmt.Println(err)
  107. } else {
  108. return editor
  109. }
  110. fallthrough
  111. case os.Getenv("EDITOR") != "":
  112. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  113. if err != nil {
  114. fmt.Println(err)
  115. } else {
  116. return editor
  117. }
  118. fallthrough
  119. case os.Getenv("VISUAL") != "":
  120. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  121. if err != nil {
  122. fmt.Println(err)
  123. } else {
  124. return editor
  125. }
  126. fallthrough
  127. default:
  128. fmt.Println(boldRedFgBlackBg("Warning:"),
  129. boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
  130. fmt.Println("Please add $EDITOR or to your environment variables.")
  131. editorLoop:
  132. fmt.Print(greenFg("Edit PKGBUILD with:"))
  133. var editorInput string
  134. _, err := fmt.Scanln(&editorInput)
  135. if err != nil {
  136. fmt.Println(err)
  137. goto editorLoop
  138. }
  139. editor, err := exec.LookPath(editorInput)
  140. if err != nil {
  141. fmt.Println(err)
  142. goto editorLoop
  143. }
  144. return editor
  145. }
  146. }
  147. // ContinueTask prompts if user wants to continue task.
  148. //If NoConfirm is set the action will continue without user input.
  149. func continueTask(s string, def string) (cont bool) {
  150. if config.NoConfirm {
  151. return true
  152. }
  153. var postFix string
  154. if def == "nN" {
  155. postFix = " [Y/n] "
  156. } else {
  157. postFix = " [y/N] "
  158. }
  159. var response string
  160. fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
  161. n, err := fmt.Scanln(&response)
  162. if err != nil || n == 0 {
  163. return true
  164. }
  165. if response == string(def[0]) || response == string(def[1]) {
  166. return false
  167. }
  168. return true
  169. }
  170. func (config Configuration) String() string {
  171. var buf bytes.Buffer
  172. enc := json.NewEncoder(&buf)
  173. enc.SetIndent("", "\t")
  174. if err := enc.Encode(config); err != nil {
  175. fmt.Println(err)
  176. }
  177. return buf.String()
  178. }