config.go 4.9 KB

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