forked from Jguer/yay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (164 loc) · 4.87 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main // import "github.com/Jguer/yay"
import (
"encoding/json"
"errors"
"fmt"
"os"
alpm "github.com/Jguer/go-alpm"
pacmanconf "github.com/Morganamilo/go-pacmanconf"
"github.com/leonelquinteros/gotext"
"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/text"
)
func initGotext() {
if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
localePath = envLocalePath
}
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
}
func initConfig(configPath string) error {
cfile, err := os.Open(configPath)
if !os.IsNotExist(err) && err != nil {
return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
}
defer cfile.Close()
if !os.IsNotExist(err) {
decoder := json.NewDecoder(cfile)
if err = decoder.Decode(&config); err != nil {
return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
}
}
aurdest := os.Getenv("AURDEST")
if aurdest != "" {
config.BuildDir = aurdest
}
return nil
}
func initVCS(vcsFilePath string) error {
vfile, err := os.Open(vcsFilePath)
if !os.IsNotExist(err) && err != nil {
return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
}
defer vfile.Close()
if !os.IsNotExist(err) {
decoder := json.NewDecoder(vfile)
if err = decoder.Decode(&savedInfo); err != nil {
return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
}
}
return nil
}
func initBuildDir() error {
if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
}
} else if err != nil {
return err
}
return nil
}
func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*alpm.Handle, *pacmanconf.Config, error) {
root := "/"
if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
root = value
}
pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
if err != nil {
return nil, nil, fmt.Errorf("%s", stderr)
}
if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
pacmanConf.DBPath = dbPath
}
if arch, _, exists := cmdArgs.GetArg("arch"); exists {
pacmanConf.Architecture = arch
}
if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
}
if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
}
if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
pacmanConf.CacheDir = cacheArray
}
if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
pacmanConf.GPGDir = gpgDir
}
alpmHandle, err := initAlpmHandle(pacmanConf, nil)
if err != nil {
return nil, nil, err
}
switch value, _, _ := cmdArgs.GetArg("color"); value {
case "always":
text.UseColor = true
case "auto":
text.UseColor = isTty()
case "never":
text.UseColor = false
default:
text.UseColor = pacmanConf.Color && isTty()
}
return alpmHandle, pacmanConf, nil
}
func initAlpmHandle(pacmanConf *pacmanconf.Config, oldAlpmHandle *alpm.Handle) (*alpm.Handle, error) {
if oldAlpmHandle != nil {
if errRelease := oldAlpmHandle.Release(); errRelease != nil {
return nil, errRelease
}
}
alpmHandle, err := alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath)
if err != nil {
return nil, errors.New(gotext.Get("unable to CreateHandle: %s", err))
}
if err := configureAlpm(pacmanConf, alpmHandle); err != nil {
return nil, err
}
alpmHandle.SetQuestionCallback(questionCallback)
alpmHandle.SetLogCallback(logCallback)
return alpmHandle, nil
}
func exitOnError(err error) {
if err != nil {
if str := err.Error(); str != "" {
fmt.Fprintln(os.Stderr, str)
}
cleanup(config.Runtime.AlpmHandle)
os.Exit(1)
}
}
func cleanup(alpmHandle *alpm.Handle) int {
if alpmHandle != nil {
if err := alpmHandle.Release(); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
}
return 0
}
func main() {
initGotext()
if os.Geteuid() == 0 {
text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
}
cmdArgs := settings.MakeArguments()
runtime, err := settings.MakeRuntime()
exitOnError(err)
config = settings.MakeConfig()
config.Runtime = runtime
exitOnError(initConfig(runtime.ConfigPath))
exitOnError(cmdArgs.ParseCommandLine(config))
if config.Runtime.SaveConfig {
errS := config.SaveConfig(runtime.ConfigPath)
if errS != nil {
fmt.Fprintln(os.Stderr, err)
}
}
config.ExpandEnv()
exitOnError(initBuildDir())
exitOnError(initVCS(runtime.VCSPath))
config.Runtime.AlpmHandle, config.Runtime.PacmanConf, err = initAlpm(cmdArgs, config.PacmanConf)
exitOnError(err)
exitOnError(handleCmd(cmdArgs, config.Runtime.AlpmHandle))
os.Exit(cleanup(config.Runtime.AlpmHandle))
}