config.go 6.9 KB

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