config.go 11 KB

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