2021-10-11 00:48:33 +02:00
|
|
|
// edit menu
|
2021-10-11 22:22:03 +02:00
|
|
|
package menus
|
2021-10-11 00:48:33 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
gosrc "github.com/Morganamilo/go-srcinfo"
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
|
|
|
"github.com/Jguer/yay/v11/pkg/dep"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/stringset"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Editor returns the preferred system editor.
|
2021-10-11 22:22:03 +02:00
|
|
|
func editor(editorConfig, editorFlags string, noConfirm bool) (editor string, args []string) {
|
2021-10-11 00:48:33 +02:00
|
|
|
switch {
|
2021-10-11 22:22:03 +02:00
|
|
|
case editorConfig != "":
|
|
|
|
editor, err := exec.LookPath(editorConfig)
|
2021-10-11 00:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
} else {
|
2021-10-11 22:22:03 +02:00
|
|
|
return editor, strings.Fields(editorFlags)
|
2021-10-11 00:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
2021-11-23 21:32:35 +01:00
|
|
|
case os.Getenv("VISUAL") != "":
|
|
|
|
if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
|
2021-10-11 00:48:33 +02:00
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
} else {
|
|
|
|
return editor, editorArgs[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
2021-11-23 21:32:35 +01:00
|
|
|
case os.Getenv("EDITOR") != "":
|
|
|
|
if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
|
2021-10-11 00:48:33 +02:00
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
} else {
|
|
|
|
return editor, editorArgs[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
fmt.Fprintln(os.Stderr)
|
|
|
|
text.Errorln(gotext.Get("%s is not set", text.Bold(text.Cyan("$EDITOR"))))
|
|
|
|
text.Warnln(gotext.Get("Add %s or %s to your environment variables", text.Bold(text.Cyan("$EDITOR")), text.Bold(text.Cyan("$VISUAL"))))
|
|
|
|
|
|
|
|
for {
|
|
|
|
text.Infoln(gotext.Get("Edit PKGBUILD with?"))
|
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
editorInput, err := text.GetInput("", noConfirm)
|
2021-10-11 00:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
editorArgs := strings.Fields(editorInput)
|
|
|
|
if len(editorArgs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return editor, editorArgs[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
func editPkgbuilds(buildDir string, bases []dep.Base, editorConfig,
|
|
|
|
editorFlags string, srcinfos map[string]*gosrc.Srcinfo, noConfirm bool) error {
|
2021-10-11 00:48:33 +02:00
|
|
|
pkgbuilds := make([]string, 0, len(bases))
|
|
|
|
|
|
|
|
for _, base := range bases {
|
|
|
|
pkg := base.Pkgbase()
|
2021-10-11 22:22:03 +02:00
|
|
|
dir := filepath.Join(buildDir, pkg)
|
2021-10-11 00:48:33 +02:00
|
|
|
pkgbuilds = append(pkgbuilds, filepath.Join(dir, "PKGBUILD"))
|
|
|
|
|
|
|
|
for _, splitPkg := range srcinfos[pkg].SplitPackages() {
|
|
|
|
if splitPkg.Install != "" {
|
|
|
|
pkgbuilds = append(pkgbuilds, filepath.Join(dir, splitPkg.Install))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pkgbuilds) > 0 {
|
2021-10-11 22:22:03 +02:00
|
|
|
editor, editorArgs := editor(editorConfig, editorFlags, noConfirm)
|
2021-10-11 00:48:33 +02:00
|
|
|
editorArgs = append(editorArgs, pkgbuilds...)
|
|
|
|
editcmd := exec.Command(editor, editorArgs...)
|
|
|
|
editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
|
|
|
|
if err := editcmd.Run(); err != nil {
|
|
|
|
return errors.New(gotext.Get("editor did not exit successfully, aborting: %s", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
func Edit(editMenuOption bool, buildDir string, bases []dep.Base, editorConfig,
|
|
|
|
editorFlags string, installed stringset.StringSet, srcinfos map[string]*gosrc.Srcinfo,
|
|
|
|
noConfirm bool, editDefaultAnswer string) error {
|
2021-10-11 00:48:33 +02:00
|
|
|
if !editMenuOption {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:08:18 +02:00
|
|
|
toEdit, errMenu := selectionMenu(buildDir, bases,
|
|
|
|
installed, gotext.Get("PKGBUILDs to edit?"), noConfirm, editDefaultAnswer, nil)
|
2021-10-11 00:48:33 +02:00
|
|
|
if errMenu != nil || len(toEdit) == 0 {
|
|
|
|
return errMenu
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:22:03 +02:00
|
|
|
if errEdit := editPkgbuilds(buildDir, toEdit, editorConfig, editorFlags, srcinfos, noConfirm); errEdit != nil {
|
2021-10-11 00:48:33 +02:00
|
|
|
return errEdit
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if !text.ContinueTask(gotext.Get("Proceed with install?"), true, false) {
|
|
|
|
return settings.ErrUserAbort{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|