config.go 8.7 KB

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