config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. alpm "github.com/jguer/go-alpm"
  11. )
  12. // Verbosity settings for search
  13. const (
  14. NumberMenu = iota
  15. Detailed
  16. Minimal
  17. )
  18. // Describes Sorting method for numberdisplay
  19. const (
  20. BottomUp = iota
  21. TopDown
  22. )
  23. type targetMode int
  24. const (
  25. ModeAUR targetMode = iota
  26. ModeRepo
  27. ModeAny
  28. )
  29. // Configuration stores yay's config.
  30. type Configuration struct {
  31. BuildDir string `json:"buildDir"`
  32. Editor string `json:"editor"`
  33. EditorFlags string `json:"editorflags"`
  34. MakepkgBin string `json:"makepkgbin"`
  35. PacmanBin string `json:"pacmanbin"`
  36. PacmanConf string `json:"pacmanconf"`
  37. TarBin string `json:"tarbin"`
  38. ReDownload string `json:"redownload"`
  39. ReBuild string `json:"rebuild"`
  40. AnswerClean string `json:"answerclean"`
  41. AnswerEdit string `json:"answeredit"`
  42. AnswerUpgrade string `json:"answerupgrade"`
  43. GitBin string `json:"gitbin"`
  44. GpgBin string `json:"gpgbin"`
  45. GpgFlags string `json:"gpgflags"`
  46. MFlags string `json:"mflags"`
  47. SortBy string `json:"sortby"`
  48. GitFlags string `json:"gitflags"`
  49. RequestSplitN int `json:"requestsplitn"`
  50. SearchMode int `json:"-"`
  51. SortMode int `json:"sortmode"`
  52. SudoLoop bool `json:"sudoloop"`
  53. TimeUpdate bool `json:"timeupdate"`
  54. NoConfirm bool `json:"-"`
  55. Devel bool `json:"devel"`
  56. CleanAfter bool `json:"cleanAfter"`
  57. GitClone bool `json:"gitclone"`
  58. Provides bool `json:"provides"`
  59. PGPFetch bool `json:"pgpfetch"`
  60. ShowDiffs bool `json:"showdifs"`
  61. }
  62. var version = "5.688"
  63. // configFileName holds the name of the config file.
  64. const configFileName string = "config.json"
  65. // vcsFileName holds the name of the vcs file.
  66. const vcsFileName string = "vcs.json"
  67. // baseURL givers the AUR default address.
  68. const baseURL string = "https://aur.archlinux.org"
  69. // useColor enables/disables colored printing
  70. var useColor bool
  71. // configHome handles config directory home
  72. var configHome string
  73. // cacheHome handles cache home
  74. var cacheHome string
  75. // savedInfo holds the current vcs info
  76. var savedInfo vcsInfo
  77. // configfile holds yay config file path.
  78. var configFile string
  79. // vcsfile holds yay vcs info file path.
  80. var vcsFile string
  81. // shouldSaveConfig holds whether or not the config should be saved
  82. var shouldSaveConfig bool
  83. // YayConf holds the current config values for yay.
  84. var config Configuration
  85. // AlpmConf holds the current config values for pacman.
  86. var alpmConf alpm.PacmanConfig
  87. // AlpmHandle is the alpm handle used by yay.
  88. var alpmHandle *alpm.Handle
  89. // Mode is used to restrict yay to AUR or repo only modes
  90. var mode targetMode = ModeAny
  91. func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
  92. file, err := os.Open(pacmanconf)
  93. if err != nil {
  94. return
  95. }
  96. conf, err = alpm.ParseConfig(file)
  97. if err != nil {
  98. return
  99. }
  100. return
  101. }
  102. // SaveConfig writes yay config to file.
  103. func (config *Configuration) saveConfig() error {
  104. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  105. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  106. if err != nil {
  107. return err
  108. }
  109. defer in.Close()
  110. _, err = in.Write(marshalledinfo)
  111. if err != nil {
  112. return err
  113. }
  114. err = in.Sync()
  115. return err
  116. }
  117. func defaultSettings(config *Configuration) {
  118. config.BuildDir = cacheHome
  119. config.CleanAfter = false
  120. config.Editor = ""
  121. config.EditorFlags = ""
  122. config.Devel = false
  123. config.MakepkgBin = "makepkg"
  124. config.NoConfirm = false
  125. config.PacmanBin = "pacman"
  126. config.PGPFetch = true
  127. config.PacmanConf = "/etc/pacman.conf"
  128. config.GpgFlags = ""
  129. config.MFlags = ""
  130. config.GitFlags = ""
  131. config.SortMode = BottomUp
  132. config.SortBy = "votes"
  133. config.SudoLoop = false
  134. config.TarBin = "bsdtar"
  135. config.GitBin = "git"
  136. config.GpgBin = "gpg"
  137. config.TimeUpdate = false
  138. config.RequestSplitN = 150
  139. config.ReDownload = "no"
  140. config.ReBuild = "no"
  141. config.AnswerClean = ""
  142. config.AnswerEdit = ""
  143. config.AnswerUpgrade = ""
  144. config.GitClone = true
  145. config.Provides = true
  146. config.ShowDiffs = true
  147. }
  148. // Editor returns the preferred system editor.
  149. func editor() (string, []string) {
  150. switch {
  151. case config.Editor != "":
  152. editor, err := exec.LookPath(config.Editor)
  153. if err != nil {
  154. fmt.Println(err)
  155. } else {
  156. return editor, strings.Fields(config.EditorFlags)
  157. }
  158. fallthrough
  159. case os.Getenv("EDITOR") != "":
  160. editorArgs := strings.Fields(os.Getenv("EDITOR"))
  161. editor, err := exec.LookPath(editorArgs[0])
  162. if err != nil {
  163. fmt.Println(err)
  164. } else {
  165. return editor, editorArgs[1:]
  166. }
  167. fallthrough
  168. case os.Getenv("VISUAL") != "":
  169. editorArgs := strings.Fields(os.Getenv("VISUAL"))
  170. editor, err := exec.LookPath(editorArgs[0])
  171. if err != nil {
  172. fmt.Println(err)
  173. } else {
  174. return editor, editorArgs[1:]
  175. }
  176. fallthrough
  177. default:
  178. fmt.Println()
  179. fmt.Println(bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  180. fmt.Println(bold(red(arrow)) + bold(" Please add ") + bold(cyan("$EDITOR")) + bold(" or ") + bold(cyan("$VISUAL")) + bold(" to your environment variables."))
  181. for {
  182. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  183. editorInput, err := getInput("")
  184. if err != nil {
  185. fmt.Println(err)
  186. continue
  187. }
  188. editorArgs := strings.Fields(editorInput)
  189. editor, err := exec.LookPath(editorArgs[0])
  190. if err != nil {
  191. fmt.Println(err)
  192. continue
  193. }
  194. return editor, editorArgs[1:]
  195. }
  196. }
  197. }
  198. // ContinueTask prompts if user wants to continue task.
  199. //If NoConfirm is set the action will continue without user input.
  200. func continueTask(s string, def string) (cont bool) {
  201. if config.NoConfirm {
  202. return true
  203. }
  204. var postFix string
  205. if def == "nN" {
  206. postFix = " [Y/n] "
  207. } else {
  208. postFix = " [y/N] "
  209. }
  210. var response string
  211. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  212. n, err := fmt.Scanln(&response)
  213. if err != nil || n == 0 {
  214. return true
  215. }
  216. if response == string(def[0]) || response == string(def[1]) {
  217. return false
  218. }
  219. return true
  220. }
  221. func getInput(defaultValue string) (string, error) {
  222. if defaultValue != "" || config.NoConfirm {
  223. fmt.Println(defaultValue)
  224. return defaultValue, nil
  225. }
  226. reader := bufio.NewReader(os.Stdin)
  227. buf, overflow, err := reader.ReadLine()
  228. if err != nil {
  229. return "", err
  230. }
  231. if overflow {
  232. return "", fmt.Errorf("Input too long")
  233. }
  234. return string(buf), nil
  235. }
  236. func (config Configuration) String() string {
  237. var buf bytes.Buffer
  238. enc := json.NewEncoder(&buf)
  239. enc.SetIndent("", "\t")
  240. if err := enc.Encode(config); err != nil {
  241. fmt.Println(err)
  242. }
  243. return buf.String()
  244. }