config.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package settings
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/leonelquinteros/gotext"
  10. "github.com/Jguer/yay/v10/pkg/settings/exe"
  11. "github.com/Jguer/yay/v10/pkg/vcs"
  12. )
  13. const (
  14. // Describes Sorting method for numberdisplay
  15. BottomUp = iota
  16. TopDown
  17. )
  18. // HideMenus indicates if pacman's provider menus must be hidden
  19. var HideMenus = false
  20. // NoConfirm indicates if user input should be skipped
  21. var NoConfirm = false
  22. // Configuration stores yay's config.
  23. type Configuration struct {
  24. AURURL string `json:"aururl"`
  25. BuildDir string `json:"buildDir"`
  26. ABSDir string `json:"absdir"`
  27. Editor string `json:"editor"`
  28. EditorFlags string `json:"editorflags"`
  29. MakepkgBin string `json:"makepkgbin"`
  30. MakepkgConf string `json:"makepkgconf"`
  31. PacmanBin string `json:"pacmanbin"`
  32. PacmanConf string `json:"pacmanconf"`
  33. ReDownload string `json:"redownload"`
  34. ReBuild string `json:"rebuild"`
  35. AnswerClean string `json:"answerclean"`
  36. AnswerDiff string `json:"answerdiff"`
  37. AnswerEdit string `json:"answeredit"`
  38. AnswerUpgrade string `json:"answerupgrade"`
  39. GitBin string `json:"gitbin"`
  40. GpgBin string `json:"gpgbin"`
  41. GpgFlags string `json:"gpgflags"`
  42. MFlags string `json:"mflags"`
  43. SortBy string `json:"sortby"`
  44. SearchBy string `json:"searchby"`
  45. GitFlags string `json:"gitflags"`
  46. RemoveMake string `json:"removemake"`
  47. SudoBin string `json:"sudobin"`
  48. SudoFlags string `json:"sudoflags"`
  49. RequestSplitN int `json:"requestsplitn"`
  50. SearchMode int `json:"-"`
  51. SortMode int `json:"sortmode"`
  52. CompletionInterval int `json:"completionrefreshtime"`
  53. SudoLoop bool `json:"sudoloop"`
  54. TimeUpdate bool `json:"timeupdate"`
  55. Devel bool `json:"devel"`
  56. CleanAfter bool `json:"cleanAfter"`
  57. Provides bool `json:"provides"`
  58. PGPFetch bool `json:"pgpfetch"`
  59. UpgradeMenu bool `json:"upgrademenu"`
  60. CleanMenu bool `json:"cleanmenu"`
  61. DiffMenu bool `json:"diffmenu"`
  62. EditMenu bool `json:"editmenu"`
  63. CombinedUpgrade bool `json:"combinedupgrade"`
  64. UseAsk bool `json:"useask"`
  65. BatchInstall bool `json:"batchinstall"`
  66. Runtime *Runtime `json:"-"`
  67. }
  68. // SaveConfig writes yay config to file.
  69. func (c *Configuration) Save(configPath string) error {
  70. marshalledinfo, err := json.MarshalIndent(c, "", "\t")
  71. if err != nil {
  72. return err
  73. }
  74. // https://github.com/Jguer/yay/issues/1325
  75. marshalledinfo = append(marshalledinfo, '\n')
  76. in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  77. if err != nil {
  78. return err
  79. }
  80. defer in.Close()
  81. if _, err = in.Write(marshalledinfo); err != nil {
  82. return err
  83. }
  84. return in.Sync()
  85. }
  86. func (c *Configuration) expandEnv() {
  87. c.AURURL = os.ExpandEnv(c.AURURL)
  88. c.ABSDir = os.ExpandEnv(c.ABSDir)
  89. c.BuildDir = os.ExpandEnv(c.BuildDir)
  90. c.Editor = os.ExpandEnv(c.Editor)
  91. c.EditorFlags = os.ExpandEnv(c.EditorFlags)
  92. c.MakepkgBin = os.ExpandEnv(c.MakepkgBin)
  93. c.MakepkgConf = os.ExpandEnv(c.MakepkgConf)
  94. c.PacmanBin = os.ExpandEnv(c.PacmanBin)
  95. c.PacmanConf = os.ExpandEnv(c.PacmanConf)
  96. c.GpgFlags = os.ExpandEnv(c.GpgFlags)
  97. c.MFlags = os.ExpandEnv(c.MFlags)
  98. c.GitFlags = os.ExpandEnv(c.GitFlags)
  99. c.SortBy = os.ExpandEnv(c.SortBy)
  100. c.SearchBy = os.ExpandEnv(c.SearchBy)
  101. c.GitBin = os.ExpandEnv(c.GitBin)
  102. c.GpgBin = os.ExpandEnv(c.GpgBin)
  103. c.SudoBin = os.ExpandEnv(c.SudoBin)
  104. c.SudoFlags = os.ExpandEnv(c.SudoFlags)
  105. c.ReDownload = os.ExpandEnv(c.ReDownload)
  106. c.ReBuild = os.ExpandEnv(c.ReBuild)
  107. c.AnswerClean = os.ExpandEnv(c.AnswerClean)
  108. c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
  109. c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
  110. c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
  111. c.RemoveMake = os.ExpandEnv(c.RemoveMake)
  112. }
  113. func (c *Configuration) String() string {
  114. var buf bytes.Buffer
  115. enc := json.NewEncoder(&buf)
  116. enc.SetIndent("", "\t")
  117. if err := enc.Encode(c); err != nil {
  118. fmt.Fprintln(os.Stderr, err)
  119. }
  120. return buf.String()
  121. }
  122. func DefaultConfig() *Configuration {
  123. return &Configuration{
  124. AURURL: "https://aur.archlinux.org",
  125. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  126. ABSDir: os.ExpandEnv("$HOME/.cache/yay/abs"),
  127. CleanAfter: false,
  128. Editor: "",
  129. EditorFlags: "",
  130. Devel: false,
  131. MakepkgBin: "makepkg",
  132. MakepkgConf: "",
  133. PacmanBin: "pacman",
  134. PGPFetch: true,
  135. PacmanConf: "/etc/pacman.conf",
  136. GpgFlags: "",
  137. MFlags: "",
  138. GitFlags: "",
  139. SortMode: BottomUp,
  140. CompletionInterval: 7,
  141. SortBy: "votes",
  142. SearchBy: "name-desc",
  143. SudoLoop: false,
  144. GitBin: "git",
  145. GpgBin: "gpg",
  146. SudoBin: "sudo",
  147. SudoFlags: "",
  148. TimeUpdate: false,
  149. RequestSplitN: 150,
  150. ReDownload: "no",
  151. ReBuild: "no",
  152. BatchInstall: false,
  153. AnswerClean: "",
  154. AnswerDiff: "",
  155. AnswerEdit: "",
  156. AnswerUpgrade: "",
  157. RemoveMake: "ask",
  158. Provides: true,
  159. UpgradeMenu: true,
  160. CleanMenu: true,
  161. DiffMenu: true,
  162. EditMenu: false,
  163. UseAsk: false,
  164. CombinedUpgrade: false,
  165. }
  166. }
  167. func NewConfig() (*Configuration, error) {
  168. newConfig := DefaultConfig()
  169. cacheHome := getCacheHome()
  170. newConfig.BuildDir = cacheHome
  171. configPath := getConfigPath()
  172. newConfig.load(configPath)
  173. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  174. newConfig.BuildDir = aurdest
  175. }
  176. newConfig.expandEnv()
  177. newConfig.Runtime = &Runtime{
  178. Mode: ModeAny,
  179. SaveConfig: false,
  180. CompletionPath: filepath.Join(cacheHome, completionFileName),
  181. CmdRunner: &exe.OSRunner{},
  182. CmdBuilder: &exe.CmdBuilder{
  183. GitBin: newConfig.GitBin,
  184. GitFlags: strings.Fields(newConfig.GitFlags),
  185. MakepkgFlags: strings.Fields(newConfig.MFlags),
  186. MakepkgConfPath: newConfig.MakepkgConf,
  187. MakepkgBin: newConfig.MakepkgBin,
  188. },
  189. PacmanConf: nil,
  190. VCSStore: nil,
  191. }
  192. newConfig.Runtime.VCSStore = vcs.NewInfoStore(filepath.Join(cacheHome, vcsFileName),
  193. newConfig.Runtime.CmdRunner, newConfig.Runtime.CmdBuilder)
  194. if err := initDir(newConfig.BuildDir); err != nil {
  195. return nil, err
  196. }
  197. err := newConfig.Runtime.VCSStore.Load()
  198. return newConfig, err
  199. }
  200. func (c *Configuration) load(configPath string) {
  201. cfile, err := os.Open(configPath)
  202. if !os.IsNotExist(err) && err != nil {
  203. fmt.Fprintln(os.Stderr,
  204. gotext.Get("failed to open config file '%s': %s", configPath, err))
  205. return
  206. }
  207. defer cfile.Close()
  208. if !os.IsNotExist(err) {
  209. decoder := json.NewDecoder(cfile)
  210. if err = decoder.Decode(c); err != nil {
  211. fmt.Fprintln(os.Stderr,
  212. gotext.Get("failed to read config file '%s': %s", configPath, err))
  213. }
  214. }
  215. }