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"
|
2023-01-03 22:43:56 +01:00
|
|
|
"errors"
|
2018-03-22 16:46:48 +01:00
|
|
|
"os"
|
2021-02-13 18:40:28 +01:00
|
|
|
"os/exec"
|
2022-08-23 18:50:17 +02:00
|
|
|
"runtime/debug"
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/db/ialpm"
|
2023-08-06 21:39:41 +02:00
|
|
|
"github.com/Jguer/yay/v12/pkg/runtime"
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/settings"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/parser"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/text"
|
2018-03-22 16:46:48 +01:00
|
|
|
)
|
|
|
|
|
2023-03-05 22:58:18 +01:00
|
|
|
var (
|
2023-04-06 18:49:18 +02:00
|
|
|
yayVersion = "12.0.4" // To be set by compiler.
|
2023-03-05 22:58:18 +01:00
|
|
|
localePath = "/usr/share/locale" // To be set by compiler.
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-08 18:43:37 +02:00
|
|
|
func main() {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog := text.NewLogger(os.Stdout, os.Stderr, os.Stdin, false, "fallback")
|
2021-08-12 18:56:23 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ctx = context.Background()
|
|
|
|
ret = 0
|
|
|
|
)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2022-08-23 18:50:17 +02:00
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(rec)
|
2022-08-23 18:50:17 +02:00
|
|
|
debug.PrintStack()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Warnln(gotext.Get("Avoid running yay as root/sudo."))
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 09:48:39 +01:00
|
|
|
configPath := settings.GetConfigPath()
|
|
|
|
// Parse config
|
2023-08-06 21:39:41 +02:00
|
|
|
cfg, err := settings.NewConfig(fallbackLog, configPath, 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 != "" {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.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
|
|
|
}
|
|
|
|
|
2023-08-06 21:39:41 +02:00
|
|
|
if errS := cfg.RunMigrations(fallbackLog,
|
2023-03-13 09:48:39 +01:00
|
|
|
settings.DefaultMigrations(), configPath, yayVersion); errS != nil {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(errS)
|
2022-08-05 22:55:54 +02:00
|
|
|
}
|
|
|
|
|
2021-08-08 00:08:04 +02:00
|
|
|
cmdArgs := parser.MakeArguments()
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2023-03-13 09:48:39 +01:00
|
|
|
// Parse command line
|
2024-01-25 16:03:47 +01:00
|
|
|
if err = cfg.ParseCommandLine(cmdArgs); err != nil {
|
2020-08-08 18:43:37 +02:00
|
|
|
if str := err.Error(); str != "" {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.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
|
|
|
}
|
|
|
|
|
2023-03-13 09:48:39 +01:00
|
|
|
if cfg.SaveConfig {
|
|
|
|
if errS := cfg.Save(configPath, yayVersion); errS != nil {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(errS)
|
2020-08-08 18:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2023-08-06 21:39:41 +02:00
|
|
|
// Build run
|
|
|
|
run, err := runtime.NewRuntime(cfg, cmdArgs, yayVersion)
|
2023-03-13 09:48:39 +01:00
|
|
|
if err != nil {
|
|
|
|
if str := err.Error(); str != "" {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(str)
|
2023-03-13 09:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-06 21:39:41 +02:00
|
|
|
dbExecutor, err := ialpm.NewExecutor(run.PacmanConf, run.Logger.Child("db"))
|
2020-08-08 18:43:37 +02:00
|
|
|
if err != nil {
|
|
|
|
if str := err.Error(); str != "" {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.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
|
|
|
}
|
|
|
|
|
2022-08-23 18:50:17 +02:00
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(rec, string(debug.Stack()))
|
2022-08-23 18:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dbExecutor.Cleanup()
|
|
|
|
}()
|
2021-02-13 18:40:28 +01:00
|
|
|
|
2023-08-06 21:39:41 +02:00
|
|
|
if err = handleCmd(ctx, run, cmdArgs, dbExecutor); err != nil {
|
2021-08-21 16:17:19 +02:00
|
|
|
if str := err.Error(); str != "" {
|
2023-08-06 21:39:41 +02:00
|
|
|
fallbackLog.Errorln(str)
|
2019-10-13 13:15:51 +02:00
|
|
|
}
|
2021-02-13 18:40:28 +01:00
|
|
|
|
2023-01-03 22:43:56 +01:00
|
|
|
exitError := &exec.ExitError{}
|
|
|
|
if errors.As(err, &exitError) {
|
2021-02-13 18:40:28 +01:00
|
|
|
// mirror pacman exit code when applicable
|
|
|
|
ret = exitError.ExitCode()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback
|
2020-08-08 18:43:37 +02:00
|
|
|
ret = 1
|
2018-09-05 00:05:35 +02:00
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|