config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. TarBin string `json:"tarbin"`
  42. ReDownload string `json:"redownload"`
  43. ReBuild string `json:"rebuild"`
  44. BatchInstall bool `json:"batchinstall"`
  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. GitClone bool `json:"gitclone"`
  69. Provides bool `json:"provides"`
  70. PGPFetch bool `json:"pgpfetch"`
  71. UpgradeMenu bool `json:"upgrademenu"`
  72. CleanMenu bool `json:"cleanmenu"`
  73. DiffMenu bool `json:"diffmenu"`
  74. EditMenu bool `json:"editmenu"`
  75. CombinedUpgrade bool `json:"combinedupgrade"`
  76. UseAsk bool `json:"useask"`
  77. }
  78. var version = "9.4.2"
  79. // configFileName holds the name of the config file.
  80. const configFileName string = "config.json"
  81. // vcsFileName holds the name of the vcs file.
  82. const vcsFileName string = "vcs.json"
  83. // useColor enables/disables colored printing
  84. var useColor bool
  85. // configHome handles config directory home
  86. var configHome string
  87. // cacheHome handles cache home
  88. var cacheHome string
  89. // savedInfo holds the current vcs info
  90. var savedInfo vcsInfo
  91. // configfile holds yay config file path.
  92. var configFile string
  93. // vcsfile holds yay vcs info file path.
  94. var vcsFile string
  95. // shouldSaveConfig holds whether or not the config should be saved
  96. var shouldSaveConfig bool
  97. // YayConf holds the current config values for yay.
  98. var config *Configuration
  99. // AlpmConf holds the current config values for pacman.
  100. var pacmanConf *pacmanconf.Config
  101. // AlpmHandle is the alpm handle used by yay.
  102. var alpmHandle *alpm.Handle
  103. // Mode is used to restrict yay to AUR or repo only modes
  104. var mode = modeAny
  105. var hideMenus = false
  106. // SaveConfig writes yay config to file.
  107. func (config *Configuration) saveConfig() error {
  108. marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
  109. in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  110. if err != nil {
  111. return err
  112. }
  113. defer in.Close()
  114. _, err = in.Write(marshalledinfo)
  115. if err != nil {
  116. return err
  117. }
  118. err = in.Sync()
  119. return err
  120. }
  121. func defaultSettings() *Configuration {
  122. config := &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. TarBin: "bsdtar",
  145. GitBin: "git",
  146. GpgBin: "gpg",
  147. SudoBin: "sudo",
  148. SudoFlags: "",
  149. TimeUpdate: false,
  150. RequestSplitN: 150,
  151. ReDownload: "no",
  152. ReBuild: "no",
  153. BatchInstall: false,
  154. AnswerClean: "",
  155. AnswerDiff: "",
  156. AnswerEdit: "",
  157. AnswerUpgrade: "",
  158. RemoveMake: "ask",
  159. GitClone: true,
  160. Provides: true,
  161. UpgradeMenu: true,
  162. CleanMenu: true,
  163. DiffMenu: true,
  164. EditMenu: false,
  165. UseAsk: false,
  166. CombinedUpgrade: false,
  167. }
  168. if os.Getenv("XDG_CACHE_HOME") != "" {
  169. config.BuildDir = "$XDG_CACHE_HOME/yay"
  170. }
  171. return config
  172. }
  173. func (config *Configuration) expandEnv() {
  174. config.AURURL = os.ExpandEnv(config.AURURL)
  175. config.ABSDir = os.ExpandEnv(config.ABSDir)
  176. config.BuildDir = os.ExpandEnv(config.BuildDir)
  177. config.Editor = os.ExpandEnv(config.Editor)
  178. config.EditorFlags = os.ExpandEnv(config.EditorFlags)
  179. config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
  180. config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
  181. config.PacmanBin = os.ExpandEnv(config.PacmanBin)
  182. config.PacmanConf = os.ExpandEnv(config.PacmanConf)
  183. config.GpgFlags = os.ExpandEnv(config.GpgFlags)
  184. config.MFlags = os.ExpandEnv(config.MFlags)
  185. config.GitFlags = os.ExpandEnv(config.GitFlags)
  186. config.SortBy = os.ExpandEnv(config.SortBy)
  187. config.SearchBy = os.ExpandEnv(config.SearchBy)
  188. config.TarBin = os.ExpandEnv(config.TarBin)
  189. config.GitBin = os.ExpandEnv(config.GitBin)
  190. config.GpgBin = os.ExpandEnv(config.GpgBin)
  191. config.SudoBin = os.ExpandEnv(config.SudoBin)
  192. config.SudoFlags = os.ExpandEnv(config.SudoFlags)
  193. config.ReDownload = os.ExpandEnv(config.ReDownload)
  194. config.ReBuild = os.ExpandEnv(config.ReBuild)
  195. config.AnswerClean = os.ExpandEnv(config.AnswerClean)
  196. config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
  197. config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
  198. config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
  199. config.RemoveMake = os.ExpandEnv(config.RemoveMake)
  200. }
  201. // Editor returns the preferred system editor.
  202. func editor() (string, []string) {
  203. switch {
  204. case config.Editor != "":
  205. editor, err := exec.LookPath(config.Editor)
  206. if err != nil {
  207. fmt.Fprintln(os.Stderr, err)
  208. } else {
  209. return editor, strings.Fields(config.EditorFlags)
  210. }
  211. fallthrough
  212. case os.Getenv("EDITOR") != "":
  213. editorArgs := strings.Fields(os.Getenv("EDITOR"))
  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. case os.Getenv("VISUAL") != "":
  222. editorArgs := strings.Fields(os.Getenv("VISUAL"))
  223. editor, err := exec.LookPath(editorArgs[0])
  224. if err != nil {
  225. fmt.Fprintln(os.Stderr, err)
  226. } else {
  227. return editor, editorArgs[1:]
  228. }
  229. fallthrough
  230. default:
  231. fmt.Fprintln(os.Stderr)
  232. fmt.Fprintln(os.Stderr, bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  233. fmt.Fprintln(os.Stderr, bold(red(arrow))+bold(" Please add ")+bold(cyan("$EDITOR"))+bold(" or ")+bold(cyan("$VISUAL"))+bold(" to your environment variables."))
  234. for {
  235. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  236. editorInput, err := getInput("")
  237. if err != nil {
  238. fmt.Fprintln(os.Stderr, err)
  239. continue
  240. }
  241. editorArgs := strings.Fields(editorInput)
  242. if len(editorArgs) == 0 {
  243. continue
  244. }
  245. editor, err := exec.LookPath(editorArgs[0])
  246. if err != nil {
  247. fmt.Fprintln(os.Stderr, err)
  248. continue
  249. }
  250. return editor, editorArgs[1:]
  251. }
  252. }
  253. }
  254. // ContinueTask prompts if user wants to continue task.
  255. //If NoConfirm is set the action will continue without user input.
  256. func continueTask(s string, cont bool) bool {
  257. if config.NoConfirm {
  258. return cont
  259. }
  260. var response string
  261. var postFix string
  262. yes := "yes"
  263. no := "no"
  264. y := string([]rune(yes)[0])
  265. n := string([]rune(no)[0])
  266. if cont {
  267. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  268. } else {
  269. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  270. }
  271. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  272. if _, err := fmt.Scanln(&response); err != nil {
  273. return cont
  274. }
  275. response = strings.ToLower(response)
  276. return response == yes || response == y
  277. }
  278. func getInput(defaultValue string) (string, error) {
  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("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(conf *pacmanconf.Config) 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. }