config.go 8.8 KB

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