config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. // Describes Sorting method for numberdisplay
  20. const (
  21. BottomUp = iota
  22. TopDown
  23. )
  24. type targetMode int
  25. const (
  26. ModeAUR targetMode = iota
  27. ModeRepo
  28. ModeAny
  29. )
  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 = "8.1139"
  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 (config *Configuration) defaultSettings() {
  117. buildDir := "$HOME/.cache/yay"
  118. if os.Getenv("XDG_CACHE_HOME") != "" {
  119. buildDir = "$XDG_CACHE_HOME/yay"
  120. }
  121. config.AURURL = "https://aur.archlinux.org"
  122. config.BuildDir = buildDir
  123. config.CleanAfter = false
  124. config.Editor = ""
  125. config.EditorFlags = ""
  126. config.Devel = false
  127. config.MakepkgBin = "makepkg"
  128. config.MakepkgConf = ""
  129. config.NoConfirm = false
  130. config.PacmanBin = "pacman"
  131. config.PGPFetch = true
  132. config.PacmanConf = "/etc/pacman.conf"
  133. config.GpgFlags = ""
  134. config.MFlags = ""
  135. config.GitFlags = ""
  136. config.SortMode = BottomUp
  137. config.CompletionInterval = 7
  138. config.SortBy = "votes"
  139. config.SudoLoop = false
  140. config.TarBin = "bsdtar"
  141. config.GitBin = "git"
  142. config.GpgBin = "gpg"
  143. config.TimeUpdate = false
  144. config.RequestSplitN = 150
  145. config.ReDownload = "no"
  146. config.ReBuild = "no"
  147. config.AnswerClean = ""
  148. config.AnswerDiff = ""
  149. config.AnswerEdit = ""
  150. config.AnswerUpgrade = ""
  151. config.RemoveMake = "ask"
  152. config.GitClone = true
  153. config.Provides = true
  154. config.UpgradeMenu = true
  155. config.CleanMenu = true
  156. config.DiffMenu = true
  157. config.EditMenu = false
  158. config.UseAsk = false
  159. config.CombinedUpgrade = false
  160. }
  161. func (config *Configuration) expandEnv() {
  162. config.AURURL = os.ExpandEnv(config.AURURL)
  163. config.BuildDir = os.ExpandEnv(config.BuildDir)
  164. config.Editor = os.ExpandEnv(config.Editor)
  165. config.EditorFlags = os.ExpandEnv(config.EditorFlags)
  166. config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
  167. config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
  168. config.PacmanBin = os.ExpandEnv(config.PacmanBin)
  169. config.PacmanConf = os.ExpandEnv(config.PacmanConf)
  170. config.GpgFlags = os.ExpandEnv(config.GpgFlags)
  171. config.MFlags = os.ExpandEnv(config.MFlags)
  172. config.GitFlags = os.ExpandEnv(config.GitFlags)
  173. config.SortBy = os.ExpandEnv(config.SortBy)
  174. config.TarBin = os.ExpandEnv(config.TarBin)
  175. config.GitBin = os.ExpandEnv(config.GitBin)
  176. config.GpgBin = os.ExpandEnv(config.GpgBin)
  177. config.ReDownload = os.ExpandEnv(config.ReDownload)
  178. config.ReBuild = os.ExpandEnv(config.ReBuild)
  179. config.AnswerClean = os.ExpandEnv(config.AnswerClean)
  180. config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
  181. config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
  182. config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
  183. config.RemoveMake = os.ExpandEnv(config.RemoveMake)
  184. }
  185. // Editor returns the preferred system editor.
  186. func editor() (string, []string) {
  187. switch {
  188. case config.Editor != "":
  189. editor, err := exec.LookPath(config.Editor)
  190. if err != nil {
  191. fmt.Println(err)
  192. } else {
  193. return editor, strings.Fields(config.EditorFlags)
  194. }
  195. fallthrough
  196. case os.Getenv("EDITOR") != "":
  197. editorArgs := strings.Fields(os.Getenv("EDITOR"))
  198. editor, err := exec.LookPath(editorArgs[0])
  199. if err != nil {
  200. fmt.Println(err)
  201. } else {
  202. return editor, editorArgs[1:]
  203. }
  204. fallthrough
  205. case os.Getenv("VISUAL") != "":
  206. editorArgs := strings.Fields(os.Getenv("VISUAL"))
  207. editor, err := exec.LookPath(editorArgs[0])
  208. if err != nil {
  209. fmt.Println(err)
  210. } else {
  211. return editor, editorArgs[1:]
  212. }
  213. fallthrough
  214. default:
  215. fmt.Println()
  216. fmt.Println(bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
  217. fmt.Println(bold(red(arrow)) + bold(" Please add ") + bold(cyan("$EDITOR")) + bold(" or ") + bold(cyan("$VISUAL")) + bold(" to your environment variables."))
  218. for {
  219. fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
  220. editorInput, err := getInput("")
  221. if err != nil {
  222. fmt.Println(err)
  223. continue
  224. }
  225. editorArgs := strings.Fields(editorInput)
  226. editor, err := exec.LookPath(editorArgs[0])
  227. if err != nil {
  228. fmt.Println(err)
  229. continue
  230. }
  231. return editor, editorArgs[1:]
  232. }
  233. }
  234. }
  235. // ContinueTask prompts if user wants to continue task.
  236. //If NoConfirm is set the action will continue without user input.
  237. func continueTask(s string, cont bool) bool {
  238. if config.NoConfirm {
  239. return cont
  240. }
  241. var response string
  242. var postFix string
  243. yes := "yes"
  244. no := "no"
  245. y := string([]rune(yes)[0])
  246. n := string([]rune(no)[0])
  247. if cont {
  248. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  249. } else {
  250. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  251. }
  252. fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
  253. len, err := fmt.Scanln(&response)
  254. if err != nil || len == 0 {
  255. return cont
  256. }
  257. response = strings.ToLower(response)
  258. return response == yes || response == y
  259. }
  260. func getInput(defaultValue string) (string, error) {
  261. if defaultValue != "" || config.NoConfirm {
  262. fmt.Println(defaultValue)
  263. return defaultValue, nil
  264. }
  265. reader := bufio.NewReader(os.Stdin)
  266. buf, overflow, err := reader.ReadLine()
  267. if err != nil {
  268. return "", err
  269. }
  270. if overflow {
  271. return "", fmt.Errorf("Input too long")
  272. }
  273. return string(buf), nil
  274. }
  275. func (config Configuration) String() string {
  276. var buf bytes.Buffer
  277. enc := json.NewEncoder(&buf)
  278. enc.SetIndent("", "\t")
  279. if err := enc.Encode(config); err != nil {
  280. fmt.Println(err)
  281. }
  282. return buf.String()
  283. }
  284. func toUsage(usages []string) alpm.Usage {
  285. if len(usages) == 0 {
  286. return alpm.UsageAll
  287. }
  288. var ret alpm.Usage = 0
  289. for _, usage := range usages {
  290. switch usage {
  291. case "Sync":
  292. ret |= alpm.UsageSync
  293. case "Search":
  294. ret |= alpm.UsageSearch
  295. case "Install":
  296. ret |= alpm.UsageInstall
  297. case "Upgrade":
  298. ret |= alpm.UsageUpgrade
  299. case "All":
  300. ret |= alpm.UsageAll
  301. }
  302. }
  303. return ret
  304. }
  305. func configureAlpm(conf *pacmanconf.Config) error {
  306. var err error
  307. // TODO: set SigLevel
  308. //sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
  309. //localFileSigLevel := alpm.SigUseDefault
  310. //remoteFileSigLevel := alpm.SigUseDefault
  311. for _, repo := range pacmanConf.Repos {
  312. // TODO: set SigLevel
  313. db, err := alpmHandle.RegisterSyncDb(repo.Name, 0)
  314. if err != nil {
  315. return err
  316. }
  317. db.SetServers(repo.Servers)
  318. db.SetUsage(toUsage(repo.Usage))
  319. }
  320. if err = alpmHandle.SetCacheDirs(pacmanConf.CacheDir...); err != nil {
  321. return err
  322. }
  323. // add hook directories 1-by-1 to avoid overwriting the system directory
  324. for _, dir := range pacmanConf.HookDir {
  325. if err = alpmHandle.AddHookDir(dir); err != nil {
  326. return err
  327. }
  328. }
  329. if err = alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
  330. return err
  331. }
  332. if err = alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
  333. return err
  334. }
  335. if err = alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg...); err != nil {
  336. return err
  337. }
  338. if err = alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup...); err != nil {
  339. return err
  340. }
  341. if err = alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
  342. return err
  343. }
  344. if err = alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade...); err != nil {
  345. return err
  346. }
  347. if alpmHandle.SetNoExtracts(pacmanConf.NoExtract...); err != nil {
  348. return err
  349. }
  350. /*if err = alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
  351. return err
  352. }
  353. if err = alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
  354. return err
  355. }
  356. if err = alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
  357. return err
  358. }*/
  359. if err = alpmHandle.SetDeltaRatio(pacmanConf.UseDelta); err != nil {
  360. return err
  361. }
  362. if err = alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
  363. return err
  364. }
  365. if err = alpmHandle.SetCheckSpace(pacmanConf.CheckSpace); err != nil {
  366. return err
  367. }
  368. return nil
  369. }