edit_menu.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // edit menu
  2. package menus
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. gosrc "github.com/Morganamilo/go-srcinfo"
  11. "github.com/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v11/pkg/dep"
  13. "github.com/Jguer/yay/v11/pkg/settings"
  14. "github.com/Jguer/yay/v11/pkg/stringset"
  15. "github.com/Jguer/yay/v11/pkg/text"
  16. )
  17. // Editor returns the preferred system editor.
  18. func editor(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. fmt.Fprintln(os.Stderr, 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. fmt.Fprintln(os.Stderr, 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. fmt.Fprintln(os.Stderr, err)
  43. } else {
  44. return editor, editorArgs[1:]
  45. }
  46. }
  47. fallthrough
  48. default:
  49. fmt.Fprintln(os.Stderr)
  50. text.Errorln(gotext.Get("%s is not set", text.Bold(text.Cyan("$EDITOR"))))
  51. text.Warnln(gotext.Get("Add %s or %s to your environment variables", text.Bold(text.Cyan("$EDITOR")), text.Bold(text.Cyan("$VISUAL"))))
  52. for {
  53. text.Infoln(gotext.Get("Edit PKGBUILD with?"))
  54. editorInput, err := text.GetInput("", noConfirm)
  55. if err != nil {
  56. fmt.Fprintln(os.Stderr, 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. fmt.Fprintln(os.Stderr, err)
  66. continue
  67. }
  68. return editor, editorArgs[1:]
  69. }
  70. }
  71. }
  72. func editPkgbuilds(buildDir string, bases []dep.Base, editorConfig,
  73. editorFlags string, srcinfos map[string]*gosrc.Srcinfo, noConfirm bool,
  74. ) error {
  75. pkgbuilds := make([]string, 0, len(bases))
  76. for _, base := range bases {
  77. pkg := base.Pkgbase()
  78. dir := filepath.Join(buildDir, pkg)
  79. pkgbuilds = append(pkgbuilds, filepath.Join(dir, "PKGBUILD"))
  80. for _, splitPkg := range srcinfos[pkg].SplitPackages() {
  81. if splitPkg.Install != "" {
  82. pkgbuilds = append(pkgbuilds, filepath.Join(dir, splitPkg.Install))
  83. }
  84. }
  85. }
  86. if len(pkgbuilds) > 0 {
  87. editor, editorArgs := editor(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 Edit(editMenuOption bool, buildDir string, bases []dep.Base, editorConfig,
  98. editorFlags string, installed stringset.StringSet, srcinfos map[string]*gosrc.Srcinfo,
  99. noConfirm bool, editDefaultAnswer string,
  100. ) error {
  101. if !editMenuOption {
  102. return nil
  103. }
  104. toEdit, errMenu := selectionMenu(buildDir, bases,
  105. installed, gotext.Get("PKGBUILDs to edit?"), noConfirm, editDefaultAnswer, nil)
  106. if errMenu != nil || len(toEdit) == 0 {
  107. return errMenu
  108. }
  109. if errEdit := editPkgbuilds(buildDir, toEdit, editorConfig, editorFlags, srcinfos, noConfirm); errEdit != nil {
  110. return errEdit
  111. }
  112. fmt.Println()
  113. if !text.ContinueTask(os.Stdin, gotext.Get("Proceed with install?"), true, false) {
  114. return settings.ErrUserAbort{}
  115. }
  116. return nil
  117. }