2020-07-05 02:01:08 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-13 07:27:24 +02:00
|
|
|
"context"
|
2020-07-05 02:01:08 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-01-30 12:52:50 +01:00
|
|
|
"net/http"
|
2020-07-05 02:01:08 +02:00
|
|
|
"os"
|
2021-08-31 01:40:43 +02:00
|
|
|
"os/exec"
|
2020-08-21 02:39:52 +02:00
|
|
|
"path/filepath"
|
2020-08-22 01:24:52 +02:00
|
|
|
"strings"
|
2020-08-21 02:39:52 +02:00
|
|
|
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2021-05-13 07:27:24 +02:00
|
|
|
"github.com/Jguer/aur"
|
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/exe"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/parser"
|
2021-10-15 19:22:28 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/vcs"
|
2020-07-05 02:01:08 +02:00
|
|
|
)
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// HideMenus indicates if pacman's provider menus must be hidden.
|
2020-07-10 02:36:45 +02:00
|
|
|
var HideMenus = false
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// NoConfirm indicates if user input should be skipped.
|
2020-08-19 00:17:35 +02:00
|
|
|
var NoConfirm = false
|
|
|
|
|
2020-07-05 02:01:08 +02:00
|
|
|
// Configuration stores yay's config.
|
|
|
|
type Configuration struct {
|
2020-07-05 03:17:35 +02:00
|
|
|
AURURL string `json:"aururl"`
|
|
|
|
BuildDir string `json:"buildDir"`
|
|
|
|
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"`
|
|
|
|
SudoBin string `json:"sudobin"`
|
|
|
|
SudoFlags string `json:"sudoflags"`
|
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
CompletionInterval int `json:"completionrefreshtime"`
|
2021-11-07 00:15:50 +01:00
|
|
|
BottomUp bool `json:"bottomup"`
|
2020-07-05 03:17:35 +02:00
|
|
|
SudoLoop bool `json:"sudoloop"`
|
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
|
|
|
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"`
|
2021-10-29 22:59:06 +02:00
|
|
|
SingleLineResults bool `json:"singlelineresults"`
|
2022-03-07 00:48:15 +01:00
|
|
|
SeparateSources bool `json:"separatesources"`
|
2020-07-05 03:17:35 +02:00
|
|
|
Runtime *Runtime `json:"-"`
|
2022-03-05 00:30:15 +01:00
|
|
|
Version string `json:"version"`
|
2020-07-05 02:01:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveConfig writes yay config to file.
|
2020-08-21 02:39:52 +02:00
|
|
|
func (c *Configuration) Save(configPath string) error {
|
2022-03-05 00:30:15 +01:00
|
|
|
c.Version = c.Runtime.Version
|
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
marshalledinfo, err := json.MarshalIndent(c, "", "\t")
|
2020-07-05 02:01:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-21 02:39:52 +02:00
|
|
|
|
2020-08-07 23:59:55 +02:00
|
|
|
// https://github.com/Jguer/yay/issues/1325
|
|
|
|
marshalledinfo = append(marshalledinfo, '\n')
|
2020-10-26 11:07:03 +01:00
|
|
|
// https://github.com/Jguer/yay/issues/1399
|
2020-11-01 09:09:38 +01:00
|
|
|
if _, err = os.Stat(filepath.Dir(configPath)); os.IsNotExist(err) && err != nil {
|
|
|
|
if mkErr := os.MkdirAll(filepath.Dir(configPath), 0o755); mkErr != nil {
|
2020-10-26 11:07:03 +01:00
|
|
|
return mkErr
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-06 02:02:12 +02:00
|
|
|
in, err := os.OpenFile(configPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
|
2020-07-05 02:01:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-05 02:01:08 +02:00
|
|
|
if _, err = in.Write(marshalledinfo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-05 02:01:08 +02:00
|
|
|
return in.Sync()
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
func (c *Configuration) expandEnv() {
|
|
|
|
c.AURURL = os.ExpandEnv(c.AURURL)
|
|
|
|
c.BuildDir = os.ExpandEnv(c.BuildDir)
|
|
|
|
c.Editor = os.ExpandEnv(c.Editor)
|
|
|
|
c.EditorFlags = os.ExpandEnv(c.EditorFlags)
|
|
|
|
c.MakepkgBin = os.ExpandEnv(c.MakepkgBin)
|
|
|
|
c.MakepkgConf = os.ExpandEnv(c.MakepkgConf)
|
|
|
|
c.PacmanBin = os.ExpandEnv(c.PacmanBin)
|
|
|
|
c.PacmanConf = os.ExpandEnv(c.PacmanConf)
|
|
|
|
c.GpgFlags = os.ExpandEnv(c.GpgFlags)
|
|
|
|
c.MFlags = os.ExpandEnv(c.MFlags)
|
|
|
|
c.GitFlags = os.ExpandEnv(c.GitFlags)
|
|
|
|
c.SortBy = os.ExpandEnv(c.SortBy)
|
|
|
|
c.SearchBy = os.ExpandEnv(c.SearchBy)
|
|
|
|
c.GitBin = os.ExpandEnv(c.GitBin)
|
|
|
|
c.GpgBin = os.ExpandEnv(c.GpgBin)
|
|
|
|
c.SudoBin = os.ExpandEnv(c.SudoBin)
|
|
|
|
c.SudoFlags = os.ExpandEnv(c.SudoFlags)
|
|
|
|
c.ReDownload = os.ExpandEnv(c.ReDownload)
|
|
|
|
c.ReBuild = os.ExpandEnv(c.ReBuild)
|
|
|
|
c.AnswerClean = os.ExpandEnv(c.AnswerClean)
|
|
|
|
c.AnswerDiff = os.ExpandEnv(c.AnswerDiff)
|
|
|
|
c.AnswerEdit = os.ExpandEnv(c.AnswerEdit)
|
|
|
|
c.AnswerUpgrade = os.ExpandEnv(c.AnswerUpgrade)
|
|
|
|
c.RemoveMake = os.ExpandEnv(c.RemoveMake)
|
2020-07-05 02:01:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
func (c *Configuration) String() string {
|
2020-07-05 02:01:08 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
enc.SetIndent("", "\t")
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
if err := enc.Encode(c); err != nil {
|
2020-07-05 02:01:08 +02:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-05 02:01:08 +02:00
|
|
|
return buf.String()
|
|
|
|
}
|
2020-07-05 03:28:57 +02:00
|
|
|
|
2021-08-31 01:40:43 +02:00
|
|
|
// check privilege elevator exists otherwise try to find another one.
|
|
|
|
func (c *Configuration) setPrivilegeElevator() error {
|
2022-02-08 09:39:41 +01:00
|
|
|
if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
|
|
|
|
c.SudoBin = auth
|
|
|
|
if auth != "sudo" {
|
|
|
|
c.SudoFlags = ""
|
|
|
|
c.SudoLoop = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 01:40:43 +02:00
|
|
|
for _, bin := range [...]string{c.SudoBin, "sudo"} {
|
|
|
|
if _, err := exec.LookPath(bin); err == nil {
|
|
|
|
c.SudoBin = bin
|
|
|
|
return nil // wrapper or sudo command existing. Retrocompatiblity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SudoFlags = ""
|
|
|
|
c.SudoLoop = false
|
|
|
|
|
|
|
|
for _, bin := range [...]string{"doas", "pkexec", "su"} {
|
|
|
|
if _, err := exec.LookPath(bin); err == nil {
|
|
|
|
c.SudoBin = bin
|
|
|
|
return nil // command existing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ErrPrivilegeElevatorNotFound{confValue: c.SudoBin}
|
|
|
|
}
|
|
|
|
|
2022-03-05 00:30:15 +01:00
|
|
|
func DefaultConfig(version string) *Configuration {
|
2020-08-21 02:39:52 +02:00
|
|
|
return &Configuration{
|
2020-07-05 03:28:57 +02:00
|
|
|
AURURL: "https://aur.archlinux.org",
|
2020-08-21 02:39:52 +02:00
|
|
|
BuildDir: os.ExpandEnv("$HOME/.cache/yay"),
|
2020-07-05 03:28:57 +02:00
|
|
|
CleanAfter: false,
|
|
|
|
Editor: "",
|
|
|
|
EditorFlags: "",
|
|
|
|
Devel: false,
|
|
|
|
MakepkgBin: "makepkg",
|
|
|
|
MakepkgConf: "",
|
|
|
|
PacmanBin: "pacman",
|
|
|
|
PGPFetch: true,
|
|
|
|
PacmanConf: "/etc/pacman.conf",
|
|
|
|
GpgFlags: "",
|
|
|
|
MFlags: "",
|
|
|
|
GitFlags: "",
|
2021-11-07 00:15:50 +01:00
|
|
|
BottomUp: true,
|
2020-07-05 03:28:57 +02:00
|
|
|
CompletionInterval: 7,
|
|
|
|
SortBy: "votes",
|
|
|
|
SearchBy: "name-desc",
|
|
|
|
SudoLoop: false,
|
|
|
|
GitBin: "git",
|
|
|
|
GpgBin: "gpg",
|
|
|
|
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,
|
2022-03-07 00:48:15 +01:00
|
|
|
SeparateSources: false,
|
2022-03-05 00:30:15 +01:00
|
|
|
Version: version,
|
2020-07-05 03:28:57 +02:00
|
|
|
}
|
2020-08-21 02:39:52 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 07:27:24 +02:00
|
|
|
func NewConfig(version string) (*Configuration, error) {
|
2022-03-05 00:30:15 +01:00
|
|
|
newConfig := DefaultConfig(version)
|
2020-08-21 02:39:52 +02:00
|
|
|
|
2021-10-15 19:22:28 +02:00
|
|
|
cacheHome, errCache := getCacheHome()
|
|
|
|
if errCache != nil {
|
|
|
|
text.Errorln(errCache)
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
newConfig.BuildDir = cacheHome
|
|
|
|
|
|
|
|
configPath := getConfigPath()
|
|
|
|
newConfig.load(configPath)
|
|
|
|
|
2021-10-15 19:55:38 +02:00
|
|
|
if aurdest := os.Getenv("AURDEST"); aurdest != "" {
|
|
|
|
newConfig.BuildDir = aurdest
|
|
|
|
}
|
|
|
|
|
|
|
|
newConfig.expandEnv()
|
|
|
|
|
2021-10-15 19:22:28 +02:00
|
|
|
if newConfig.BuildDir != systemdCache {
|
|
|
|
errBuildDir := initDir(newConfig.BuildDir)
|
|
|
|
if errBuildDir != nil {
|
|
|
|
return nil, errBuildDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 23:51:07 +02:00
|
|
|
if errPE := newConfig.setPrivilegeElevator(); errPE != nil {
|
2021-08-31 01:40:43 +02:00
|
|
|
return nil, errPE
|
|
|
|
}
|
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
newConfig.Runtime = &Runtime{
|
2020-09-03 18:26:57 +02:00
|
|
|
ConfigPath: configPath,
|
2022-03-05 00:30:15 +01:00
|
|
|
Version: version,
|
2021-08-08 00:08:04 +02:00
|
|
|
Mode: parser.ModeAny,
|
2020-08-21 02:39:52 +02:00
|
|
|
SaveConfig: false,
|
|
|
|
CompletionPath: filepath.Join(cacheHome, completionFileName),
|
2021-08-08 20:22:39 +02:00
|
|
|
CmdBuilder: newConfig.CmdBuilder(nil),
|
2021-08-08 00:57:55 +02:00
|
|
|
PacmanConf: nil,
|
|
|
|
VCSStore: nil,
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
AURClient: nil,
|
2021-05-13 07:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var errAUR error
|
|
|
|
|
|
|
|
newConfig.Runtime.AURClient, errAUR = aur.NewClient(aur.WithHTTPClient(newConfig.Runtime.HTTPClient),
|
|
|
|
aur.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
|
|
|
|
req.Header.Set("User-Agent", fmt.Sprintf("Yay/%s", version))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
if errAUR != nil {
|
|
|
|
return nil, errAUR
|
2020-07-05 03:28:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 20:22:39 +02:00
|
|
|
newConfig.Runtime.VCSStore = vcs.NewInfoStore(
|
|
|
|
filepath.Join(cacheHome, vcsFileName), newConfig.Runtime.CmdBuilder)
|
2020-08-21 02:39:52 +02:00
|
|
|
|
|
|
|
err := newConfig.Runtime.VCSStore.Load()
|
|
|
|
|
|
|
|
return newConfig, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) load(configPath string) {
|
|
|
|
cfile, err := os.Open(configPath)
|
|
|
|
if !os.IsNotExist(err) && err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr,
|
|
|
|
gotext.Get("failed to open config file '%s': %s", configPath, err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cfile.Close()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
decoder := json.NewDecoder(cfile)
|
|
|
|
if err = decoder.Decode(c); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr,
|
|
|
|
gotext.Get("failed to read config file '%s': %s", configPath, err))
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 03:28:57 +02:00
|
|
|
}
|
2021-08-08 00:57:55 +02:00
|
|
|
|
2021-08-08 20:22:39 +02:00
|
|
|
func (c *Configuration) CmdBuilder(runner exe.Runner) exe.ICmdBuilder {
|
|
|
|
if runner == nil {
|
|
|
|
runner = &exe.OSRunner{}
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-08 00:57:55 +02:00
|
|
|
return &exe.CmdBuilder{
|
|
|
|
GitBin: c.GitBin,
|
|
|
|
GitFlags: strings.Fields(c.GitFlags),
|
|
|
|
MakepkgFlags: strings.Fields(c.MFlags),
|
|
|
|
MakepkgConfPath: c.MakepkgConf,
|
|
|
|
MakepkgBin: c.MakepkgBin,
|
|
|
|
SudoBin: c.SudoBin,
|
|
|
|
SudoFlags: strings.Fields(c.SudoFlags),
|
2021-08-31 01:40:43 +02:00
|
|
|
SudoLoopEnabled: c.SudoLoop,
|
2021-08-08 00:57:55 +02:00
|
|
|
PacmanBin: c.PacmanBin,
|
|
|
|
PacmanConfigPath: c.PacmanConf,
|
2021-08-08 20:22:39 +02:00
|
|
|
PacmanDBPath: "",
|
|
|
|
Runner: runner,
|
2021-08-08 00:57:55 +02:00
|
|
|
}
|
|
|
|
}
|