123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package main // import "github.com/Jguer/yay"
- import (
- "context"
- "os"
- "os/exec"
- "runtime/debug"
- "github.com/leonelquinteros/gotext"
- "github.com/Jguer/yay/v11/pkg/db"
- "github.com/Jguer/yay/v11/pkg/db/ialpm"
- "github.com/Jguer/yay/v11/pkg/query"
- "github.com/Jguer/yay/v11/pkg/settings"
- "github.com/Jguer/yay/v11/pkg/settings/parser"
- "github.com/Jguer/yay/v11/pkg/text"
- )
- func initGotext() {
- 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 main() {
- var (
- err error
- ctx = context.Background()
- ret = 0
- )
- defer func() {
- if rec := recover(); rec != nil {
- text.Errorln(rec)
- debug.PrintStack()
- }
- os.Exit(ret)
- }()
- initGotext()
- if os.Geteuid() == 0 {
- text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
- }
- config, err = settings.NewConfig(yayVersion)
- if err != nil {
- if str := err.Error(); str != "" {
- text.Errorln(str)
- }
- ret = 1
- return
- }
- if errS := config.RunMigrations(
- settings.DefaultMigrations(), config.Runtime.ConfigPath); errS != nil {
- text.Errorln(errS)
- }
- cmdArgs := parser.MakeArguments()
- if err = config.ParseCommandLine(cmdArgs); err != nil {
- if str := err.Error(); str != "" {
- text.Errorln(str)
- }
- ret = 1
- return
- }
- if config.Runtime.SaveConfig {
- if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
- text.Errorln(errS)
- }
- }
- if config.SeparateSources {
- config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(config.SortBy,
- config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
- } else {
- config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(config.SortBy,
- config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
- }
- var useColor bool
- config.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, config.PacmanConf)
- if err != nil {
- if str := err.Error(); str != "" {
- text.Errorln(str)
- }
- ret = 1
- return
- }
- config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
- text.UseColor = useColor
- dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
- if err != nil {
- if str := err.Error(); str != "" {
- text.Errorln(str)
- }
- ret = 1
- return
- }
- defer func() {
- if rec := recover(); rec != nil {
- text.Errorln(rec)
- debug.PrintStack()
- }
- 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
- ret = 1
- }
- }
|