config.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. Version string `json:"version"`
  68. }
  69. // SaveConfig writes yay config to file.
  70. func (c *Configuration) Save(configPath string) error {
  71. c.Version = c.Runtime.Version
  72. marshalledinfo, err := json.MarshalIndent(c, "", "\t")
  73. if err != nil {
  74. return err
  75. }
  76. // https://github.com/Jguer/yay/issues/1325
  77. marshalledinfo = append(marshalledinfo, '\n')
  78. // https://github.com/Jguer/yay/issues/1399
  79. if _, err = os.Stat(filepath.Dir(configPath)); os.IsNotExist(err) && err != nil {
  80. if mkErr := os.MkdirAll(filepath.Dir(configPath), 0o755); mkErr != nil {
  81. return mkErr
  82. }
  83. }
  84. in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  85. if err != nil {
  86. return err
  87. }
  88. defer in.Close()
  89. if _, err = in.Write(marshalledinfo); err != nil {
  90. return err
  91. }
  92. return in.Sync()
  93. }
  94. func (c *Configuration) expandEnv() {
  95. c.AURURL = os.ExpandEnv(c.AURURL)
  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. // check privilege elevator exists otherwise try to find another one.
  130. func (c *Configuration) setPrivilegeElevator() error {
  131. if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
  132. c.SudoBin = auth
  133. if auth != "sudo" {
  134. c.SudoFlags = ""
  135. c.SudoLoop = false
  136. }
  137. }
  138. for _, bin := range [...]string{c.SudoBin, "sudo"} {
  139. if _, err := exec.LookPath(bin); err == nil {
  140. c.SudoBin = bin
  141. return nil // wrapper or sudo command existing. Retrocompatiblity
  142. }
  143. }
  144. c.SudoFlags = ""
  145. c.SudoLoop = false
  146. for _, bin := range [...]string{"doas", "pkexec", "su"} {
  147. if _, err := exec.LookPath(bin); err == nil {
  148. c.SudoBin = bin
  149. return nil // command existing
  150. }
  151. }
  152. return &ErrPrivilegeElevatorNotFound{confValue: c.SudoBin}
  153. }
  154. func DefaultConfig(version string) *Configuration {
  155. return &Configuration{
  156. AURURL: "https://aur.archlinux.org",
  157. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  158. CleanAfter: false,
  159. Editor: "",
  160. EditorFlags: "",
  161. Devel: false,
  162. MakepkgBin: "makepkg",
  163. MakepkgConf: "",
  164. PacmanBin: "pacman",
  165. PGPFetch: true,
  166. PacmanConf: "/etc/pacman.conf",
  167. GpgFlags: "",
  168. MFlags: "",
  169. GitFlags: "",
  170. BottomUp: true,
  171. CompletionInterval: 7,
  172. SortBy: "votes",
  173. SearchBy: "name-desc",
  174. SudoLoop: false,
  175. GitBin: "git",
  176. GpgBin: "gpg",
  177. SudoBin: "sudo",
  178. SudoFlags: "",
  179. TimeUpdate: false,
  180. RequestSplitN: 150,
  181. ReDownload: "no",
  182. ReBuild: "no",
  183. BatchInstall: false,
  184. AnswerClean: "",
  185. AnswerDiff: "",
  186. AnswerEdit: "",
  187. AnswerUpgrade: "",
  188. RemoveMake: "ask",
  189. Provides: true,
  190. UpgradeMenu: true,
  191. CleanMenu: true,
  192. DiffMenu: true,
  193. EditMenu: false,
  194. UseAsk: false,
  195. CombinedUpgrade: false,
  196. Version: version,
  197. }
  198. }
  199. func NewConfig(version string) (*Configuration, error) {
  200. newConfig := DefaultConfig(version)
  201. cacheHome, errCache := getCacheHome()
  202. if errCache != nil {
  203. text.Errorln(errCache)
  204. }
  205. newConfig.BuildDir = cacheHome
  206. configPath := getConfigPath()
  207. newConfig.load(configPath)
  208. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  209. newConfig.BuildDir = aurdest
  210. }
  211. newConfig.expandEnv()
  212. if newConfig.BuildDir != systemdCache {
  213. errBuildDir := initDir(newConfig.BuildDir)
  214. if errBuildDir != nil {
  215. return nil, errBuildDir
  216. }
  217. }
  218. if errPE := newConfig.setPrivilegeElevator(); errPE != nil {
  219. return nil, errPE
  220. }
  221. newConfig.Runtime = &Runtime{
  222. ConfigPath: configPath,
  223. Version: version,
  224. Mode: parser.ModeAny,
  225. SaveConfig: false,
  226. CompletionPath: filepath.Join(cacheHome, completionFileName),
  227. CmdBuilder: newConfig.CmdBuilder(nil),
  228. PacmanConf: nil,
  229. VCSStore: nil,
  230. HTTPClient: &http.Client{},
  231. AURClient: nil,
  232. }
  233. var errAUR error
  234. newConfig.Runtime.AURClient, errAUR = aur.NewClient(aur.WithHTTPClient(newConfig.Runtime.HTTPClient),
  235. aur.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
  236. req.Header.Set("User-Agent", fmt.Sprintf("Yay/%s", version))
  237. return nil
  238. }))
  239. if errAUR != nil {
  240. return nil, errAUR
  241. }
  242. newConfig.Runtime.VCSStore = vcs.NewInfoStore(
  243. filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder)
  244. err := newConfig.Runtime.VCSStore.Load()
  245. return newConfig, err
  246. }
  247. func (c *Configuration) load(configPath string) {
  248. cfile, err := os.Open(configPath)
  249. if !os.IsNotExist(err) && err != nil {
  250. fmt.Fprintln(os.Stderr,
  251. gotext.Get("failed to open config file '%s': %s", configPath, err))
  252. return
  253. }
  254. defer cfile.Close()
  255. if !os.IsNotExist(err) {
  256. decoder := json.NewDecoder(cfile)
  257. if err = decoder.Decode(c); err != nil {
  258. fmt.Fprintln(os.Stderr,
  259. gotext.Get("failed to read config file '%s': %s", configPath, err))
  260. }
  261. }
  262. }
  263. func (c *Configuration) CmdBuilder(runner exe.Runner) exe.ICmdBuilder {
  264. if runner == nil {
  265. runner = &exe.OSRunner{}
  266. }
  267. return &exe.CmdBuilder{
  268. GitBin: c.GitBin,
  269. GitFlags: strings.Fields(c.GitFlags),
  270. MakepkgFlags: strings.Fields(c.MFlags),
  271. MakepkgConfPath: c.MakepkgConf,
  272. MakepkgBin: c.MakepkgBin,
  273. SudoBin: c.SudoBin,
  274. SudoFlags: strings.Fields(c.SudoFlags),
  275. SudoLoopEnabled: c.SudoLoop,
  276. PacmanBin: c.PacmanBin,
  277. PacmanConfigPath: c.PacmanConf,
  278. PacmanDBPath: "",
  279. Runner: runner,
  280. }
  281. }