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. 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.0.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. len, err := fmt.Scanln(&response)
  256. if err != nil || len == 0 {
  257. return cont
  258. }
  259. response = strings.ToLower(response)
  260. return response == yes || response == y
  261. }
  262. func getInput(defaultValue string) (string, error) {
  263. if defaultValue != "" || config.NoConfirm {
  264. fmt.Println(defaultValue)
  265. return defaultValue, nil
  266. }
  267. reader := bufio.NewReader(os.Stdin)
  268. buf, overflow, err := reader.ReadLine()
  269. if err != nil {
  270. return "", err
  271. }
  272. if overflow {
  273. return "", fmt.Errorf("Input too long")
  274. }
  275. return string(buf), nil
  276. }
  277. func (config Configuration) String() string {
  278. var buf bytes.Buffer
  279. enc := json.NewEncoder(&buf)
  280. enc.SetIndent("", "\t")
  281. if err := enc.Encode(config); err != nil {
  282. fmt.Fprintln(os.Stderr, err)
  283. }
  284. return buf.String()
  285. }
  286. func toUsage(usages []string) alpm.Usage {
  287. if len(usages) == 0 {
  288. return alpm.UsageAll
  289. }
  290. var ret alpm.Usage
  291. for _, usage := range usages {
  292. switch usage {
  293. case "Sync":
  294. ret |= alpm.UsageSync
  295. case "Search":
  296. ret |= alpm.UsageSearch
  297. case "Install":
  298. ret |= alpm.UsageInstall
  299. case "Upgrade":
  300. ret |= alpm.UsageUpgrade
  301. case "All":
  302. ret |= alpm.UsageAll
  303. }
  304. }
  305. return ret
  306. }
  307. func configureAlpm(conf *pacmanconf.Config) error {
  308. var err 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 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. if err = alpmHandle.SetCheckSpace(pacmanConf.CheckSpace); err != nil {
  368. return err
  369. }
  370. return nil
  371. }