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