edit_menu.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // edit menu
  2. package menus
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "strings"
  12. gosrc "github.com/Morganamilo/go-srcinfo"
  13. mapset "github.com/deckarep/golang-set/v2"
  14. "github.com/leonelquinteros/gotext"
  15. "github.com/Jguer/yay/v12/pkg/settings"
  16. "github.com/Jguer/yay/v12/pkg/text"
  17. )
  18. // Editor returns the preferred system editor.
  19. func editor(log *text.Logger, editorConfig, editorFlags string, noConfirm bool) (editor string, args []string) {
  20. switch {
  21. case editorConfig != "":
  22. editor, err := exec.LookPath(editorConfig)
  23. if err != nil {
  24. log.Errorln(err)
  25. } else {
  26. return editor, strings.Fields(editorFlags)
  27. }
  28. fallthrough
  29. case os.Getenv("VISUAL") != "":
  30. if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
  31. editor, err := exec.LookPath(editorArgs[0])
  32. if err != nil {
  33. log.Errorln(err)
  34. } else {
  35. return editor, editorArgs[1:]
  36. }
  37. }
  38. fallthrough
  39. case os.Getenv("EDITOR") != "":
  40. if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
  41. editor, err := exec.LookPath(editorArgs[0])
  42. if err != nil {
  43. log.Errorln(err)
  44. } else {
  45. return editor, editorArgs[1:]
  46. }
  47. }
  48. fallthrough
  49. default:
  50. log.Errorln("\n", gotext.Get("%s is not set", text.Bold(text.Cyan("$EDITOR"))))
  51. log.Warnln(gotext.Get("Add %s or %s to your environment variables", text.Bold(text.Cyan("$EDITOR")), text.Bold(text.Cyan("$VISUAL"))))
  52. for {
  53. log.Infoln(gotext.Get("Edit PKGBUILD with?"))
  54. editorInput, err := text.GetInput(os.Stdin, "", noConfirm)
  55. if err != nil {
  56. log.Errorln(err)
  57. continue
  58. }
  59. editorArgs := strings.Fields(editorInput)
  60. if len(editorArgs) == 0 {
  61. continue
  62. }
  63. editor, err := exec.LookPath(editorArgs[0])
  64. if err != nil {
  65. log.Errorln(err)
  66. continue
  67. }
  68. return editor, editorArgs[1:]
  69. }
  70. }
  71. }
  72. func editPkgbuilds(log *text.Logger, pkgbuildDirs map[string]string, bases []string, editorConfig,
  73. editorFlags string, srcinfos map[string]*gosrc.Srcinfo, noConfirm bool,
  74. ) error {
  75. pkgbuilds := make([]string, 0, len(bases))
  76. for _, pkg := range bases {
  77. dir := pkgbuildDirs[pkg]
  78. pkgbuilds = append(pkgbuilds, filepath.Join(dir, "PKGBUILD"))
  79. if srcinfos != nil {
  80. for _, splitPkg := range srcinfos[pkg].SplitPackages() {
  81. if splitPkg.Install != "" {
  82. pkgbuilds = append(pkgbuilds, filepath.Join(dir, splitPkg.Install))
  83. }
  84. }
  85. }
  86. }
  87. if len(pkgbuilds) > 0 {
  88. editor, editorArgs := editor(log, editorConfig, editorFlags, noConfirm)
  89. editorArgs = append(editorArgs, pkgbuilds...)
  90. editcmd := exec.Command(editor, editorArgs...)
  91. editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  92. if err := editcmd.Run(); err != nil {
  93. return errors.New(gotext.Get("editor did not exit successfully, aborting: %s", err))
  94. }
  95. }
  96. return nil
  97. }
  98. func Edit(w io.Writer, log *text.Logger, editMenuOption bool, pkgbuildDirs map[string]string, editorConfig,
  99. editorFlags string, installed mapset.Set[string], srcinfos map[string]*gosrc.Srcinfo,
  100. noConfirm bool, editDefaultAnswer string,
  101. ) error {
  102. if !editMenuOption {
  103. return nil
  104. }
  105. bases := make([]string, 0, len(pkgbuildDirs))
  106. for pkg := range pkgbuildDirs {
  107. bases = append(bases, pkg)
  108. }
  109. toEdit, errMenu := selectionMenu(w, pkgbuildDirs, bases,
  110. installed, gotext.Get("PKGBUILDs to edit?"), noConfirm, editDefaultAnswer, nil)
  111. if errMenu != nil || len(toEdit) == 0 {
  112. return errMenu
  113. }
  114. if errEdit := editPkgbuilds(log, pkgbuildDirs, toEdit, editorConfig, editorFlags, srcinfos, noConfirm); errEdit != nil {
  115. return errEdit
  116. }
  117. fmt.Println()
  118. if !text.ContinueTask(os.Stdin, gotext.Get("Proceed with install?"), true, false) {
  119. return settings.ErrUserAbort{}
  120. }
  121. return nil
  122. }
  123. func EditFn(ctx context.Context, cfg *settings.Configuration, w io.Writer,
  124. pkgbuildDirsByBase map[string]string,
  125. ) error {
  126. if len(pkgbuildDirsByBase) == 0 {
  127. return nil // no work to do
  128. }
  129. bases := make([]string, 0, len(pkgbuildDirsByBase))
  130. for pkg := range pkgbuildDirsByBase {
  131. bases = append(bases, pkg)
  132. }
  133. toEdit, errMenu := selectionMenu(w, pkgbuildDirsByBase, bases,
  134. mapset.NewThreadUnsafeSet[string](),
  135. gotext.Get("PKGBUILDs to edit?"), settings.NoConfirm, cfg.AnswerEdit, nil)
  136. if errMenu != nil || len(toEdit) == 0 {
  137. return errMenu
  138. }
  139. // TOFIX: remove or use srcinfo data
  140. if errEdit := editPkgbuilds(cfg.Runtime.Logger, pkgbuildDirsByBase,
  141. toEdit, cfg.Editor, cfg.EditorFlags, nil, settings.NoConfirm); errEdit != nil {
  142. return errEdit
  143. }
  144. cfg.Runtime.Logger.Println()
  145. if !text.ContinueTask(os.Stdin, gotext.Get("Proceed with install?"), true, false) {
  146. return settings.ErrUserAbort{}
  147. }
  148. return nil
  149. }