config.go 8.6 KB

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