edit_menu.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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) error {
  74. pkgbuilds := make([]string, 0, len(bases))
  75. for _, base := range bases {
  76. pkg := base.Pkgbase()
  77. dir := filepath.Join(buildDir, pkg)
  78. pkgbuilds = append(pkgbuilds, filepath.Join(dir, "PKGBUILD"))
  79. for _, splitPkg := range srcinfos[pkg].SplitPackages() {
  80. if splitPkg.Install != "" {
  81. pkgbuilds = append(pkgbuilds, filepath.Join(dir, splitPkg.Install))
  82. }
  83. }
  84. }
  85. if len(pkgbuilds) > 0 {
  86. editor, editorArgs := editor(editorConfig, editorFlags, noConfirm)
  87. editorArgs = append(editorArgs, pkgbuilds...)
  88. editcmd := exec.Command(editor, editorArgs...)
  89. editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  90. if err := editcmd.Run(); err != nil {
  91. return errors.New(gotext.Get("editor did not exit successfully, aborting: %s", err))
  92. }
  93. }
  94. return nil
  95. }
  96. func Edit(editMenuOption bool, buildDir string, bases []dep.Base, editorConfig,
  97. editorFlags string, installed stringset.StringSet, srcinfos map[string]*gosrc.Srcinfo,
  98. noConfirm bool, editDefaultAnswer string) error {
  99. if !editMenuOption {
  100. return nil
  101. }
  102. toEdit, errMenu := selectionMenu(buildDir, bases,
  103. installed, gotext.Get("PKGBUILDs to edit?"), noConfirm, editDefaultAnswer, nil)
  104. if errMenu != nil || len(toEdit) == 0 {
  105. return errMenu
  106. }
  107. if errEdit := editPkgbuilds(buildDir, toEdit, editorConfig, editorFlags, srcinfos, noConfirm); errEdit != nil {
  108. return errEdit
  109. }
  110. fmt.Println()
  111. if !text.ContinueTask(gotext.Get("Proceed with install?"), true, false) {
  112. return settings.ErrUserAbort{}
  113. }
  114. return nil
  115. }