config.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/v11/pkg/settings/exe"
  15. "github.com/Jguer/yay/v11/pkg/settings/parser"
  16. "github.com/Jguer/yay/v11/pkg/text"
  17. "github.com/Jguer/yay/v11/pkg/vcs"
  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. 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. CompletionInterval int `json:"completionrefreshtime"`
  51. BottomUp bool `json:"bottomup"`
  52. SudoLoop bool `json:"sudoloop"`
  53. TimeUpdate bool `json:"timeupdate"`
  54. Devel bool `json:"devel"`
  55. CleanAfter bool `json:"cleanAfter"`
  56. Provides bool `json:"provides"`
  57. PGPFetch bool `json:"pgpfetch"`
  58. UpgradeMenu bool `json:"upgrademenu"`
  59. CleanMenu bool `json:"cleanmenu"`
  60. DiffMenu bool `json:"diffmenu"`
  61. EditMenu bool `json:"editmenu"`
  62. CombinedUpgrade bool `json:"combinedupgrade"`
  63. UseAsk bool `json:"useask"`
  64. BatchInstall bool `json:"batchinstall"`
  65. SingleLineResults bool `json:"singlelineresults"`
  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.BuildDir = os.ExpandEnv(c.BuildDir)
  95. c.Editor = os.ExpandEnv(c.Editor)
  96. c.EditorFlags = os.ExpandEnv(c.EditorFlags)
  97. c.MakepkgBin = os.ExpandEnv(c.MakepkgBin)
  98. c.MakepkgConf = os.ExpandEnv(c.MakepkgConf)
  99. c.PacmanBin = os.ExpandEnv(c.PacmanBin)
  100. c.PacmanConf = os.ExpandEnv(c.PacmanConf)
  101. c.GpgFlags = os.ExpandEnv(c.GpgFlags)
  102. c.MFlags = os.ExpandEnv(c.MFlags)
  103. c.GitFlags = os.ExpandEnv(c.GitFlags)
  104. c.SortBy = os.ExpandEnv(c.SortBy)
  105. c.SearchBy = os.ExpandEnv(c.SearchBy)
  106. c.GitBin = os.ExpandEnv(c.GitBin)
  107. c.GpgBin = os.ExpandEnv(c.GpgBin)
  108. c.SudoBin = os.ExpandEnv(c.SudoBin)
  109. c.SudoFlags = os.ExpandEnv(c.SudoFlags)
  110. c.ReDownload = os.ExpandEnv(c.ReDownload)
  111. c.ReBuild = os.ExpandEnv(c.ReBuild)
  112. c.AnswerClean = os.ExpandEnv(c.AnswerClean)
  113. c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
  114. c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
  115. c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
  116. c.RemoveMake = os.ExpandEnv(c.RemoveMake)
  117. }
  118. func (c *Configuration) String() string {
  119. var buf bytes.Buffer
  120. enc := json.NewEncoder(&buf)
  121. enc.SetIndent("", "\t")
  122. if err := enc.Encode(c); err != nil {
  123. fmt.Fprintln(os.Stderr, err)
  124. }
  125. return buf.String()
  126. }
  127. // check privilege elevator exists otherwise try to find another one.
  128. func (c *Configuration) setPrivilegeElevator() error {
  129. if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
  130. c.SudoBin = auth
  131. if auth != "sudo" {
  132. c.SudoFlags = ""
  133. c.SudoLoop = false
  134. }
  135. }
  136. for _, bin := range [...]string{c.SudoBin, "sudo"} {
  137. if _, err := exec.LookPath(bin); err == nil {
  138. c.SudoBin = bin
  139. return nil // wrapper or sudo command existing. Retrocompatiblity
  140. }
  141. }
  142. c.SudoFlags = ""
  143. c.SudoLoop = false
  144. for _, bin := range [...]string{"doas", "pkexec", "su"} {
  145. if _, err := exec.LookPath(bin); err == nil {
  146. c.SudoBin = bin
  147. return nil // command existing
  148. }
  149. }
  150. return &ErrPrivilegeElevatorNotFound{confValue: c.SudoBin}
  151. }
  152. func DefaultConfig() *Configuration {
  153. return &Configuration{
  154. AURURL: "https://aur.archlinux.org",
  155. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  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. BottomUp: true,
  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, errCache := getCacheHome()
  199. if errCache != nil {
  200. text.Errorln(errCache)
  201. }
  202. newConfig.BuildDir = cacheHome
  203. configPath := getConfigPath()
  204. newConfig.load(configPath)
  205. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  206. newConfig.BuildDir = aurdest
  207. }
  208. newConfig.expandEnv()
  209. if newConfig.BuildDir != systemdCache {
  210. errBuildDir := initDir(newConfig.BuildDir)
  211. if errBuildDir != nil {
  212. return nil, errBuildDir
  213. }
  214. }
  215. if errPE := newConfig.setPrivilegeElevator(); errPE != nil {
  216. return nil, errPE
  217. }
  218. newConfig.Runtime = &Runtime{
  219. ConfigPath: configPath,
  220. Mode: parser.ModeAny,
  221. SaveConfig: false,
  222. CompletionPath: filepath.Join(cacheHome, completionFileName),
  223. CmdBuilder: newConfig.CmdBuilder(nil),
  224. PacmanConf: nil,
  225. VCSStore: nil,
  226. HTTPClient: &http.Client{},
  227. AURClient: nil,
  228. }
  229. var errAUR error
  230. newConfig.Runtime.AURClient, errAUR = aur.NewClient(aur.WithHTTPClient(newConfig.Runtime.HTTPClient),
  231. aur.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
  232. req.Header.Set("User-Agent", fmt.Sprintf("Yay/%s", version))
  233. return nil
  234. }))
  235. if errAUR != nil {
  236. return nil, errAUR
  237. }
  238. newConfig.Runtime.VCSStore = vcs.NewInfoStore(
  239. filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder)
  240. err := newConfig.Runtime.VCSStore.Load()
  241. return newConfig, err
  242. }
  243. func (c *Configuration) load(configPath string) {
  244. cfile, err := os.Open(configPath)
  245. if !os.IsNotExist(err) && err != nil {
  246. fmt.Fprintln(os.Stderr,
  247. gotext.Get("failed to open config file '%s': %s", configPath, err))
  248. return
  249. }
  250. defer cfile.Close()
  251. if !os.IsNotExist(err) {
  252. decoder := json.NewDecoder(cfile)
  253. if err = decoder.Decode(c); err != nil {
  254. fmt.Fprintln(os.Stderr,
  255. gotext.Get("failed to read config file '%s': %s", configPath, err))
  256. }
  257. }
  258. }
  259. func (c *Configuration) CmdBuilder(runner exe.Runner) exe.ICmdBuilder {
  260. if runner == nil {
  261. runner = &exe.OSRunner{}
  262. }
  263. return &exe.CmdBuilder{
  264. GitBin: c.GitBin,
  265. GitFlags: strings.Fields(c.GitFlags),
  266. MakepkgFlags: strings.Fields(c.MFlags),
  267. MakepkgConfPath: c.MakepkgConf,
  268. MakepkgBin: c.MakepkgBin,
  269. SudoBin: c.SudoBin,
  270. SudoFlags: strings.Fields(c.SudoFlags),
  271. SudoLoopEnabled: c.SudoLoop,
  272. PacmanBin: c.PacmanBin,
  273. PacmanConfigPath: c.PacmanConf,
  274. PacmanDBPath: "",
  275. Runner: runner,
  276. }
  277. }