2018-09-15 19:17:03 +02:00
|
|
|
package main // import "github.com/Jguer/yay"
|
2018-03-22 16:46:48 +01:00
|
|
|
|
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2018-03-22 17:39:27 +01:00
|
|
|
"fmt"
|
2018-03-22 16:46:48 +01:00
|
|
|
"os"
|
2021-02-13 18:40:28 +01:00
|
|
|
"os/exec"
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2019-06-17 14:22:30 +02:00
|
|
|
pacmanconf "github.com/Morganamilo/go-pacmanconf"
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2020-12-10 00:18:23 +01:00
|
|
|
"golang.org/x/term"
|
2020-05-04 09:24:32 +02:00
|
|
|
|
2021-09-08 22:28:08 +02:00
|
|
|
"github.com/Jguer/yay/v11/pkg/db"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/db/ialpm"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/settings/parser"
|
|
|
|
"github.com/Jguer/yay/v11/pkg/text"
|
2018-03-22 16:46:48 +01:00
|
|
|
)
|
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
func initGotext() {
|
2020-05-08 18:13:51 +02:00
|
|
|
if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
|
|
|
|
localePath = envLocalePath
|
|
|
|
}
|
|
|
|
|
2021-08-20 18:30:08 +02:00
|
|
|
if lc := os.Getenv("LANGUAGE"); lc != "" {
|
|
|
|
gotext.Configure(localePath, lc, "yay")
|
|
|
|
} else if lc := os.Getenv("LC_ALL"); lc != "" {
|
|
|
|
gotext.Configure(localePath, lc, "yay")
|
|
|
|
} else if lc := os.Getenv("LC_MESSAGES"); lc != "" {
|
|
|
|
gotext.Configure(localePath, lc, "yay")
|
2021-01-29 22:45:42 +01:00
|
|
|
} else {
|
|
|
|
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
|
|
|
|
}
|
2020-05-04 09:24:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 00:08:04 +02:00
|
|
|
func initAlpm(cmdArgs *parser.Arguments, pacmanConfigPath string) (*pacmanconf.Config, bool, error) {
|
2018-09-04 16:47:22 +02:00
|
|
|
root := "/"
|
2020-07-05 02:45:23 +02:00
|
|
|
if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
root = value
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-05 15:34:27 +02:00
|
|
|
pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
|
2018-09-04 16:47:22 +02:00
|
|
|
if err != nil {
|
2020-12-23 18:45:24 +01:00
|
|
|
cmdErr := err
|
|
|
|
if stderr != "" {
|
|
|
|
cmdErr = fmt.Errorf("%s\n%s", err, stderr)
|
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-12-23 18:45:24 +01:00
|
|
|
return nil, false, cmdErr
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:08:47 +02:00
|
|
|
if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
|
|
|
|
pacmanConf.DBPath = dbPath
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 11:10:25 +02:00
|
|
|
if arch := cmdArgs.GetArgs("arch"); arch != nil {
|
|
|
|
pacmanConf.Architecture = append(pacmanConf.Architecture, arch...)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:00:28 +02:00
|
|
|
if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
|
|
|
|
pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:08:47 +02:00
|
|
|
if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
|
|
|
|
pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:08:47 +02:00
|
|
|
if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
|
|
|
|
pacmanConf.CacheDir = cacheArray
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 23:08:47 +02:00
|
|
|
if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
|
|
|
|
pacmanConf.GPGDir = gpgDir
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 00:18:23 +01:00
|
|
|
useColor := pacmanConf.Color && term.IsTerminal(int(os.Stdout.Fd()))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-05 02:45:23 +02:00
|
|
|
switch value, _, _ := cmdArgs.GetArg("color"); value {
|
2019-03-04 17:07:04 +01:00
|
|
|
case "always":
|
2020-08-08 18:43:37 +02:00
|
|
|
useColor = true
|
2019-03-04 17:07:04 +01:00
|
|
|
case "auto":
|
2020-12-10 00:18:23 +01:00
|
|
|
useColor = term.IsTerminal(int(os.Stdout.Fd()))
|
2019-03-04 17:07:04 +01:00
|
|
|
case "never":
|
2020-08-08 18:43:37 +02:00
|
|
|
useColor = false
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return pacmanConf, useColor, nil
|
2018-06-22 15:44:38 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
func main() {
|
2021-08-12 18:56:23 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ctx = context.Background()
|
|
|
|
ret = 0
|
|
|
|
)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
defer func() { os.Exit(ret) }()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
initGotext()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
if os.Geteuid() == 0 {
|
|
|
|
text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
|
|
|
|
}
|
|
|
|
|
2021-05-13 07:27:24 +02:00
|
|
|
config, err = settings.NewConfig(yayVersion)
|
2020-07-05 16:58:35 +02:00
|
|
|
if err != nil {
|
2020-08-08 18:43:37 +02:00
|
|
|
if str := err.Error(); str != "" {
|
2021-08-31 01:40:43 +02:00
|
|
|
text.Errorln(str)
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return
|
2018-06-22 15:44:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 00:08:04 +02:00
|
|
|
cmdArgs := parser.MakeArguments()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
|
|
|
if err = config.ParseCommandLine(cmdArgs); err != nil {
|
2020-08-08 18:43:37 +02:00
|
|
|
if str := err.Error(); str != "" {
|
2021-08-31 01:40:43 +02:00
|
|
|
text.Errorln(str)
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return
|
2018-09-04 16:47:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
if config.Runtime.SaveConfig {
|
2020-11-01 09:09:38 +01:00
|
|
|
if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
|
2021-08-31 01:40:43 +02:00
|
|
|
text.Errorln(errS)
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
var useColor bool
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
|
|
|
|
if err != nil {
|
|
|
|
if str := err.Error(); str != "" {
|
2021-08-31 01:40:43 +02:00
|
|
|
text.Errorln(str)
|
2018-03-23 20:49:51 +01:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2021-08-08 00:57:55 +02:00
|
|
|
config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
text.UseColor = useColor
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2020-08-21 02:39:52 +02:00
|
|
|
dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
|
2020-08-08 18:43:37 +02:00
|
|
|
if err != nil {
|
|
|
|
if str := err.Error(); str != "" {
|
2021-08-31 01:40:43 +02:00
|
|
|
text.Errorln(str)
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
defer dbExecutor.Cleanup()
|
2021-02-13 18:40:28 +01:00
|
|
|
|
2021-08-21 16:17:19 +02:00
|
|
|
if err = handleCmd(ctx, cmdArgs, db.Executor(dbExecutor)); err != nil {
|
|
|
|
if str := err.Error(); str != "" {
|
|
|
|
text.Errorln(str)
|
2019-10-13 13:15:51 +02:00
|
|
|
}
|
2021-02-13 18:40:28 +01:00
|
|
|
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
// mirror pacman exit code when applicable
|
|
|
|
ret = exitError.ExitCode()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
return
|
2018-09-05 00:05:35 +02:00
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|