config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. alpm "github.com/Jguer/go-alpm"
  9. "github.com/Morganamilo/go-pacmanconf"
  10. "github.com/leonelquinteros/gotext"
  11. "github.com/Jguer/yay/v10/pkg/settings"
  12. "github.com/Jguer/yay/v10/pkg/text"
  13. )
  14. // Verbosity settings for search
  15. const (
  16. numberMenu = iota
  17. detailed
  18. minimal
  19. )
  20. var yayVersion = "10.0.0"
  21. var localePath = "/usr/share/locale"
  22. // savedInfo holds the current vcs info
  23. var savedInfo vcsInfo
  24. // YayConf holds the current config values for yay.
  25. var config *settings.Configuration
  26. // AlpmHandle is the alpm handle used by yay.
  27. // var alpmHandle *alpm.Handle
  28. var hideMenus = false
  29. // Editor returns the preferred system editor.
  30. func editor() (editor string, args []string) {
  31. switch {
  32. case config.Editor != "":
  33. editor, err := exec.LookPath(config.Editor)
  34. if err != nil {
  35. fmt.Fprintln(os.Stderr, err)
  36. } else {
  37. return editor, strings.Fields(config.EditorFlags)
  38. }
  39. fallthrough
  40. case os.Getenv("EDITOR") != "":
  41. if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
  42. editor, err := exec.LookPath(editorArgs[0])
  43. if err != nil {
  44. fmt.Fprintln(os.Stderr, err)
  45. } else {
  46. return editor, editorArgs[1:]
  47. }
  48. }
  49. fallthrough
  50. case os.Getenv("VISUAL") != "":
  51. if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
  52. editor, err := exec.LookPath(editorArgs[0])
  53. if err != nil {
  54. fmt.Fprintln(os.Stderr, err)
  55. } else {
  56. return editor, editorArgs[1:]
  57. }
  58. }
  59. fallthrough
  60. default:
  61. fmt.Fprintln(os.Stderr)
  62. text.Errorln(gotext.Get("%s is not set", bold(cyan("$EDITOR"))))
  63. text.Warnln(gotext.Get("Add %s or %s to your environment variables", bold(cyan("$EDITOR")), bold(cyan("$VISUAL"))))
  64. for {
  65. text.Infoln(gotext.Get("Edit PKGBUILD with?"))
  66. editorInput, err := getInput("")
  67. if err != nil {
  68. fmt.Fprintln(os.Stderr, err)
  69. continue
  70. }
  71. editorArgs := strings.Fields(editorInput)
  72. if len(editorArgs) == 0 {
  73. continue
  74. }
  75. editor, err := exec.LookPath(editorArgs[0])
  76. if err != nil {
  77. fmt.Fprintln(os.Stderr, err)
  78. continue
  79. }
  80. return editor, editorArgs[1:]
  81. }
  82. }
  83. }
  84. // ContinueTask prompts if user wants to continue task.
  85. // If NoConfirm is set the action will continue without user input.
  86. func continueTask(s string, cont bool) bool {
  87. if config.NoConfirm {
  88. return cont
  89. }
  90. var response string
  91. var postFix string
  92. yes := gotext.Get("yes")
  93. no := gotext.Get("no")
  94. y := string([]rune(yes)[0])
  95. n := string([]rune(no)[0])
  96. if cont {
  97. postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
  98. } else {
  99. postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
  100. }
  101. text.Info(bold(s), bold(postFix))
  102. if _, err := fmt.Scanln(&response); err != nil {
  103. return cont
  104. }
  105. response = strings.ToLower(response)
  106. return response == yes || response == y
  107. }
  108. func getInput(defaultValue string) (string, error) {
  109. text.Info()
  110. if defaultValue != "" || config.NoConfirm {
  111. fmt.Println(defaultValue)
  112. return defaultValue, nil
  113. }
  114. reader := bufio.NewReader(os.Stdin)
  115. buf, overflow, err := reader.ReadLine()
  116. if err != nil {
  117. return "", err
  118. }
  119. if overflow {
  120. return "", fmt.Errorf(gotext.Get("input too long"))
  121. }
  122. return string(buf), nil
  123. }
  124. func toUsage(usages []string) alpm.Usage {
  125. if len(usages) == 0 {
  126. return alpm.UsageAll
  127. }
  128. var ret alpm.Usage
  129. for _, usage := range usages {
  130. switch usage {
  131. case "Sync":
  132. ret |= alpm.UsageSync
  133. case "Search":
  134. ret |= alpm.UsageSearch
  135. case "Install":
  136. ret |= alpm.UsageInstall
  137. case "Upgrade":
  138. ret |= alpm.UsageUpgrade
  139. case "All":
  140. ret |= alpm.UsageAll
  141. }
  142. }
  143. return ret
  144. }
  145. func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *alpm.Handle) error {
  146. // TODO: set SigLevel
  147. // sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
  148. // localFileSigLevel := alpm.SigUseDefault
  149. // remoteFileSigLevel := alpm.SigUseDefault
  150. for _, repo := range pacmanConf.Repos {
  151. // TODO: set SigLevel
  152. db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
  153. if err != nil {
  154. return err
  155. }
  156. db.SetServers(repo.Servers)
  157. db.SetUsage(toUsage(repo.Usage))
  158. }
  159. if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
  160. return err
  161. }
  162. // add hook directories 1-by-1 to avoid overwriting the system directory
  163. for _, dir := range pacmanConf.HookDir {
  164. if err := alpmHandle.AddHookDir(dir); err != nil {
  165. return err
  166. }
  167. }
  168. if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
  169. return err
  170. }
  171. if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
  172. return err
  173. }
  174. if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
  175. return err
  176. }
  177. if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
  178. return err
  179. }
  180. if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
  181. return err
  182. }
  183. if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
  184. return err
  185. }
  186. if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
  187. return err
  188. }
  189. /*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
  190. return err
  191. }
  192. if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
  193. return err
  194. }
  195. if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
  196. return err
  197. }*/
  198. if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
  199. return err
  200. }
  201. return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
  202. }