config.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package settings
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/Jguer/yay/v12/pkg/settings/parser"
  11. "github.com/Jguer/yay/v12/pkg/text"
  12. "github.com/leonelquinteros/gotext"
  13. )
  14. // HideMenus indicates if pacman's provider menus must be hidden.
  15. var HideMenus = false
  16. // NoConfirm indicates if user input should be skipped.
  17. var NoConfirm = false
  18. // Configuration stores yay's config.
  19. type Configuration struct {
  20. AURURL string `json:"aururl"`
  21. AURRPCURL string `json:"aurrpcurl"`
  22. BuildDir string `json:"buildDir"`
  23. Editor string `json:"editor"`
  24. EditorFlags string `json:"editorflags"`
  25. MakepkgBin string `json:"makepkgbin"`
  26. MakepkgConf string `json:"makepkgconf"`
  27. PacmanBin string `json:"pacmanbin"`
  28. PacmanConf string `json:"pacmanconf"`
  29. ReDownload string `json:"redownload"`
  30. AnswerClean string `json:"answerclean"`
  31. AnswerDiff string `json:"answerdiff"`
  32. AnswerEdit string `json:"answeredit"`
  33. AnswerUpgrade string `json:"answerupgrade"`
  34. GitBin string `json:"gitbin"`
  35. GpgBin string `json:"gpgbin"`
  36. GpgFlags string `json:"gpgflags"`
  37. MFlags string `json:"mflags"`
  38. SortBy string `json:"sortby"`
  39. SearchBy string `json:"searchby"`
  40. GitFlags string `json:"gitflags"`
  41. RemoveMake string `json:"removemake"`
  42. SudoBin string `json:"sudobin"`
  43. SudoFlags string `json:"sudoflags"`
  44. Version string `json:"version"`
  45. RequestSplitN int `json:"requestsplitn"`
  46. CompletionInterval int `json:"completionrefreshtime"`
  47. MaxConcurrentDownloads int `json:"maxconcurrentdownloads"`
  48. BottomUp bool `json:"bottomup"`
  49. SudoLoop bool `json:"sudoloop"`
  50. TimeUpdate bool `json:"timeupdate"`
  51. Devel bool `json:"devel"`
  52. CleanAfter bool `json:"cleanAfter"`
  53. Provides bool `json:"provides"`
  54. PGPFetch bool `json:"pgpfetch"`
  55. CleanMenu bool `json:"cleanmenu"`
  56. DiffMenu bool `json:"diffmenu"`
  57. EditMenu bool `json:"editmenu"`
  58. CombinedUpgrade bool `json:"combinedupgrade"`
  59. UseAsk bool `json:"useask"`
  60. BatchInstall bool `json:"batchinstall"`
  61. SingleLineResults bool `json:"singlelineresults"`
  62. SeparateSources bool `json:"separatesources"`
  63. Debug bool `json:"debug"`
  64. UseRPC bool `json:"rpc"`
  65. DoubleConfirm bool `json:"doubleconfirm"` // confirm install before and after build
  66. CompletionPath string `json:"-"`
  67. VCSFilePath string `json:"-"`
  68. // ConfigPath string `json:"-"`
  69. SaveConfig bool `json:"-"`
  70. Mode parser.TargetMode `json:"-"`
  71. ReBuild parser.RebuildMode `json:"rebuild"`
  72. }
  73. // SaveConfig writes yay config to file.
  74. func (c *Configuration) Save(configPath, version string) error {
  75. c.Version = version
  76. marshalledinfo, err := json.MarshalIndent(c, "", "\t")
  77. if err != nil {
  78. return err
  79. }
  80. // https://github.com/Jguer/yay/issues/1325
  81. marshalledinfo = append(marshalledinfo, '\n')
  82. // https://github.com/Jguer/yay/issues/1399
  83. if _, err = os.Stat(filepath.Dir(configPath)); os.IsNotExist(err) && err != nil {
  84. if mkErr := os.MkdirAll(filepath.Dir(configPath), 0o755); mkErr != nil {
  85. return mkErr
  86. }
  87. }
  88. in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
  89. if err != nil {
  90. return err
  91. }
  92. defer in.Close()
  93. if _, err = in.Write(marshalledinfo); err != nil {
  94. return err
  95. }
  96. return in.Sync()
  97. }
  98. func (c *Configuration) expandEnv() {
  99. c.AURURL = os.ExpandEnv(c.AURURL)
  100. c.AURRPCURL = os.ExpandEnv(c.AURRPCURL)
  101. c.BuildDir = expandEnvOrHome(c.BuildDir)
  102. c.Editor = expandEnvOrHome(c.Editor)
  103. c.EditorFlags = os.ExpandEnv(c.EditorFlags)
  104. c.MakepkgBin = expandEnvOrHome(c.MakepkgBin)
  105. c.MakepkgConf = expandEnvOrHome(c.MakepkgConf)
  106. c.PacmanBin = expandEnvOrHome(c.PacmanBin)
  107. c.PacmanConf = expandEnvOrHome(c.PacmanConf)
  108. c.GpgFlags = os.ExpandEnv(c.GpgFlags)
  109. c.MFlags = os.ExpandEnv(c.MFlags)
  110. c.GitFlags = os.ExpandEnv(c.GitFlags)
  111. c.SortBy = os.ExpandEnv(c.SortBy)
  112. c.SearchBy = os.ExpandEnv(c.SearchBy)
  113. c.GitBin = expandEnvOrHome(c.GitBin)
  114. c.GpgBin = expandEnvOrHome(c.GpgBin)
  115. c.SudoBin = expandEnvOrHome(c.SudoBin)
  116. c.SudoFlags = os.ExpandEnv(c.SudoFlags)
  117. c.ReDownload = os.ExpandEnv(c.ReDownload)
  118. c.ReBuild = parser.RebuildMode(os.ExpandEnv(string(c.ReBuild)))
  119. c.AnswerClean = os.ExpandEnv(c.AnswerClean)
  120. c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
  121. c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
  122. c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
  123. c.RemoveMake = os.ExpandEnv(c.RemoveMake)
  124. }
  125. func expandEnvOrHome(path string) string {
  126. path = os.ExpandEnv(path)
  127. if strings.HasPrefix(path, "~/") {
  128. path = filepath.Join(os.Getenv("HOME"), path[2:])
  129. }
  130. return path
  131. }
  132. func (c *Configuration) String() string {
  133. var buf bytes.Buffer
  134. enc := json.NewEncoder(&buf)
  135. enc.SetIndent("", "\t")
  136. if err := enc.Encode(c); err != nil {
  137. fmt.Fprintln(os.Stderr, err)
  138. }
  139. return buf.String()
  140. }
  141. // check privilege elevator exists otherwise try to find another one.
  142. func (c *Configuration) setPrivilegeElevator() error {
  143. if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
  144. c.SudoBin = auth
  145. if auth != "sudo" {
  146. c.SudoFlags = ""
  147. c.SudoLoop = false
  148. }
  149. }
  150. for _, bin := range [...]string{c.SudoBin, "sudo"} {
  151. if _, err := exec.LookPath(bin); err == nil {
  152. c.SudoBin = bin
  153. return nil // wrapper or sudo command existing. Retrocompatiblity
  154. }
  155. }
  156. c.SudoFlags = ""
  157. c.SudoLoop = false
  158. for _, bin := range [...]string{"doas", "pkexec", "su"} {
  159. if _, err := exec.LookPath(bin); err == nil {
  160. c.SudoBin = bin
  161. return nil // command existing
  162. }
  163. }
  164. return &ErrPrivilegeElevatorNotFound{confValue: c.SudoBin}
  165. }
  166. func DefaultConfig(version string) *Configuration {
  167. return &Configuration{
  168. AURURL: "https://aur.archlinux.org",
  169. BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
  170. CleanAfter: false,
  171. Editor: "",
  172. EditorFlags: "",
  173. Devel: false,
  174. MakepkgBin: "makepkg",
  175. MakepkgConf: "",
  176. PacmanBin: "pacman",
  177. PGPFetch: true,
  178. PacmanConf: "/etc/pacman.conf",
  179. GpgFlags: "",
  180. MFlags: "",
  181. GitFlags: "",
  182. BottomUp: true,
  183. CompletionInterval: 7,
  184. MaxConcurrentDownloads: 0,
  185. SortBy: "votes",
  186. SearchBy: "name-desc",
  187. SudoLoop: false,
  188. GitBin: "git",
  189. GpgBin: "gpg",
  190. SudoBin: "sudo",
  191. SudoFlags: "",
  192. TimeUpdate: false,
  193. RequestSplitN: 150,
  194. ReDownload: "no",
  195. ReBuild: "no",
  196. BatchInstall: false,
  197. AnswerClean: "",
  198. AnswerDiff: "",
  199. AnswerEdit: "",
  200. AnswerUpgrade: "",
  201. RemoveMake: "ask",
  202. Provides: true,
  203. CleanMenu: true,
  204. DiffMenu: true,
  205. EditMenu: false,
  206. UseAsk: false,
  207. CombinedUpgrade: true,
  208. SeparateSources: true,
  209. Version: version,
  210. Debug: false,
  211. UseRPC: true,
  212. DoubleConfirm: true,
  213. Mode: parser.ModeAny,
  214. }
  215. }
  216. func NewConfig(logger *text.Logger, configPath, version string) (*Configuration, error) {
  217. newConfig := DefaultConfig(version)
  218. cacheHome, errCache := getCacheHome()
  219. if errCache != nil && logger != nil {
  220. logger.Errorln(errCache)
  221. }
  222. newConfig.BuildDir = cacheHome
  223. newConfig.CompletionPath = filepath.Join(cacheHome, completionFileName)
  224. newConfig.VCSFilePath = filepath.Join(cacheHome, vcsFileName)
  225. newConfig.load(configPath)
  226. if aurdest := os.Getenv("AURDEST"); aurdest != "" {
  227. newConfig.BuildDir = aurdest
  228. }
  229. newConfig.expandEnv()
  230. if newConfig.BuildDir != systemdCache {
  231. errBuildDir := initDir(newConfig.BuildDir)
  232. if errBuildDir != nil {
  233. return nil, errBuildDir
  234. }
  235. }
  236. if errPE := newConfig.setPrivilegeElevator(); errPE != nil {
  237. return nil, errPE
  238. }
  239. return newConfig, nil
  240. }
  241. func (c *Configuration) load(configPath string) {
  242. cfile, err := os.Open(configPath)
  243. if !os.IsNotExist(err) && err != nil {
  244. fmt.Fprintln(os.Stderr,
  245. gotext.Get("failed to open config file '%s': %s", configPath, err))
  246. return
  247. }
  248. defer cfile.Close()
  249. if !os.IsNotExist(err) {
  250. decoder := json.NewDecoder(cfile)
  251. if err = decoder.Decode(c); err != nil {
  252. fmt.Fprintln(os.Stderr,
  253. gotext.Get("failed to read config file '%s': %s", configPath, err))
  254. }
  255. }
  256. }