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