config.go 7.3 KB

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