config.go 4.4 KB

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