config.go 11 KB

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