config.go 8.1 KB

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