config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/Jguer/yay/v12/pkg/settings/exe"
  13. "github.com/Jguer/yay/v12/pkg/settings/parser"
  14. "github.com/Jguer/yay/v12/pkg/text"
  15. "github.com/Jguer/yay/v12/pkg/vcs"
  16. "github.com/Jguer/aur/metadata"
  17. "github.com/Jguer/aur/rpc"
  18. "github.com/Jguer/votar/pkg/vote"
  19. "github.com/leonelquinteros/gotext"
  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. Runtime *Runtime `json:"-"`
  28. AURURL string `json:"aururl"`
  29. AURRPCURL string `json:"aurrpcurl"`
  30. BuildDir string `json:"buildDir"`
  31. Editor string `json:"editor"`
  32. EditorFlags string `json:"editorflags"`
  33. MakepkgBin string `json:"makepkgbin"`
  34. MakepkgConf string `json:"makepkgconf"`
  35. PacmanBin string `json:"pacmanbin"`
  36. PacmanConf string `json:"pacmanconf"`
  37. ReDownload string `json:"redownload"`
  38. ReBuild string `json:"rebuild"`
  39. AnswerClean string `json:"answerclean"`
  40. AnswerDiff string `json:"answerdiff"`
  41. AnswerEdit string `json:"answeredit"`
  42. AnswerUpgrade string `json:"answerupgrade"`
  43. GitBin string `json:"gitbin"`
  44. GpgBin string `json:"gpgbin"`
  45. GpgFlags string `json:"gpgflags"`
  46. MFlags string `json:"mflags"`
  47. SortBy string `json:"sortby"`
  48. SearchBy string `json:"searchby"`
  49. GitFlags string `json:"gitflags"`
  50. RemoveMake string `json:"removemake"`
  51. SudoBin string `json:"sudobin"`
  52. SudoFlags string `json:"sudoflags"`
  53. Version string `json:"version"`
  54. RequestSplitN int `json:"requestsplitn"`
  55. CompletionInterval int `json:"completionrefreshtime"`
  56. MaxConcurrentDownloads int `json:"maxconcurrentdownloads"`
  57. BottomUp bool `json:"bottomup"`
  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. SingleLineResults bool `json:"singlelineresults"`
  72. SeparateSources bool `json:"separatesources"`
  73. NewInstallEngine bool `json:"newinstallengine"`
  74. Debug bool `json:"debug"`
  75. UseRPC bool `json:"rpc"`
  76. }
  77. // SaveConfig writes yay config to file.
  78. func (c *Configuration) Save(configPath string) error {
  79. c.Version = c.Runtime.Version
  80. marshalledinfo, err := json.MarshalIndent(c, "", "\t")
  81. if err != nil {
  82. return err
  83. }
  84. // https://github.com/Jguer/yay/issues/1325
  85. marshalledinfo = append(marshalledinfo, '\n')
  86. // https://github.com/Jguer/yay/issues/1399
  87. if _, err = os.Stat(filepath.Dir(configPath)); os.IsNotExist(err) && err != nil {
  88. if mkErr := os.MkdirAll(filepath.Dir(configPath), 0o755); mkErr != nil {
  89. return mkErr
  90. }
  91. }
  92. in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  93. if err != nil {
  94. return err
  95. }
  96. defer in.Close()
  97. if _, err = in.Write(marshalledinfo); err != nil {
  98. return err
  99. }
  100. return in.Sync()
  101. }
  102. func (c *Configuration) expandEnv() {
  103. c.AURURL = os.ExpandEnv(c.AURURL)
  104. c.AURRPCURL = os.ExpandEnv(c.AURRPCURL)
  105. c.BuildDir = expandEnvOrHome(c.BuildDir)
  106. c.Editor = expandEnvOrHome(c.Editor)
  107. c.EditorFlags = os.ExpandEnv(c.EditorFlags)
  108. c.MakepkgBin = expandEnvOrHome(c.MakepkgBin)
  109. c.MakepkgConf = expandEnvOrHome(c.MakepkgConf)
  110. c.PacmanBin = expandEnvOrHome(c.PacmanBin)
  111. c.PacmanConf = expandEnvOrHome(c.PacmanConf)
  112. c.GpgFlags = os.ExpandEnv(c.GpgFlags)
  113. c.MFlags = os.ExpandEnv(c.MFlags)
  114. c.GitFlags = os.ExpandEnv(c.GitFlags)
  115. c.SortBy = os.ExpandEnv(c.SortBy)
  116. c.SearchBy = os.ExpandEnv(c.SearchBy)
  117. c.GitBin = expandEnvOrHome(c.GitBin)
  118. c.GpgBin = expandEnvOrHome(c.GpgBin)
  119. c.SudoBin = expandEnvOrHome(c.SudoBin)
  120. c.SudoFlags = os.ExpandEnv(c.SudoFlags)
  121. c.ReDownload = os.ExpandEnv(c.ReDownload)
  122. c.ReBuild = os.ExpandEnv(c.ReBuild)
  123. c.AnswerClean = os.ExpandEnv(c.AnswerClean)
  124. c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
  125. c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
  126. c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
  127. c.RemoveMake = os.ExpandEnv(c.RemoveMake)
  128. }
  129. func expandEnvOrHome(path string) string {
  130. path = os.ExpandEnv(path)
  131. if strings.HasPrefix(path, "~/") {
  132. path = filepath.Join(os.Getenv("HOME"), path[2:])
  133. }
  134. return path
  135. }
  136. func (c *Configuration) String() string {
  137. var buf bytes.Buffer
  138. enc := json.NewEncoder(&buf)
  139. enc.SetIndent("", "\t")
  140. if err := enc.Encode(c); err != nil {
  141. fmt.Fprintln(os.Stderr, err)
  142. }
  143. return buf.String()
  144. }
  145. // check privilege elevator exists otherwise try to find another one.
  146. func (c *Configuration) setPrivilegeElevator() error {
  147. if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
  148. c.SudoBin = auth
  149. if auth != "sudo" {
  150. c.SudoFlags = ""
  151. c.SudoLoop = false
  152. }
  153. }
  154. for _, bin := range [...]string{c.SudoBin, "sudo"} {
  155. if _, err := exec.LookPath(bin); err == nil {
  156. c.SudoBin = bin
  157. return nil // wrapper or sudo command existing. Retrocompatiblity
  158. }
  159. }
  160. c.SudoFlags = ""
  161. c.SudoLoop = false
  162. for _, bin := range [...]string{"doas", "pkexec", "su"} {
  163. if _, err := exec.LookPath(bin); err == nil {
  164. c.SudoBin = bin
  165. return nil // command existing
  166. }
  167. }
  168. return &ErrPrivilegeElevatorNotFound{confValue: c.SudoBin}
  169. }
  170. func DefaultConfig(version string) *Configuration {
  171. return &Configuration{
  172. AURURL: "https://aur.archlinux.org",
  173. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  174. CleanAfter: false,
  175. Editor: "",
  176. EditorFlags: "",
  177. Devel: false,
  178. MakepkgBin: "makepkg",
  179. MakepkgConf: "",
  180. PacmanBin: "pacman",
  181. PGPFetch: true,
  182. PacmanConf: "/etc/pacman.conf",
  183. GpgFlags: "",
  184. MFlags: "",
  185. GitFlags: "",
  186. BottomUp: true,
  187. CompletionInterval: 7,
  188. MaxConcurrentDownloads: 0,
  189. SortBy: "votes",
  190. SearchBy: "name-desc",
  191. SudoLoop: false,
  192. GitBin: "git",
  193. GpgBin: "gpg",
  194. SudoBin: "sudo",
  195. SudoFlags: "",
  196. TimeUpdate: false,
  197. RequestSplitN: 150,
  198. ReDownload: "no",
  199. ReBuild: "no",
  200. BatchInstall: false,
  201. AnswerClean: "",
  202. AnswerDiff: "",
  203. AnswerEdit: "",
  204. AnswerUpgrade: "",
  205. RemoveMake: "ask",
  206. Provides: true,
  207. UpgradeMenu: true,
  208. CleanMenu: true,
  209. DiffMenu: true,
  210. EditMenu: false,
  211. UseAsk: false,
  212. CombinedUpgrade: false,
  213. SeparateSources: true,
  214. NewInstallEngine: true,
  215. Version: version,
  216. Debug: false,
  217. UseRPC: true,
  218. Runtime: &Runtime{
  219. Logger: text.GlobalLogger,
  220. },
  221. }
  222. }
  223. func NewConfig(version string) (*Configuration, error) {
  224. newConfig := DefaultConfig(version)
  225. cacheHome, errCache := getCacheHome()
  226. if errCache != nil {
  227. text.Errorln(errCache)
  228. }
  229. newConfig.BuildDir = cacheHome
  230. configPath := getConfigPath()
  231. newConfig.load(configPath)
  232. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  233. newConfig.BuildDir = aurdest
  234. }
  235. newConfig.expandEnv()
  236. if newConfig.BuildDir != systemdCache {
  237. errBuildDir := initDir(newConfig.BuildDir)
  238. if errBuildDir != nil {
  239. return nil, errBuildDir
  240. }
  241. }
  242. if errPE := newConfig.setPrivilegeElevator(); errPE != nil {
  243. return nil, errPE
  244. }
  245. userAgent := fmt.Sprintf("Yay/%s", version)
  246. voteClient, errVote := vote.NewClient(vote.WithUserAgent(userAgent))
  247. if errVote != nil {
  248. return nil, errVote
  249. }
  250. voteClient.SetCredentials(
  251. os.Getenv("AUR_USERNAME"),
  252. os.Getenv("AUR_PASSWORD"))
  253. newConfig.Runtime = &Runtime{
  254. ConfigPath: configPath,
  255. Version: version,
  256. Mode: parser.ModeAny,
  257. SaveConfig: false,
  258. CompletionPath: filepath.Join(cacheHome, completionFileName),
  259. CmdBuilder: newConfig.CmdBuilder(nil),
  260. PacmanConf: nil,
  261. VCSStore: nil,
  262. HTTPClient: &http.Client{},
  263. AURClient: nil,
  264. VoteClient: voteClient,
  265. QueryBuilder: nil,
  266. Logger: text.NewLogger(os.Stdout, os.Stdin, newConfig.Debug, "runtime"),
  267. }
  268. var errAURCache error
  269. userAgentFn := func(ctx context.Context, req *http.Request) error {
  270. req.Header.Set("User-Agent", userAgent)
  271. return nil
  272. }
  273. newConfig.Runtime.AURCache, errAURCache = metadata.New(
  274. metadata.WithHTTPClient(newConfig.Runtime.HTTPClient),
  275. metadata.WithCacheFilePath(filepath.Join(newConfig.BuildDir, "aur.json")),
  276. metadata.WithRequestEditorFn(userAgentFn),
  277. metadata.WithBaseURL(newConfig.AURURL),
  278. metadata.WithDebugLogger(newConfig.Runtime.Logger.Child("aur").Debugln),
  279. )
  280. if errAURCache != nil {
  281. return nil, fmt.Errorf(gotext.Get("failed to retrieve aur Cache")+": %w", errAURCache)
  282. }
  283. var errAUR error
  284. newConfig.Runtime.AURClient, errAUR = rpc.NewClient(
  285. rpc.WithHTTPClient(newConfig.Runtime.HTTPClient),
  286. rpc.WithRequestEditorFn(userAgentFn),
  287. rpc.WithLogFn(newConfig.Runtime.Logger.Child("rpc").Debugln))
  288. if errAUR != nil {
  289. return nil, errAUR
  290. }
  291. if newConfig.UseRPC {
  292. newConfig.Runtime.AURCache = newConfig.Runtime.AURClient
  293. }
  294. newConfig.Runtime.VCSStore = vcs.NewInfoStore(
  295. filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder,
  296. newConfig.Runtime.Logger.Child("vcs"))
  297. err := newConfig.Runtime.VCSStore.Load()
  298. return newConfig, err
  299. }
  300. func (c *Configuration) load(configPath string) {
  301. cfile, err := os.Open(configPath)
  302. if !os.IsNotExist(err) && err != nil {
  303. fmt.Fprintln(os.Stderr,
  304. gotext.Get("failed to open config file '%s': %s", configPath, err))
  305. return
  306. }
  307. defer cfile.Close()
  308. if !os.IsNotExist(err) {
  309. decoder := json.NewDecoder(cfile)
  310. if err = decoder.Decode(c); err != nil {
  311. fmt.Fprintln(os.Stderr,
  312. gotext.Get("failed to read config file '%s': %s", configPath, err))
  313. }
  314. }
  315. }
  316. func (c *Configuration) CmdBuilder(runner exe.Runner) exe.ICmdBuilder {
  317. if runner == nil {
  318. runner = &exe.OSRunner{Log: c.Runtime.Logger.Child("runner")}
  319. }
  320. return &exe.CmdBuilder{
  321. GitBin: c.GitBin,
  322. GitFlags: strings.Fields(c.GitFlags),
  323. GPGBin: c.GpgBin,
  324. GPGFlags: strings.Fields(c.GpgFlags),
  325. MakepkgFlags: strings.Fields(c.MFlags),
  326. MakepkgConfPath: c.MakepkgConf,
  327. MakepkgBin: c.MakepkgBin,
  328. SudoBin: c.SudoBin,
  329. SudoFlags: strings.Fields(c.SudoFlags),
  330. SudoLoopEnabled: c.SudoLoop,
  331. PacmanBin: c.PacmanBin,
  332. PacmanConfigPath: c.PacmanConf,
  333. PacmanDBPath: "",
  334. Runner: runner,
  335. Log: c.Runtime.Logger.Child("cmd_builder"),
  336. }
  337. }