yay/config.go

448 lines
12 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
2019-04-23 15:53:20 +00:00
alpm "github.com/Jguer/go-alpm"
pacmanconf "github.com/Morganamilo/go-pacmanconf"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v9/pkg/text"
)
2017-05-07 01:43:49 +00:00
// Verbosity settings for search
const (
numberMenu = iota
detailed
minimal
2018-11-29 20:42:06 +00:00
)
2017-05-07 01:43:49 +00:00
2018-11-29 20:42:06 +00:00
const (
// Describes Sorting method for numberdisplay
bottomUp = iota
topDown
2018-11-29 20:42:06 +00:00
)
2018-11-29 20:42:06 +00:00
const (
modeAUR targetMode = iota
modeRepo
modeAny
2017-05-07 01:43:49 +00:00
)
type targetMode int
2017-05-07 01:43:49 +00:00
// Configuration stores yay's config.
type Configuration struct {
2018-08-19 04:05:16 +00:00
AURURL string `json:"aururl"`
BuildDir string `json:"buildDir"`
ABSDir string `json:"absdir"`
Editor string `json:"editor"`
EditorFlags string `json:"editorflags"`
MakepkgBin string `json:"makepkgbin"`
MakepkgConf string `json:"makepkgconf"`
PacmanBin string `json:"pacmanbin"`
PacmanConf string `json:"pacmanconf"`
ReDownload string `json:"redownload"`
ReBuild string `json:"rebuild"`
AnswerClean string `json:"answerclean"`
AnswerDiff string `json:"answerdiff"`
AnswerEdit string `json:"answeredit"`
AnswerUpgrade string `json:"answerupgrade"`
GitBin string `json:"gitbin"`
GpgBin string `json:"gpgbin"`
GpgFlags string `json:"gpgflags"`
MFlags string `json:"mflags"`
SortBy string `json:"sortby"`
SearchBy string `json:"searchby"`
GitFlags string `json:"gitflags"`
RemoveMake string `json:"removemake"`
2019-03-19 21:10:58 +00:00
SudoBin string `json:"sudobin"`
SudoFlags string `json:"sudoflags"`
RequestSplitN int `json:"requestsplitn"`
SearchMode int `json:"-"`
SortMode int `json:"sortmode"`
CompletionInterval int `json:"completionrefreshtime"`
SudoLoop bool `json:"sudoloop"`
TimeUpdate bool `json:"timeupdate"`
NoConfirm bool `json:"-"`
Devel bool `json:"devel"`
CleanAfter bool `json:"cleanAfter"`
Provides bool `json:"provides"`
PGPFetch bool `json:"pgpfetch"`
UpgradeMenu bool `json:"upgrademenu"`
CleanMenu bool `json:"cleanmenu"`
DiffMenu bool `json:"diffmenu"`
EditMenu bool `json:"editmenu"`
CombinedUpgrade bool `json:"combinedupgrade"`
UseAsk bool `json:"useask"`
BatchInstall bool `json:"batchinstall"`
}
var yayVersion = "9.4.3"
2020-05-29 22:43:18 +00:00
var localePath = "/usr/share/locale"
// configFileName holds the name of the config file.
const configFileName string = "config.json"
// vcsFileName holds the name of the vcs file.
const vcsFileName string = "vcs.json"
// useColor enables/disables colored printing
var useColor bool
// configHome handles config directory home
var configHome string
// cacheHome handles cache home
var cacheHome string
// savedInfo holds the current vcs info
var savedInfo vcsInfo
// configfile holds yay config file path.
var configFile string
// vcsfile holds yay vcs info file path.
var vcsFile string
// shouldSaveConfig holds whether or not the config should be saved
var shouldSaveConfig bool
// YayConf holds the current config values for yay.
var config *Configuration
// AlpmConf holds the current config values for pacman.
var pacmanConf *pacmanconf.Config
// AlpmHandle is the alpm handle used by yay.
var alpmHandle *alpm.Handle
// Mode is used to restrict yay to AUR or repo only modes
var mode = modeAny
var hideMenus = false
2017-05-07 01:43:49 +00:00
// SaveConfig writes yay config to file.
func (config *Configuration) saveConfig() error {
2020-01-10 01:35:02 +00:00
marshalledinfo, err := json.MarshalIndent(config, "", "\t")
if err != nil {
return err
}
in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
2017-05-07 01:43:49 +00:00
if err != nil {
return err
}
defer in.Close()
2020-01-10 01:35:02 +00:00
if _, err = in.Write(marshalledinfo); err != nil {
2017-05-07 01:43:49 +00:00
return err
}
2020-01-10 01:35:02 +00:00
return in.Sync()
2017-05-07 01:43:49 +00:00
}
func defaultSettings() *Configuration {
newConfig := &Configuration{
AURURL: "https://aur.archlinux.org",
BuildDir: "$HOME/.cache/yay",
ABSDir: "$HOME/.cache/yay/abs",
CleanAfter: false,
Editor: "",
EditorFlags: "",
Devel: false,
MakepkgBin: "makepkg",
MakepkgConf: "",
NoConfirm: false,
PacmanBin: "pacman",
PGPFetch: true,
PacmanConf: "/etc/pacman.conf",
GpgFlags: "",
MFlags: "",
GitFlags: "",
SortMode: bottomUp,
CompletionInterval: 7,
SortBy: "votes",
SearchBy: "name-desc",
SudoLoop: false,
GitBin: "git",
GpgBin: "gpg",
2019-03-19 21:10:58 +00:00
SudoBin: "sudo",
SudoFlags: "",
TimeUpdate: false,
RequestSplitN: 150,
ReDownload: "no",
ReBuild: "no",
BatchInstall: false,
AnswerClean: "",
AnswerDiff: "",
AnswerEdit: "",
AnswerUpgrade: "",
RemoveMake: "ask",
Provides: true,
UpgradeMenu: true,
CleanMenu: true,
DiffMenu: true,
EditMenu: false,
UseAsk: false,
CombinedUpgrade: false,
}
if os.Getenv("XDG_CACHE_HOME") != "" {
newConfig.BuildDir = "$XDG_CACHE_HOME/yay"
2018-08-31 20:19:40 +00:00
}
return newConfig
}
2018-08-31 20:19:40 +00:00
func (config *Configuration) expandEnv() {
config.AURURL = os.ExpandEnv(config.AURURL)
2019-10-23 10:43:28 +00:00
config.ABSDir = os.ExpandEnv(config.ABSDir)
2018-08-31 20:19:40 +00:00
config.BuildDir = os.ExpandEnv(config.BuildDir)
config.Editor = os.ExpandEnv(config.Editor)
config.EditorFlags = os.ExpandEnv(config.EditorFlags)
config.MakepkgBin = os.ExpandEnv(config.MakepkgBin)
config.MakepkgConf = os.ExpandEnv(config.MakepkgConf)
config.PacmanBin = os.ExpandEnv(config.PacmanBin)
config.PacmanConf = os.ExpandEnv(config.PacmanConf)
config.GpgFlags = os.ExpandEnv(config.GpgFlags)
config.MFlags = os.ExpandEnv(config.MFlags)
config.GitFlags = os.ExpandEnv(config.GitFlags)
config.SortBy = os.ExpandEnv(config.SortBy)
config.SearchBy = os.ExpandEnv(config.SearchBy)
2018-08-31 20:19:40 +00:00
config.GitBin = os.ExpandEnv(config.GitBin)
config.GpgBin = os.ExpandEnv(config.GpgBin)
2019-03-19 21:10:58 +00:00
config.SudoBin = os.ExpandEnv(config.SudoBin)
config.SudoFlags = os.ExpandEnv(config.SudoFlags)
2018-08-31 20:19:40 +00:00
config.ReDownload = os.ExpandEnv(config.ReDownload)
config.ReBuild = os.ExpandEnv(config.ReBuild)
config.AnswerClean = os.ExpandEnv(config.AnswerClean)
config.AnswerDiff = os.ExpandEnv(config.AnswerDiff)
config.AnswerEdit = os.ExpandEnv(config.AnswerEdit)
config.AnswerUpgrade = os.ExpandEnv(config.AnswerUpgrade)
config.RemoveMake = os.ExpandEnv(config.RemoveMake)
}
// Editor returns the preferred system editor.
func editor() (editor string, args []string) {
switch {
2017-08-04 09:26:53 +00:00
case config.Editor != "":
editor, err := exec.LookPath(config.Editor)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
return editor, strings.Fields(config.EditorFlags)
}
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:]
}
}
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:]
}
}
fallthrough
default:
fmt.Fprintln(os.Stderr)
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"))))
for {
text.Infoln(gotext.Get("Edit PKGBUILD with?"))
editorInput, err := getInput("")
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:]
}
}
}
// ContinueTask prompts if user wants to continue task.
// If NoConfirm is set the action will continue without user input.
func continueTask(s string, cont bool) bool {
2017-08-04 09:26:53 +00:00
if config.NoConfirm {
return cont
}
var response string
var postFix string
yes := gotext.Get("yes")
no := gotext.Get("no")
y := string([]rune(yes)[0])
n := string([]rune(no)[0])
if cont {
postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
} else {
postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
}
text.Info(bold(s), bold(postFix))
if _, err := fmt.Scanln(&response); err != nil {
return cont
}
response = strings.ToLower(response)
return response == yes || response == y
}
func getInput(defaultValue string) (string, error) {
text.Info()
if defaultValue != "" || config.NoConfirm {
fmt.Println(defaultValue)
return defaultValue, nil
}
reader := bufio.NewReader(os.Stdin)
buf, overflow, err := reader.ReadLine()
if err != nil {
return "", err
}
if overflow {
return "", fmt.Errorf(gotext.Get("input too long"))
}
return string(buf), nil
}
func (config *Configuration) String() string {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", "\t")
if err := enc.Encode(config); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return buf.String()
}
func toUsage(usages []string) alpm.Usage {
if len(usages) == 0 {
return alpm.UsageAll
}
var ret alpm.Usage
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
}
func configureAlpm() error {
// TODO: set SigLevel
// sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
// localFileSigLevel := alpm.SigUseDefault
// remoteFileSigLevel := alpm.SigUseDefault
for _, repo := range pacmanConf.Repos {
// TODO: set SigLevel
2019-02-04 16:56:02 +00:00
db, err := alpmHandle.RegisterSyncDB(repo.Name, 0)
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 {
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 {
return err
}
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetNoExtracts(pacmanConf.NoExtract); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
/*if err := alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
return err
}*/
2019-03-05 20:10:04 +00:00
if err := alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
return err
}
2019-03-05 20:10:04 +00:00
return alpmHandle.SetCheckSpace(pacmanConf.CheckSpace)
}