config.go 11 KB

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