config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. BatchInstall bool `json:"batchinstall"`
  44. AnswerClean string `json:"answerclean"`
  45. AnswerDiff string `json:"answerdiff"`
  46. AnswerEdit string `json:"answeredit"`
  47. AnswerUpgrade string `json:"answerupgrade"`
  48. GitBin string `json:"gitbin"`
  49. GpgBin string `json:"gpgbin"`
  50. GpgFlags string `json:"gpgflags"`
  51. MFlags string `json:"mflags"`
  52. SortBy string `json:"sortby"`
  53. SearchBy string `json:"searchby"`
  54. GitFlags string `json:"gitflags"`
  55. RemoveMake string `json:"removemake"`
  56. SudoBin string `json:"sudobin"`
  57. SudoFlags string `json:"sudoflags"`
  58. RequestSplitN int `json:"requestsplitn"`
  59. SearchMode int `json:"-"`
  60. SortMode int `json:"sortmode"`
  61. CompletionInterval int `json:"completionrefreshtime"`
  62. SudoLoop bool `json:"sudoloop"`
  63. TimeUpdate bool `json:"timeupdate"`
  64. NoConfirm bool `json:"-"`
  65. Devel bool `json:"devel"`
  66. CleanAfter bool `json:"cleanAfter"`
  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.4.2"
  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. GitBin: "git",
  143. GpgBin: "gpg",
  144. SudoBin: "sudo",
  145. SudoFlags: "",
  146. TimeUpdate: false,
  147. RequestSplitN: 150,
  148. ReDownload: "no",
  149. ReBuild: "no",
  150. BatchInstall: false,
  151. AnswerClean: "",
  152. AnswerDiff: "",
  153. AnswerEdit: "",
  154. AnswerUpgrade: "",
  155. RemoveMake: "ask",
  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.ABSDir = os.ExpandEnv(config.ABSDir)
  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.SearchBy = os.ExpandEnv(config.SearchBy)
  184. config.GitBin = os.ExpandEnv(config.GitBin)
  185. config.GpgBin = os.ExpandEnv(config.GpgBin)
  186. config.SudoBin = os.ExpandEnv(config.SudoBin)
  187. config.SudoFlags = os.ExpandEnv(config.SudoFlags)
  188. config.ReDownload = os.ExpandEnv(config.ReDownload)
  189. config.ReBuild = os.ExpandEnv(config.ReBuild)
  190. config.AnswerClean = os.ExpandEnv(config.AnswerClean)
  191. config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
  192. config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
  193. config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
  194. config.RemoveMake = os.ExpandEnv(config.RemoveMake)
  195. }
  196. // Editor returns the preferred system editor.
  197. func editor() (string, []string) {
  198. switch {
  199. case config.Editor != "":
  200. editor, err := exec.LookPath(config.Editor)
  201. if err != nil {
  202. fmt.Fprintln(os.Stderr, err)
  203. } else {
  204. return editor, strings.Fields(config.EditorFlags)
  205. }
  206. fallthrough
  207. case os.Getenv("EDITOR") != "":
  208. editorArgs := strings.Fields(os.Getenv("EDITOR"))
  209. editor, err := exec.LookPath(editorArgs[0])
  210. if err != nil {
  211. fmt.Fprintln(os.Stderr, err)
  212. } else {
  213. return editor, editorArgs[1:]
  214. }
  215. fallthrough
  216. case os.Getenv("VISUAL") != "":
  217. editorArgs := strings.Fields(os.Getenv("VISUAL"))
  218. editor, err := exec.LookPath(editorArgs[0])
  219. if err != nil {
  220. fmt.Fprintln(os.Stderr, err)
  221. } else {
  222. return editor, editorArgs[1:]
  223. }
  224. fallthrough
  225. default:
  226. fmt.Fprintln(os.Stderr)
  227. fmt.Fprintln(os.Stderr, bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  228. fmt.Fprintln(os.Stderr, bold(red(arrow))+bold(" Please add ")+bold(cyan("$EDITOR"))+bold(" or ")+bold(cyan("$VISUAL"))+bold(" to your environment variables."))
  229. for {
  230. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  231. editorInput, err := getInput("")
  232. if err != nil {
  233. fmt.Fprintln(os.Stderr, err)
  234. continue
  235. }
  236. editorArgs := strings.Fields(editorInput)
  237. if len(editorArgs) == 0 {
  238. continue
  239. }
  240. editor, err := exec.LookPath(editorArgs[0])
  241. if err != nil {
  242. fmt.Fprintln(os.Stderr, err)
  243. continue
  244. }
  245. return editor, editorArgs[1:]
  246. }
  247. }
  248. }
  249. // ContinueTask prompts if user wants to continue task.
  250. //If NoConfirm is set the action will continue without user input.
  251. func continueTask(s string, cont bool) bool {
  252. if config.NoConfirm {
  253. return cont
  254. }
  255. var response string
  256. var postFix string
  257. yes := "yes"
  258. no := "no"
  259. y := string([]rune(yes)[0])
  260. n := string([]rune(no)[0])
  261. if cont {
  262. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  263. } else {
  264. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  265. }
  266. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  267. if _, err := fmt.Scanln(&response); err != nil {
  268. return cont
  269. }
  270. response = strings.ToLower(response)
  271. return response == yes || response == y
  272. }
  273. func getInput(defaultValue string) (string, error) {
  274. if defaultValue != "" || config.NoConfirm {
  275. fmt.Println(defaultValue)
  276. return defaultValue, nil
  277. }
  278. reader := bufio.NewReader(os.Stdin)
  279. buf, overflow, err := reader.ReadLine()
  280. if err != nil {
  281. return "", err
  282. }
  283. if overflow {
  284. return "", fmt.Errorf("Input too long")
  285. }
  286. return string(buf), nil
  287. }
  288. func (config Configuration) String() string {
  289. var buf bytes.Buffer
  290. enc := json.NewEncoder(&buf)
  291. enc.SetIndent("", "\t")
  292. if err := enc.Encode(config); err != nil {
  293. fmt.Fprintln(os.Stderr, err)
  294. }
  295. return buf.String()
  296. }
  297. func toUsage(usages []string) alpm.Usage {
  298. if len(usages) == 0 {
  299. return alpm.UsageAll
  300. }
  301. var ret alpm.Usage
  302. for _, usage := range usages {
  303. switch usage {
  304. case "Sync":
  305. ret |= alpm.UsageSync
  306. case "Search":
  307. ret |= alpm.UsageSearch
  308. case "Install":
  309. ret |= alpm.UsageInstall
  310. case "Upgrade":
  311. ret |= alpm.UsageUpgrade
  312. case "All":
  313. ret |= alpm.UsageAll
  314. }
  315. }
  316. return ret
  317. }
  318. func configureAlpm(conf *pacmanconf.Config) error {
  319. // TODO: set SigLevel
  320. //sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
  321. //localFileSigLevel := alpm.SigUseDefault
  322. //remoteFileSigLevel := alpm.SigUseDefault
  323. for _, repo := range pacmanConf.Repos {
  324. // TODO: set SigLevel
  325. db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
  326. if err != nil {
  327. return err
  328. }
  329. db.SetServers(repo.Servers)
  330. db.SetUsage(toUsage(repo.Usage))
  331. }
  332. if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
  333. return err
  334. }
  335. // add hook directories 1-by-1 to avoid overwriting the system directory
  336. for _, dir := range pacmanConf.HookDir {
  337. if err := alpmHandle.AddHookDir(dir); err != nil {
  338. return err
  339. }
  340. }
  341. if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
  342. return err
  343. }
  344. if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
  345. return err
  346. }
  347. if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
  348. return err
  349. }
  350. if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
  351. return err
  352. }
  353. if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
  354. return err
  355. }
  356. if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
  357. return err
  358. }
  359. if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
  360. return err
  361. }
  362. /*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
  363. return err
  364. }
  365. if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
  366. return err
  367. }
  368. if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
  369. return err
  370. }*/
  371. if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
  372. return err
  373. }
  374. return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
  375. }