config.go 4.9 KB

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