config.go 8.6 KB

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