config.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. // configFileName holds the name of the config file.
  40. const configFileName string = "config.json"
  41. // vcsFileName holds the name of the vcs file.
  42. const vcsFileName string = "vcs.json"
  43. // completionFilePrefix holds the prefix used for storing shell completion files.
  44. const completionFilePrefix string = "aur_"
  45. // baseURL givers the AUR default address.
  46. const baseURL string = "https://aur.archlinux.org"
  47. // configHome handles config directory home
  48. var configHome string
  49. // cacheHome handles cache home
  50. var cacheHome string
  51. // savedInfo holds the current vcs info
  52. var savedInfo vcsInfo
  53. // configfile holds yay config file path.
  54. var configFile string
  55. // vcsfile holds yay vcs info file path.
  56. var vcsFile string
  57. // completion file
  58. var completionFile string
  59. // changedConfig holds whether or not the config has changed
  60. var changedConfig bool
  61. // YayConf holds the current config values for yay.
  62. var config Configuration
  63. // AlpmConf holds the current config values for pacman.
  64. var alpmConf alpm.PacmanConfig
  65. // AlpmHandle is the alpm handle used by yay.
  66. var alpmHandle *alpm.Handle
  67. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  68. file, err := os.Open(pacmanconf)
  69. if err != nil {
  70. return
  71. }
  72. conf, err = alpm.ParseConfig(file)
  73. if err != nil {
  74. return
  75. }
  76. return
  77. }
  78. // SaveConfig writes yay config to file.
  79. func (config *Configuration) saveConfig() error {
  80. config.NoConfirm = false
  81. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  82. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  83. if err != nil {
  84. return err
  85. }
  86. defer in.Close()
  87. _, err = in.Write(marshalledinfo)
  88. if err != nil {
  89. return err
  90. }
  91. err = in.Sync()
  92. return err
  93. }
  94. func defaultSettings(config *Configuration) {
  95. config.BuildDir = fmt.Sprintf("%s/.cache/yay/", os.Getenv("HOME"))
  96. config.CleanAfter = false
  97. config.Editor = ""
  98. config.Devel = false
  99. config.MakepkgBin = "/usr/bin/makepkg"
  100. config.NoConfirm = false
  101. config.PacmanBin = "/usr/bin/pacman"
  102. config.PacmanConf = "/etc/pacman.conf"
  103. config.SortMode = BottomUp
  104. config.SudoLoop = false
  105. config.TarBin = "/usr/bin/bsdtar"
  106. config.TimeUpdate = false
  107. config.RequestSplitN = 150
  108. }
  109. // Editor returns the preferred system editor.
  110. func editor() string {
  111. switch {
  112. case config.Editor != "":
  113. editor, err := exec.LookPath(config.Editor)
  114. if err != nil {
  115. fmt.Println(err)
  116. } else {
  117. return editor
  118. }
  119. fallthrough
  120. case os.Getenv("EDITOR") != "":
  121. editor, err := exec.LookPath(os.Getenv("EDITOR"))
  122. if err != nil {
  123. fmt.Println(err)
  124. } else {
  125. return editor
  126. }
  127. fallthrough
  128. case os.Getenv("VISUAL") != "":
  129. editor, err := exec.LookPath(os.Getenv("VISUAL"))
  130. if err != nil {
  131. fmt.Println(err)
  132. } else {
  133. return editor
  134. }
  135. fallthrough
  136. default:
  137. fmt.Println(bold(red("Warning:")),
  138. bold(magenta("$EDITOR")), "is not set")
  139. fmt.Println("Please add $EDITOR or to your environment variables.")
  140. editorLoop:
  141. fmt.Print(green("Edit PKGBUILD with:"))
  142. var editorInput string
  143. _, err := fmt.Scanln(&editorInput)
  144. if err != nil {
  145. fmt.Println(err)
  146. goto editorLoop
  147. }
  148. editor, err := exec.LookPath(editorInput)
  149. if err != nil {
  150. fmt.Println(err)
  151. goto editorLoop
  152. }
  153. return editor
  154. }
  155. }
  156. // ContinueTask prompts if user wants to continue task.
  157. //If NoConfirm is set the action will continue without user input.
  158. func continueTask(s string, def string) (cont bool) {
  159. if config.NoConfirm {
  160. return true
  161. }
  162. var postFix string
  163. if def == "nN" {
  164. postFix = " [Y/n] "
  165. } else {
  166. postFix = " [y/N] "
  167. }
  168. var response string
  169. fmt.Print(bold(green(arrow+" "+s+" ")), bold(postFix))
  170. n, err := fmt.Scanln(&response)
  171. if err != nil || n == 0 {
  172. return true
  173. }
  174. if response == string(def[0]) || response == string(def[1]) {
  175. return false
  176. }
  177. return true
  178. }
  179. func (config Configuration) String() string {
  180. var buf bytes.Buffer
  181. enc := json.NewEncoder(&buf)
  182. enc.SetIndent("", "\t")
  183. if err := enc.Encode(config); err != nil {
  184. fmt.Println(err)
  185. }
  186. return buf.String()
  187. }