config.go 11 KB

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