forked from Jguer/yay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (136 loc) · 3.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main // import "github.com/Jguer/yay"
import (
"context"
"errors"
"os"
"os/exec"
"runtime/debug"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v12/pkg/db"
"github.com/Jguer/yay/v12/pkg/db/ialpm"
"github.com/Jguer/yay/v12/pkg/query"
"github.com/Jguer/yay/v12/pkg/settings"
"github.com/Jguer/yay/v12/pkg/settings/parser"
"github.com/Jguer/yay/v12/pkg/text"
)
var (
yayVersion = "12.0.4" // To be set by compiler.
localePath = "/usr/share/locale" // To be set by compiler.
)
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."))
}
configPath := settings.GetConfigPath()
// Parse config
cfg, err := settings.NewConfig(configPath, yayVersion)
if err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
ret = 1
return
}
if cfg.Debug {
text.GlobalLogger.Debug = true
}
if errS := cfg.RunMigrations(
settings.DefaultMigrations(), configPath, yayVersion); errS != nil {
text.Errorln(errS)
}
cmdArgs := parser.MakeArguments()
// Parse command line
if err = cfg.ParseCommandLine(cmdArgs); err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
ret = 1
return
}
if cfg.SaveConfig {
if errS := cfg.Save(configPath, yayVersion); errS != nil {
text.Errorln(errS)
}
}
// Build runtime
runtime, err := settings.BuildRuntime(cfg, cmdArgs, yayVersion)
if err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
ret = 1
return
}
cfg.Runtime = runtime
cfg.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
cfg.Runtime.AURCache,
cfg.Runtime.Logger.Child("mixed.querybuilder"), cfg.SortBy,
cfg.Mode, cfg.SearchBy,
cfg.BottomUp, cfg.SingleLineResults, cfg.SeparateSources)
var useColor bool
cfg.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, cfg.PacmanConf)
if err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
ret = 1
return
}
cfg.Runtime.CmdBuilder.SetPacmanDBPath(cfg.Runtime.PacmanConf.DBPath)
text.UseColor = useColor
dbExecutor, err := ialpm.NewExecutor(cfg.Runtime.PacmanConf, runtime.Logger.Child("db"))
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, cfg, cmdArgs, db.Executor(dbExecutor)); err != nil {
if str := err.Error(); str != "" {
text.Errorln(str)
}
exitError := &exec.ExitError{}
if errors.As(err, &exitError) {
// mirror pacman exit code when applicable
ret = exitError.ExitCode()
return
}
// fallback
ret = 1
}
}