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