edit_menu.go 3.7 KB

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