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/runtime"
  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 := log.GetInput("", 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 EditFn(ctx context.Context, run *runtime.Runtime, w io.Writer,
  99. pkgbuildDirsByBase map[string]string, installed mapset.Set[string],
  100. ) error {
  101. if len(pkgbuildDirsByBase) == 0 {
  102. return nil // no work to do
  103. }
  104. bases := make([]string, 0, len(pkgbuildDirsByBase))
  105. for pkg := range pkgbuildDirsByBase {
  106. bases = append(bases, pkg)
  107. }
  108. toEdit, errMenu := selectionMenu(run.Logger, pkgbuildDirsByBase, bases, installed,
  109. gotext.Get("PKGBUILDs to edit?"), settings.NoConfirm, run.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(run.Logger, pkgbuildDirsByBase,
  115. toEdit, run.Cfg.Editor, run.Cfg.EditorFlags, nil, settings.NoConfirm); errEdit != nil {
  116. return errEdit
  117. }
  118. run.Logger.Println()
  119. if !run.Logger.ContinueTask(gotext.Get("Proceed with install?"), true, false) {
  120. return settings.ErrUserAbort{}
  121. }
  122. return nil
  123. }