2017-08-02 17:24:03 +00:00
|
|
|
package main
|
2017-05-06 16:34:40 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-04 20:26:07 +00:00
|
|
|
"bufio"
|
2017-05-06 16:34:40 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-04-09 19:50:18 +00:00
|
|
|
"strings"
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2019-04-23 15:53:20 +00:00
|
|
|
alpm "github.com/Jguer/go-alpm"
|
2020-07-05 13:34:27 +00:00
|
|
|
"github.com/Morganamilo/go-pacmanconf"
|
2020-05-04 07:24:32 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2020-07-05 00:01:08 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/settings"
|
2020-06-13 17:29:50 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2017-05-06 16:34:40 +00:00
|
|
|
)
|
|
|
|
|
2017-05-07 01:43:49 +00:00
|
|
|
// Verbosity settings for search
|
|
|
|
const (
|
2018-09-14 19:18:59 +00:00
|
|
|
numberMenu = iota
|
|
|
|
detailed
|
|
|
|
minimal
|
2018-11-29 20:42:06 +00:00
|
|
|
)
|
2017-05-07 01:43:49 +00:00
|
|
|
|
2020-06-13 17:29:50 +00:00
|
|
|
var yayVersion = "10.0.0"
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2020-05-29 22:43:18 +00:00
|
|
|
var localePath = "/usr/share/locale"
|
|
|
|
|
2018-03-05 22:15:34 +00:00
|
|
|
// savedInfo holds the current vcs info
|
|
|
|
var savedInfo vcsInfo
|
2017-07-06 07:30:48 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// YayConf holds the current config values for yay.
|
2020-07-05 00:01:08 +00:00
|
|
|
var config *settings.Configuration
|
2017-05-06 16:34:40 +00:00
|
|
|
|
|
|
|
// Editor returns the preferred system editor.
|
2020-05-02 14:17:20 +00:00
|
|
|
func editor() (editor string, args []string) {
|
2017-05-06 16:34:40 +00:00
|
|
|
switch {
|
2017-08-04 09:26:53 +00:00
|
|
|
case config.Editor != "":
|
|
|
|
editor, err := exec.LookPath(config.Editor)
|
2017-05-06 16:34:40 +00:00
|
|
|
if err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2017-05-06 16:34:40 +00:00
|
|
|
} else {
|
2018-04-09 19:50:18 +00:00
|
|
|
return editor, strings.Fields(config.EditorFlags)
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("EDITOR") != "":
|
2020-04-12 13:38:44 +00:00
|
|
|
if editorArgs := strings.Fields(os.Getenv("EDITOR")); len(editorArgs) != 0 {
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
} else {
|
|
|
|
return editor, editorArgs[1:]
|
|
|
|
}
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("VISUAL") != "":
|
2020-04-12 13:38:44 +00:00
|
|
|
if editorArgs := strings.Fields(os.Getenv("VISUAL")); len(editorArgs) != 0 {
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
} else {
|
|
|
|
return editor, editorArgs[1:]
|
|
|
|
}
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr)
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Errorln(gotext.Get("%s is not set", bold(cyan("$EDITOR"))))
|
|
|
|
text.Warnln(gotext.Get("Add %s or %s to your environment variables", bold(cyan("$EDITOR")), bold(cyan("$VISUAL"))))
|
2018-04-09 20:02:44 +00:00
|
|
|
|
|
|
|
for {
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Infoln(gotext.Get("Edit PKGBUILD with?"))
|
2018-04-09 20:02:44 +00:00
|
|
|
editorInput, err := getInput("")
|
|
|
|
if err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-04-09 20:02:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
editorArgs := strings.Fields(editorInput)
|
2019-10-12 23:48:12 +00:00
|
|
|
if len(editorArgs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-09 20:02:44 +00:00
|
|
|
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-04-09 20:02:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return editor, editorArgs[1:]
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 20:26:07 +00:00
|
|
|
func getInput(defaultValue string) (string, error) {
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Info()
|
2018-04-04 21:22:33 +00:00
|
|
|
if defaultValue != "" || config.NoConfirm {
|
2018-04-04 20:26:07 +00:00
|
|
|
fmt.Println(defaultValue)
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
buf, overflow, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if overflow {
|
2020-05-04 07:24:32 +00:00
|
|
|
return "", fmt.Errorf(gotext.Get("input too long"))
|
2018-04-04 20:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2018-09-04 14:47:22 +00:00
|
|
|
func toUsage(usages []string) alpm.Usage {
|
|
|
|
if len(usages) == 0 {
|
|
|
|
return alpm.UsageAll
|
|
|
|
}
|
|
|
|
|
2018-09-14 19:18:59 +00:00
|
|
|
var ret alpm.Usage
|
2018-09-04 14:47:22 +00:00
|
|
|
for _, usage := range usages {
|
|
|
|
switch usage {
|
|
|
|
case "Sync":
|
|
|
|
ret |= alpm.UsageSync
|
|
|
|
case "Search":
|
|
|
|
ret |= alpm.UsageSearch
|
|
|
|
case "Install":
|
|
|
|
ret |= alpm.UsageInstall
|
|
|
|
case "Upgrade":
|
|
|
|
ret |= alpm.UsageUpgrade
|
|
|
|
case "All":
|
|
|
|
ret |= alpm.UsageAll
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
func configureAlpm(pacmanConf *pacmanconf.Config, alpmHandle *alpm.Handle) error {
|
2018-09-04 14:47:22 +00:00
|
|
|
// TODO: set SigLevel
|
2020-05-02 14:17:20 +00:00
|
|
|
// sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
|
|
|
|
// localFileSigLevel := alpm.SigUseDefault
|
|
|
|
// remoteFileSigLevel := alpm.SigUseDefault
|
2018-09-04 14:47:22 +00:00
|
|
|
|
|
|
|
for _, repo := range pacmanConf.Repos {
|
|
|
|
// TODO: set SigLevel
|
2019-02-04 16:56:02 +00:00
|
|
|
db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
|
2018-09-04 14:47:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.SetServers(repo.Servers)
|
|
|
|
db.SetUsage(toUsage(repo.Usage))
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetCacheDirs(pacmanConf.CacheDir); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add hook directories 1-by-1 to avoid overwriting the system directory
|
|
|
|
for _, dir := range pacmanConf.HookDir {
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.AddHookDir(dir); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
/*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
2018-09-19 18:52:58 +00:00
|
|
|
}*/
|
2018-09-04 14:47:22 +00:00
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
|
2018-09-04 14:47:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:10:04 +00:00
|
|
|
return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
|
2018-09-04 14:47:22 +00:00
|
|
|
}
|