1
0
mirror of https://github.com/Jguer/yay synced 2024-07-08 04:16:16 +00:00
yay/main.go

181 lines
3.7 KiB
Go
Raw Normal View History

2018-09-15 17:17:03 +00:00
package main // import "github.com/Jguer/yay"
2018-03-22 15:46:48 +00:00
import (
2021-08-12 16:56:23 +00:00
"context"
2018-03-22 16:39:27 +00:00
"fmt"
2018-03-22 15:46:48 +00:00
"os"
"os/exec"
2018-03-22 15:46:48 +00:00
pacmanconf "github.com/Morganamilo/go-pacmanconf"
"github.com/leonelquinteros/gotext"
"golang.org/x/term"
2021-09-08 20:28:08 +00: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 15:46:48 +00:00
)
func initGotext() {
2020-05-08 16:13:51 +00:00
if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
localePath = envLocalePath
}
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")
} else {
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
}
}
func initAlpm(cmdArgs *parser.Arguments, pacmanConfigPath string) (*pacmanconf.Config, bool, error) {
root := "/"
2020-07-05 00:45:23 +00:00
if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
root = value
2018-03-22 15:46:48 +00:00
}
pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
if err != nil {
cmdErr := err
if stderr != "" {
cmdErr = fmt.Errorf("%s\n%s", err, stderr)
}
2021-08-11 18:13:28 +00:00
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
}
if arch := cmdArgs.GetArgs("arch"); arch != nil {
pacmanConf.Architecture = append(pacmanConf.Architecture, arch...)
2018-03-22 15:46:48 +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
}
useColor := pacmanConf.Color && term.IsTerminal(int(os.Stdout.Fd()))
2021-08-11 18:13:28 +00:00
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":
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
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 13:44:38 +00:00
}
2020-08-08 16:43:37 +00:00
func main() {
2021-08-12 16:56:23 +00:00
var (
err error
ctx = context.Background()
ret = 0
)
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
defer func() { os.Exit(ret) }()
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
initGotext()
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
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)
if err != nil {
2020-08-08 16:43:37 +00:00
if str := err.Error(); str != "" {
text.Errorln(str)
2020-08-08 16:43:37 +00:00
}
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
ret = 1
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
return
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 13:44:38 +00:00
}
cmdArgs := parser.MakeArguments()
2021-08-11 18:13:28 +00:00
if err = config.ParseCommandLine(cmdArgs); err != nil {
2020-08-08 16:43:37 +00:00
if str := err.Error(); str != "" {
text.Errorln(str)
2020-08-08 16:43:37 +00:00
}
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
ret = 1
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
return
}
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 {
text.Errorln(errS)
2020-08-08 16:43:37 +00:00
}
}
2018-03-22 15:46:48 +00:00
2020-08-08 16:43:37 +00:00
var useColor bool
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
if err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
ret = 1
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
return
2018-03-22 15:46:48 +00:00
}
2021-08-11 18:13:28 +00:00
config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
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
dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
2020-08-08 16:43:37 +00:00
if err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
2020-08-08 16:43:37 +00:00
}
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
ret = 1
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
return
2018-03-22 15:46:48 +00:00
}
2020-08-08 16:43:37 +00:00
defer dbExecutor.Cleanup()
if err = handleCmd(ctx, cmdArgs, db.Executor(dbExecutor)); err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
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
2021-08-11 18:13:28 +00:00
2020-08-08 16:43:37 +00:00
return
2018-09-04 22:05:35 +00:00
}
2018-03-22 15:46:48 +00:00
}