config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // 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.SudoLoop = false
  94. config.TarBin = "/usr/bin/bsdtar"
  95. config.TimeUpdate = false
  96. config.RequestSplitN = 150
  97. }
  98. // Editor returns the preferred system editor.
  99. func editor() string {
  100. switch {
  101. case config.Editor != "":
  102. editor, err := exec.LookPath(config.Editor)
  103. if err != nil {
  104. fmt.Println(err)
  105. } else {
  106. return editor
  107. }
  108. fallthrough
  109. case os.Getenv("EDITOR") != "":
  110. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  111. if err != nil {
  112. fmt.Println(err)
  113. } else {
  114. return editor
  115. }
  116. fallthrough
  117. case os.Getenv("VISUAL") != "":
  118. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  119. if err != nil {
  120. fmt.Println(err)
  121. } else {
  122. return editor
  123. }
  124. fallthrough
  125. default:
  126. fmt.Println(boldRedFgBlackBg("Warning:"),
  127. boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
  128. fmt.Println("Please add $EDITOR or to your environment variables.")
  129. editorLoop:
  130. fmt.Print(greenFg("Edit PKGBUILD with:"))
  131. var editorInput string
  132. _, err := fmt.Scanln(&editorInput)
  133. if err != nil {
  134. fmt.Println(err)
  135. goto editorLoop
  136. }
  137. editor, err := exec.LookPath(editorInput)
  138. if err != nil {
  139. fmt.Println(err)
  140. goto editorLoop
  141. }
  142. return editor
  143. }
  144. }
  145. // ContinueTask prompts if user wants to continue task.
  146. //If NoConfirm is set the action will continue without user input.
  147. func continueTask(s string, def string) (cont bool) {
  148. if config.NoConfirm {
  149. return true
  150. }
  151. var postFix string
  152. if def == "nN" {
  153. postFix = " [Y/n] "
  154. } else {
  155. postFix = " [y/N] "
  156. }
  157. var response string
  158. fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
  159. n, err := fmt.Scanln(&response)
  160. if err != nil || n == 0 {
  161. return true
  162. }
  163. if response == string(def[0]) || response == string(def[1]) {
  164. return false
  165. }
  166. return true
  167. }
  168. func (config Configuration) String() string {
  169. var buf bytes.Buffer
  170. enc := json.NewEncoder(&buf)
  171. enc.SetIndent("", "\t")
  172. if err := enc.Encode(config); err != nil {
  173. fmt.Println(err)
  174. }
  175. return buf.String()
  176. }