config.go 5.4 KB

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