config.go 11 KB

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