config.go 7.7 KB

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