config.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // https://github.com/Jguer/yay/issues/1399
  77. if _, err = os.Stat(filepath.Dir(configPath)); os.IsNotExist(err) && err != nil {
  78. if mkErr := os.MkdirAll(filepath.Dir(configPath), 0o755); mkErr != nil {
  79. return mkErr
  80. }
  81. }
  82. in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  83. if err != nil {
  84. return err
  85. }
  86. defer in.Close()
  87. if _, err = in.Write(marshalledinfo); err != nil {
  88. return err
  89. }
  90. return in.Sync()
  91. }
  92. func (c *Configuration) expandEnv() {
  93. c.AURURL = os.ExpandEnv(c.AURURL)
  94. c.ABSDir = os.ExpandEnv(c.ABSDir)
  95. c.BuildDir = os.ExpandEnv(c.BuildDir)
  96. c.Editor = os.ExpandEnv(c.Editor)
  97. c.EditorFlags = os.ExpandEnv(c.EditorFlags)
  98. c.MakepkgBin = os.ExpandEnv(c.MakepkgBin)
  99. c.MakepkgConf = os.ExpandEnv(c.MakepkgConf)
  100. c.PacmanBin = os.ExpandEnv(c.PacmanBin)
  101. c.PacmanConf = os.ExpandEnv(c.PacmanConf)
  102. c.GpgFlags = os.ExpandEnv(c.GpgFlags)
  103. c.MFlags = os.ExpandEnv(c.MFlags)
  104. c.GitFlags = os.ExpandEnv(c.GitFlags)
  105. c.SortBy = os.ExpandEnv(c.SortBy)
  106. c.SearchBy = os.ExpandEnv(c.SearchBy)
  107. c.GitBin = os.ExpandEnv(c.GitBin)
  108. c.GpgBin = os.ExpandEnv(c.GpgBin)
  109. c.SudoBin = os.ExpandEnv(c.SudoBin)
  110. c.SudoFlags = os.ExpandEnv(c.SudoFlags)
  111. c.ReDownload = os.ExpandEnv(c.ReDownload)
  112. c.ReBuild = os.ExpandEnv(c.ReBuild)
  113. c.AnswerClean = os.ExpandEnv(c.AnswerClean)
  114. c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
  115. c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
  116. c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
  117. c.RemoveMake = os.ExpandEnv(c.RemoveMake)
  118. }
  119. func (c *Configuration) String() string {
  120. var buf bytes.Buffer
  121. enc := json.NewEncoder(&buf)
  122. enc.SetIndent("", "\t")
  123. if err := enc.Encode(c); err != nil {
  124. fmt.Fprintln(os.Stderr, err)
  125. }
  126. return buf.String()
  127. }
  128. func DefaultConfig() *Configuration {
  129. return &Configuration{
  130. AURURL: "https://aur.archlinux.org",
  131. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  132. ABSDir: os.ExpandEnv("$HOME/.cache/yay/abs"),
  133. CleanAfter: false,
  134. Editor: "",
  135. EditorFlags: "",
  136. Devel: false,
  137. MakepkgBin: "makepkg",
  138. MakepkgConf: "",
  139. PacmanBin: "pacman",
  140. PGPFetch: true,
  141. PacmanConf: "/etc/pacman.conf",
  142. GpgFlags: "",
  143. MFlags: "",
  144. GitFlags: "",
  145. SortMode: BottomUp,
  146. CompletionInterval: 7,
  147. SortBy: "votes",
  148. SearchBy: "name-desc",
  149. SudoLoop: false,
  150. GitBin: "git",
  151. GpgBin: "gpg",
  152. SudoBin: "sudo",
  153. SudoFlags: "",
  154. TimeUpdate: false,
  155. RequestSplitN: 150,
  156. ReDownload: "no",
  157. ReBuild: "no",
  158. BatchInstall: false,
  159. AnswerClean: "",
  160. AnswerDiff: "",
  161. AnswerEdit: "",
  162. AnswerUpgrade: "",
  163. RemoveMake: "ask",
  164. Provides: true,
  165. UpgradeMenu: true,
  166. CleanMenu: true,
  167. DiffMenu: true,
  168. EditMenu: false,
  169. UseAsk: false,
  170. CombinedUpgrade: false,
  171. }
  172. }
  173. func NewConfig() (*Configuration, error) {
  174. newConfig := DefaultConfig()
  175. cacheHome := getCacheHome()
  176. newConfig.BuildDir = cacheHome
  177. configPath := getConfigPath()
  178. newConfig.load(configPath)
  179. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  180. newConfig.BuildDir = aurdest
  181. }
  182. newConfig.expandEnv()
  183. newConfig.Runtime = &Runtime{
  184. ConfigPath: configPath,
  185. Mode: ModeAny,
  186. SaveConfig: false,
  187. CompletionPath: filepath.Join(cacheHome, completionFileName),
  188. CmdRunner: &exe.OSRunner{},
  189. CmdBuilder: &exe.CmdBuilder{
  190. GitBin: newConfig.GitBin,
  191. GitFlags: strings.Fields(newConfig.GitFlags),
  192. MakepkgFlags: strings.Fields(newConfig.MFlags),
  193. MakepkgConfPath: newConfig.MakepkgConf,
  194. MakepkgBin: newConfig.MakepkgBin,
  195. },
  196. PacmanConf: nil,
  197. VCSStore: nil,
  198. }
  199. newConfig.Runtime.VCSStore = vcs.NewInfoStore(filepath.Join(cacheHome, vcsFileName),
  200. newConfig.Runtime.CmdRunner, newConfig.Runtime.CmdBuilder)
  201. if err := initDir(newConfig.BuildDir); err != nil {
  202. return nil, err
  203. }
  204. err := newConfig.Runtime.VCSStore.Load()
  205. return newConfig, err
  206. }
  207. func (c *Configuration) load(configPath string) {
  208. cfile, err := os.Open(configPath)
  209. if !os.IsNotExist(err) && err != nil {
  210. fmt.Fprintln(os.Stderr,
  211. gotext.Get("failed to open config file '%s': %s", configPath, err))
  212. return
  213. }
  214. defer cfile.Close()
  215. if !os.IsNotExist(err) {
  216. decoder := json.NewDecoder(cfile)
  217. if err = decoder.Decode(c); err != nil {
  218. fmt.Fprintln(os.Stderr,
  219. gotext.Get("failed to read config file '%s': %s", configPath, err))
  220. }
  221. }
  222. }