config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  12. )
  13. // Verbosity settings for search
  14. const (
  15. numberMenu = iota
  16. detailed
  17. minimal
  18. )
  19. const (
  20. // Describes Sorting method for numberdisplay
  21. bottomUp = iota
  22. topDown
  23. )
  24. const (
  25. modeAUR targetMode = iota
  26. modeRepo
  27. modeAny
  28. )
  29. type targetMode int
  30. // Configuration stores yay's config.
  31. type Configuration struct {
  32. AURURL string `json:"aururl"`
  33. BuildDir string `json:"buildDir"`
  34. ABSDir string `json:"absdir"`
  35. Editor string `json:"editor"`
  36. EditorFlags string `json:"editorflags"`
  37. MakepkgBin string `json:"makepkgbin"`
  38. MakepkgConf string `json:"makepkgconf"`
  39. PacmanBin string `json:"pacmanbin"`
  40. PacmanConf string `json:"pacmanconf"`
  41. TarBin string `json:"tarbin"`
  42. ReDownload string `json:"redownload"`
  43. ReBuild string `json:"rebuild"`
  44. BatchInstall bool `json:"batchinstall"`
  45. AnswerClean string `json:"answerclean"`
  46. AnswerDiff string `json:"answerdiff"`
  47. AnswerEdit string `json:"answeredit"`
  48. AnswerUpgrade string `json:"answerupgrade"`
  49. GitBin string `json:"gitbin"`
  50. GpgBin string `json:"gpgbin"`
  51. GpgFlags string `json:"gpgflags"`
  52. MFlags string `json:"mflags"`
  53. SortBy string `json:"sortby"`
  54. SearchBy string `json:"searchby"`
  55. GitFlags string `json:"gitflags"`
  56. RemoveMake string `json:"removemake"`
  57. RequestSplitN int `json:"requestsplitn"`
  58. SearchMode int `json:"-"`
  59. SortMode int `json:"sortmode"`
  60. CompletionInterval int `json:"completionrefreshtime"`
  61. SudoLoop bool `json:"sudoloop"`
  62. TimeUpdate bool `json:"timeupdate"`
  63. NoConfirm bool `json:"-"`
  64. Devel bool `json:"devel"`
  65. CleanAfter bool `json:"cleanAfter"`
  66. GitClone bool `json:"gitclone"`
  67. Provides bool `json:"provides"`
  68. PGPFetch bool `json:"pgpfetch"`
  69. UpgradeMenu bool `json:"upgrademenu"`
  70. CleanMenu bool `json:"cleanmenu"`
  71. DiffMenu bool `json:"diffmenu"`
  72. EditMenu bool `json:"editmenu"`
  73. CombinedUpgrade bool `json:"combinedupgrade"`
  74. UseAsk bool `json:"useask"`
  75. }
  76. var version = "9.3.1"
  77. // configFileName holds the name of the config file.
  78. const configFileName string = "config.json"
  79. // vcsFileName holds the name of the vcs file.
  80. const vcsFileName string = "vcs.json"
  81. // useColor enables/disables colored printing
  82. var useColor bool
  83. // configHome handles config directory home
  84. var configHome string
  85. // cacheHome handles cache home
  86. var cacheHome string
  87. // savedInfo holds the current vcs info
  88. var savedInfo vcsInfo
  89. // configfile holds yay config file path.
  90. var configFile string
  91. // vcsfile holds yay vcs info file path.
  92. var vcsFile string
  93. // shouldSaveConfig holds whether or not the config should be saved
  94. var shouldSaveConfig bool
  95. // YayConf holds the current config values for yay.
  96. var config *Configuration
  97. // AlpmConf holds the current config values for pacman.
  98. var pacmanConf *pacmanconf.Config
  99. // AlpmHandle is the alpm handle used by yay.
  100. var alpmHandle *alpm.Handle
  101. // Mode is used to restrict yay to AUR or repo only modes
  102. var mode = modeAny
  103. var hideMenus = false
  104. // SaveConfig writes yay config to file.
  105. func (config *Configuration) saveConfig() error {
  106. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  107. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  108. if err != nil {
  109. return err
  110. }
  111. defer in.Close()
  112. _, err = in.Write(marshalledinfo)
  113. if err != nil {
  114. return err
  115. }
  116. err = in.Sync()
  117. return err
  118. }
  119. func defaultSettings() *Configuration {
  120. config := &Configuration{
  121. AURURL: "https://aur.archlinux.org",
  122. BuildDir: "$HOME/.cache/yay",
  123. ABSDir: "$HOME/.cache/yay/abs",
  124. CleanAfter: false,
  125. Editor: "",
  126. EditorFlags: "",
  127. Devel: false,
  128. MakepkgBin: "makepkg",
  129. MakepkgConf: "",
  130. NoConfirm: false,
  131. PacmanBin: "pacman",
  132. PGPFetch: true,
  133. PacmanConf: "/etc/pacman.conf",
  134. GpgFlags: "",
  135. MFlags: "",
  136. GitFlags: "",
  137. SortMode: bottomUp,
  138. CompletionInterval: 7,
  139. SortBy: "votes",
  140. SearchBy: "name-desc",
  141. SudoLoop: false,
  142. TarBin: "bsdtar",
  143. GitBin: "git",
  144. GpgBin: "gpg",
  145. TimeUpdate: false,
  146. RequestSplitN: 150,
  147. ReDownload: "no",
  148. ReBuild: "no",
  149. BatchInstall: false,
  150. AnswerClean: "",
  151. AnswerDiff: "",
  152. AnswerEdit: "",
  153. AnswerUpgrade: "",
  154. RemoveMake: "ask",
  155. GitClone: true,
  156. Provides: true,
  157. UpgradeMenu: true,
  158. CleanMenu: true,
  159. DiffMenu: true,
  160. EditMenu: false,
  161. UseAsk: false,
  162. CombinedUpgrade: false,
  163. }
  164. if os.Getenv("XDG_CACHE_HOME") != "" {
  165. config.BuildDir = "$XDG_CACHE_HOME/yay"
  166. }
  167. return config
  168. }
  169. func (config *Configuration) expandEnv() {
  170. config.AURURL = os.ExpandEnv(config.AURURL)
  171. config.BuildDir = os.ExpandEnv(config.BuildDir)
  172. config.Editor = os.ExpandEnv(config.Editor)
  173. config.EditorFlags = os.ExpandEnv(config.EditorFlags)
  174. config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
  175. config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
  176. config.PacmanBin = os.ExpandEnv(config.PacmanBin)
  177. config.PacmanConf = os.ExpandEnv(config.PacmanConf)
  178. config.GpgFlags = os.ExpandEnv(config.GpgFlags)
  179. config.MFlags = os.ExpandEnv(config.MFlags)
  180. config.GitFlags = os.ExpandEnv(config.GitFlags)
  181. config.SortBy = os.ExpandEnv(config.SortBy)
  182. config.SearchBy = os.ExpandEnv(config.SearchBy)
  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.Fprintln(os.Stderr, 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.Fprintln(os.Stderr, 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.Fprintln(os.Stderr, err)
  219. } else {
  220. return editor, editorArgs[1:]
  221. }
  222. fallthrough
  223. default:
  224. fmt.Fprintln(os.Stderr)
  225. fmt.Fprintln(os.Stderr, bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  226. fmt.Fprintln(os.Stderr, 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.Fprintln(os.Stderr, err)
  232. continue
  233. }
  234. editorArgs := strings.Fields(editorInput)
  235. if len(editorArgs) == 0 {
  236. continue
  237. }
  238. editor, err := exec.LookPath(editorArgs[0])
  239. if err != nil {
  240. fmt.Fprintln(os.Stderr, err)
  241. continue
  242. }
  243. return editor, editorArgs[1:]
  244. }
  245. }
  246. }
  247. // ContinueTask prompts if user wants to continue task.
  248. //If NoConfirm is set the action will continue without user input.
  249. func continueTask(s string, cont bool) bool {
  250. if config.NoConfirm {
  251. return cont
  252. }
  253. var response string
  254. var postFix string
  255. yes := "yes"
  256. no := "no"
  257. y := string([]rune(yes)[0])
  258. n := string([]rune(no)[0])
  259. if cont {
  260. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  261. } else {
  262. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  263. }
  264. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  265. if _, err := fmt.Scanln(&response); err != nil {
  266. return cont
  267. }
  268. response = strings.ToLower(response)
  269. return response == yes || response == y
  270. }
  271. func getInput(defaultValue string) (string, error) {
  272. if defaultValue != "" || config.NoConfirm {
  273. fmt.Println(defaultValue)
  274. return defaultValue, nil
  275. }
  276. reader := bufio.NewReader(os.Stdin)
  277. buf, overflow, err := reader.ReadLine()
  278. if err != nil {
  279. return "", err
  280. }
  281. if overflow {
  282. return "", fmt.Errorf("Input too long")
  283. }
  284. return string(buf), nil
  285. }
  286. func (config Configuration) String() string {
  287. var buf bytes.Buffer
  288. enc := json.NewEncoder(&buf)
  289. enc.SetIndent("", "\t")
  290. if err := enc.Encode(config); err != nil {
  291. fmt.Fprintln(os.Stderr, err)
  292. }
  293. return buf.String()
  294. }
  295. func toUsage(usages []string) alpm.Usage {
  296. if len(usages) == 0 {
  297. return alpm.UsageAll
  298. }
  299. var ret alpm.Usage
  300. for _, usage := range usages {
  301. switch usage {
  302. case "Sync":
  303. ret |= alpm.UsageSync
  304. case "Search":
  305. ret |= alpm.UsageSearch
  306. case "Install":
  307. ret |= alpm.UsageInstall
  308. case "Upgrade":
  309. ret |= alpm.UsageUpgrade
  310. case "All":
  311. ret |= alpm.UsageAll
  312. }
  313. }
  314. return ret
  315. }
  316. func configureAlpm(conf *pacmanconf.Config) error {
  317. // TODO: set SigLevel
  318. //sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
  319. //localFileSigLevel := alpm.SigUseDefault
  320. //remoteFileSigLevel := alpm.SigUseDefault
  321. for _, repo := range pacmanConf.Repos {
  322. // TODO: set SigLevel
  323. db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
  324. if err != nil {
  325. return err
  326. }
  327. db.SetServers(repo.Servers)
  328. db.SetUsage(toUsage(repo.Usage))
  329. }
  330. if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
  331. return err
  332. }
  333. // add hook directories 1-by-1 to avoid overwriting the system directory
  334. for _, dir := range pacmanConf.HookDir {
  335. if err := alpmHandle.AddHookDir(dir); err != nil {
  336. return err
  337. }
  338. }
  339. if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
  340. return err
  341. }
  342. if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
  343. return err
  344. }
  345. if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
  346. return err
  347. }
  348. if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
  349. return err
  350. }
  351. if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
  352. return err
  353. }
  354. if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
  355. return err
  356. }
  357. if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
  358. return err
  359. }
  360. /*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
  361. return err
  362. }
  363. if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
  364. return err
  365. }
  366. if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
  367. return err
  368. }*/
  369. if err := alpmHandle.SetDeltaRatio(pacmanConf.UseDelta); err != nil {
  370. return err
  371. }
  372. if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
  373. return err
  374. }
  375. return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
  376. }