config.go 9.1 KB

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