config.go 11 KB

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