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. )
  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. ReDownload string `json:"redownload"`
  42. ReBuild string `json:"rebuild"`
  43. AnswerClean string `json:"answerclean"`
  44. AnswerDiff string `json:"answerdiff"`
  45. AnswerEdit string `json:"answeredit"`
  46. AnswerUpgrade string `json:"answerupgrade"`
  47. GitBin string `json:"gitbin"`
  48. GpgBin string `json:"gpgbin"`
  49. GpgFlags string `json:"gpgflags"`
  50. MFlags string `json:"mflags"`
  51. SortBy string `json:"sortby"`
  52. SearchBy string `json:"searchby"`
  53. GitFlags string `json:"gitflags"`
  54. RemoveMake string `json:"removemake"`
  55. SudoBin string `json:"sudobin"`
  56. SudoFlags string `json:"sudoflags"`
  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. Provides bool `json:"provides"`
  67. PGPFetch bool `json:"pgpfetch"`
  68. UpgradeMenu bool `json:"upgrademenu"`
  69. CleanMenu bool `json:"cleanmenu"`
  70. DiffMenu bool `json:"diffmenu"`
  71. EditMenu bool `json:"editmenu"`
  72. CombinedUpgrade bool `json:"combinedupgrade"`
  73. UseAsk bool `json:"useask"`
  74. BatchInstall bool `json:"batchinstall"`
  75. }
  76. var yayVersion = "9.4.3"
  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, err := json.MarshalIndent(config, "", "\t")
  107. if err != nil {
  108. return err
  109. }
  110. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  111. if err != nil {
  112. return err
  113. }
  114. defer in.Close()
  115. if _, err = in.Write(marshalledinfo); err != nil {
  116. return err
  117. }
  118. return in.Sync()
  119. }
  120. func defaultSettings() *Configuration {
  121. newConfig := &Configuration{
  122. AURURL: "https://aur.archlinux.org",
  123. BuildDir: "$HOME/.cache/yay",
  124. ABSDir: "$HOME/.cache/yay/abs",
  125. CleanAfter: false,
  126. Editor: "",
  127. EditorFlags: "",
  128. Devel: false,
  129. MakepkgBin: "makepkg",
  130. MakepkgConf: "",
  131. NoConfirm: false,
  132. PacmanBin: "pacman",
  133. PGPFetch: true,
  134. PacmanConf: "/etc/pacman.conf",
  135. GpgFlags: "",
  136. MFlags: "",
  137. GitFlags: "",
  138. SortMode: bottomUp,
  139. CompletionInterval: 7,
  140. SortBy: "votes",
  141. SearchBy: "name-desc",
  142. SudoLoop: false,
  143. GitBin: "git",
  144. GpgBin: "gpg",
  145. SudoBin: "sudo",
  146. SudoFlags: "",
  147. TimeUpdate: false,
  148. RequestSplitN: 150,
  149. ReDownload: "no",
  150. ReBuild: "no",
  151. BatchInstall: false,
  152. AnswerClean: "",
  153. AnswerDiff: "",
  154. AnswerEdit: "",
  155. AnswerUpgrade: "",
  156. RemoveMake: "ask",
  157. Provides: true,
  158. UpgradeMenu: true,
  159. CleanMenu: true,
  160. DiffMenu: true,
  161. EditMenu: false,
  162. UseAsk: false,
  163. CombinedUpgrade: false,
  164. }
  165. if os.Getenv("XDG_CACHE_HOME") != "" {
  166. config.BuildDir = "$XDG_CACHE_HOME/yay"
  167. }
  168. return newConfig
  169. }
  170. func (config *Configuration) expandEnv() {
  171. config.AURURL = os.ExpandEnv(config.AURURL)
  172. config.ABSDir = os.ExpandEnv(config.ABSDir)
  173. config.BuildDir = os.ExpandEnv(config.BuildDir)
  174. config.Editor = os.ExpandEnv(config.Editor)
  175. config.EditorFlags = os.ExpandEnv(config.EditorFlags)
  176. config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
  177. config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
  178. config.PacmanBin = os.ExpandEnv(config.PacmanBin)
  179. config.PacmanConf = os.ExpandEnv(config.PacmanConf)
  180. config.GpgFlags = os.ExpandEnv(config.GpgFlags)
  181. config.MFlags = os.ExpandEnv(config.MFlags)
  182. config.GitFlags = os.ExpandEnv(config.GitFlags)
  183. config.SortBy = os.ExpandEnv(config.SortBy)
  184. config.SearchBy = os.ExpandEnv(config.SearchBy)
  185. config.GitBin = os.ExpandEnv(config.GitBin)
  186. config.GpgBin = os.ExpandEnv(config.GpgBin)
  187. config.SudoBin = os.ExpandEnv(config.SudoBin)
  188. config.SudoFlags = os.ExpandEnv(config.SudoFlags)
  189. config.ReDownload = os.ExpandEnv(config.ReDownload)
  190. config.ReBuild = os.ExpandEnv(config.ReBuild)
  191. config.AnswerClean = os.ExpandEnv(config.AnswerClean)
  192. config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
  193. config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
  194. config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
  195. config.RemoveMake = os.ExpandEnv(config.RemoveMake)
  196. }
  197. // Editor returns the preferred system editor.
  198. func editor() (editor string, args []string) {
  199. switch {
  200. case config.Editor != "":
  201. editor, err := exec.LookPath(config.Editor)
  202. if err != nil {
  203. fmt.Fprintln(os.Stderr, err)
  204. } else {
  205. return editor, strings.Fields(config.EditorFlags)
  206. }
  207. fallthrough
  208. case os.Getenv("EDITOR") != "":
  209. if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
  210. editor, err := exec.LookPath(editorArgs[0])
  211. if err != nil {
  212. fmt.Fprintln(os.Stderr, err)
  213. } else {
  214. return editor, editorArgs[1:]
  215. }
  216. }
  217. fallthrough
  218. case os.Getenv("VISUAL") != "":
  219. if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
  220. editor, err := exec.LookPath(editorArgs[0])
  221. if err != nil {
  222. fmt.Fprintln(os.Stderr, err)
  223. } else {
  224. return editor, editorArgs[1:]
  225. }
  226. }
  227. fallthrough
  228. default:
  229. fmt.Fprintln(os.Stderr)
  230. fmt.Fprintln(os.Stderr, bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  231. fmt.Fprintln(os.Stderr,
  232. bold(red(arrow))+
  233. bold(" Please add ")+
  234. bold(cyan("$EDITOR"))+
  235. bold(" or ")+
  236. bold(cyan("$VISUAL"))+
  237. bold(" to your environment variables."))
  238. for {
  239. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  240. editorInput, err := getInput("")
  241. if err != nil {
  242. fmt.Fprintln(os.Stderr, err)
  243. continue
  244. }
  245. editorArgs := strings.Fields(editorInput)
  246. if len(editorArgs) == 0 {
  247. continue
  248. }
  249. editor, err := exec.LookPath(editorArgs[0])
  250. if err != nil {
  251. fmt.Fprintln(os.Stderr, err)
  252. continue
  253. }
  254. return editor, editorArgs[1:]
  255. }
  256. }
  257. }
  258. // ContinueTask prompts if user wants to continue task.
  259. // If NoConfirm is set the action will continue without user input.
  260. func continueTask(s string, cont bool) bool {
  261. if config.NoConfirm {
  262. return cont
  263. }
  264. var response string
  265. var postFix string
  266. yes := "yes"
  267. no := "no"
  268. y := string([]rune(yes)[0])
  269. n := string([]rune(no)[0])
  270. if cont {
  271. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  272. } else {
  273. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  274. }
  275. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  276. if _, err := fmt.Scanln(&response); err != nil {
  277. return cont
  278. }
  279. response = strings.ToLower(response)
  280. return response == yes || response == y
  281. }
  282. func getInput(defaultValue string) (string, error) {
  283. if defaultValue != "" || config.NoConfirm {
  284. fmt.Println(defaultValue)
  285. return defaultValue, nil
  286. }
  287. reader := bufio.NewReader(os.Stdin)
  288. buf, overflow, err := reader.ReadLine()
  289. if err != nil {
  290. return "", err
  291. }
  292. if overflow {
  293. return "", fmt.Errorf("input too long")
  294. }
  295. return string(buf), nil
  296. }
  297. func (config *Configuration) String() string {
  298. var buf bytes.Buffer
  299. enc := json.NewEncoder(&buf)
  300. enc.SetIndent("", "\t")
  301. if err := enc.Encode(config); err != nil {
  302. fmt.Fprintln(os.Stderr, err)
  303. }
  304. return buf.String()
  305. }
  306. func toUsage(usages []string) alpm.Usage {
  307. if len(usages) == 0 {
  308. return alpm.UsageAll
  309. }
  310. var ret alpm.Usage
  311. for _, usage := range usages {
  312. switch usage {
  313. case "Sync":
  314. ret |= alpm.UsageSync
  315. case "Search":
  316. ret |= alpm.UsageSearch
  317. case "Install":
  318. ret |= alpm.UsageInstall
  319. case "Upgrade":
  320. ret |= alpm.UsageUpgrade
  321. case "All":
  322. ret |= alpm.UsageAll
  323. }
  324. }
  325. return ret
  326. }
  327. func configureAlpm() error {
  328. // TODO: set SigLevel
  329. // sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
  330. // localFileSigLevel := alpm.SigUseDefault
  331. // remoteFileSigLevel := alpm.SigUseDefault
  332. for _, repo := range pacmanConf.Repos {
  333. // TODO: set SigLevel
  334. db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
  335. if err != nil {
  336. return err
  337. }
  338. db.SetServers(repo.Servers)
  339. db.SetUsage(toUsage(repo.Usage))
  340. }
  341. if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
  342. return err
  343. }
  344. // add hook directories 1-by-1 to avoid overwriting the system directory
  345. for _, dir := range pacmanConf.HookDir {
  346. if err := alpmHandle.AddHookDir(dir); err != nil {
  347. return err
  348. }
  349. }
  350. if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
  351. return err
  352. }
  353. if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
  354. return err
  355. }
  356. if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
  357. return err
  358. }
  359. if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
  360. return err
  361. }
  362. if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
  363. return err
  364. }
  365. if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
  366. return err
  367. }
  368. if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
  369. return err
  370. }
  371. /*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
  372. return err
  373. }
  374. if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
  375. return err
  376. }
  377. if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
  378. return err
  379. }*/
  380. if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
  381. return err
  382. }
  383. return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
  384. }