2018-09-15 17:17:03 +00:00
|
|
|
package main // import "github.com/Jguer/yay"
|
2018-03-22 15:46:48 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-22 16:39:27 +00:00
|
|
|
"encoding/json"
|
2020-05-04 07:24:32 +00:00
|
|
|
"errors"
|
2018-03-22 16:39:27 +00:00
|
|
|
"fmt"
|
2018-03-22 15:46:48 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-23 15:53:20 +00:00
|
|
|
alpm "github.com/Jguer/go-alpm"
|
2019-06-17 12:22:30 +00:00
|
|
|
pacmanconf "github.com/Morganamilo/go-pacmanconf"
|
2020-05-04 07:24:32 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/settings"
|
2020-06-13 17:29:50 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2018-03-22 15:46:48 +00:00
|
|
|
)
|
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
func initGotext() {
|
2020-05-08 16:13:51 +00:00
|
|
|
if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
|
|
|
|
localePath = envLocalePath
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
|
|
|
|
}
|
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
func initConfig(configPath string) error {
|
|
|
|
cfile, err := os.Open(configPath)
|
2018-08-12 01:40:43 +00:00
|
|
|
if !os.IsNotExist(err) && err != nil {
|
2020-07-05 01:17:35 +00:00
|
|
|
return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
|
2018-08-12 01:40:43 +00:00
|
|
|
}
|
2018-03-22 15:46:48 +00:00
|
|
|
|
2018-08-12 01:40:43 +00:00
|
|
|
defer cfile.Close()
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
decoder := json.NewDecoder(cfile)
|
|
|
|
if err = decoder.Decode(&config); err != nil {
|
2020-07-05 01:17:35 +00:00
|
|
|
return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
2018-08-02 20:36:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 22:54:20 +00:00
|
|
|
aurdest := os.Getenv("AURDEST")
|
|
|
|
if aurdest != "" {
|
|
|
|
config.BuildDir = aurdest
|
|
|
|
}
|
|
|
|
|
2018-08-12 01:40:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-02 20:36:42 +00:00
|
|
|
|
2020-07-05 01:26:00 +00:00
|
|
|
func initVCS(vcsFilePath string) error {
|
|
|
|
vfile, err := os.Open(vcsFilePath)
|
2018-08-12 01:40:43 +00:00
|
|
|
if !os.IsNotExist(err) && err != nil {
|
2020-07-05 01:26:00 +00:00
|
|
|
return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
|
2018-08-02 20:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:40:43 +00:00
|
|
|
defer vfile.Close()
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
decoder := json.NewDecoder(vfile)
|
|
|
|
if err = decoder.Decode(&savedInfo); err != nil {
|
2020-07-05 01:26:00 +00:00
|
|
|
return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 01:40:43 +00:00
|
|
|
return nil
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 01:40:43 +00:00
|
|
|
func initBuildDir() error {
|
|
|
|
if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
|
2020-07-08 01:40:50 +00:00
|
|
|
if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
|
2020-05-04 07:24:32 +00:00
|
|
|
return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
|
2018-08-12 01:40:43 +00:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 20:36:42 +00:00
|
|
|
|
|
|
|
return nil
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 01:31:35 +00:00
|
|
|
func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*alpm.Handle, *pacmanconf.Config, error) {
|
2018-09-04 14:47:22 +00:00
|
|
|
root := "/"
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
root = value
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 13:34:27 +00:00
|
|
|
pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
|
2018-09-04 14:47:22 +00:00
|
|
|
if err != nil {
|
2020-07-05 14:58:35 +00:00
|
|
|
return nil, nil, fmt.Errorf("%s", stderr)
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.DBPath = value
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("arch"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.Architecture = value
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("ignore"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, strings.Split(value, ",")...)
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("ignoregroup"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, strings.Split(value, ",")...)
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
// TODO
|
|
|
|
// current system does not allow duplicate arguments
|
|
|
|
// but pacman allows multiple cachedirs to be passed
|
|
|
|
// for now only handle one cache dir
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("cachedir"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.CacheDir = []string{value}
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
if value, _, exists := cmdArgs.GetArg("gpgdir"); exists {
|
2018-09-04 14:47:22 +00:00
|
|
|
pacmanConf.GPGDir = value
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
alpmHandle, err := initAlpmHandle(pacmanConf, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2018-03-22 15:46:48 +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-06-26 07:03:45 +00:00
|
|
|
text.UseColor = true
|
2019-03-04 16:07:04 +00:00
|
|
|
case "auto":
|
2020-06-26 07:03:45 +00:00
|
|
|
text.UseColor = isTty()
|
2019-03-04 16:07:04 +00:00
|
|
|
case "never":
|
2020-06-26 07:03:45 +00:00
|
|
|
text.UseColor = false
|
2019-03-04 16:07:04 +00:00
|
|
|
default:
|
2020-06-26 07:03:45 +00:00
|
|
|
text.UseColor = pacmanConf.Color && isTty()
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
return alpmHandle, pacmanConf, nil
|
2018-06-22 13:44:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
func initAlpmHandle(pacmanConf *pacmanconf.Config, oldAlpmHandle *alpm.Handle) (*alpm.Handle, error) {
|
|
|
|
if oldAlpmHandle != nil {
|
|
|
|
if errRelease := oldAlpmHandle.Release(); errRelease != nil {
|
|
|
|
return nil, errRelease
|
2018-06-22 13:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 15:46:48 +00:00
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
alpmHandle, err := alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New(gotext.Get("unable to CreateHandle: %s", err))
|
2018-06-22 13:44:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
if err := configureAlpm(pacmanConf, alpmHandle); err != nil {
|
|
|
|
return nil, err
|
2018-09-04 14:47:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 13:44:38 +00:00
|
|
|
alpmHandle.SetQuestionCallback(questionCallback)
|
2018-07-26 12:35:19 +00:00
|
|
|
alpmHandle.SetLogCallback(logCallback)
|
2020-07-05 14:58:35 +00:00
|
|
|
return alpmHandle, nil
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:45:41 +00:00
|
|
|
func exitOnError(err error) {
|
2018-03-22 15:46:48 +00:00
|
|
|
if err != nil {
|
2018-08-02 19:45:41 +00:00
|
|
|
if str := err.Error(); str != "" {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, str)
|
2018-08-02 19:45:41 +00:00
|
|
|
}
|
2020-07-05 14:58:35 +00:00
|
|
|
cleanup(config.Runtime.AlpmHandle)
|
2018-08-02 19:45:41 +00:00
|
|
|
os.Exit(1)
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
2018-08-02 19:45:41 +00:00
|
|
|
}
|
2018-03-22 15:46:48 +00:00
|
|
|
|
2020-07-05 14:58:35 +00:00
|
|
|
func cleanup(alpmHandle *alpm.Handle) int {
|
2018-08-02 19:45:41 +00:00
|
|
|
if alpmHandle != nil {
|
|
|
|
if err := alpmHandle.Release(); err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-08-02 19:45:41 +00:00
|
|
|
return 1
|
2018-03-23 19:49:51 +00:00
|
|
|
}
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:45:41 +00:00
|
|
|
return 0
|
|
|
|
}
|
2018-03-22 15:46:48 +00:00
|
|
|
|
2018-08-02 19:45:41 +00:00
|
|
|
func main() {
|
2020-05-04 07:24:32 +00:00
|
|
|
initGotext()
|
2019-03-05 08:08:37 +00:00
|
|
|
if os.Geteuid() == 0 {
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 01:22:01 +00:00
|
|
|
cmdArgs := settings.MakeArguments()
|
2020-07-05 01:17:35 +00:00
|
|
|
runtime, err := settings.MakeRuntime()
|
|
|
|
exitOnError(err)
|
2020-07-05 01:28:57 +00:00
|
|
|
config = settings.MakeConfig()
|
2020-07-05 01:17:35 +00:00
|
|
|
config.Runtime = runtime
|
|
|
|
exitOnError(initConfig(runtime.ConfigPath))
|
2020-07-05 00:45:23 +00:00
|
|
|
exitOnError(cmdArgs.ParseCommandLine(config))
|
|
|
|
if config.Runtime.SaveConfig {
|
2020-07-05 13:34:27 +00:00
|
|
|
errS := config.SaveConfig(runtime.ConfigPath)
|
|
|
|
if errS != nil {
|
2019-10-13 11:15:51 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2018-09-04 22:05:35 +00:00
|
|
|
}
|
2020-07-05 00:01:08 +00:00
|
|
|
config.ExpandEnv()
|
2018-08-12 01:40:43 +00:00
|
|
|
exitOnError(initBuildDir())
|
2020-07-05 01:26:00 +00:00
|
|
|
exitOnError(initVCS(runtime.VCSPath))
|
2020-07-08 01:31:35 +00:00
|
|
|
config.Runtime.AlpmHandle, config.Runtime.PacmanConf, err = initAlpm(cmdArgs, config.PacmanConf)
|
2020-07-05 13:34:27 +00:00
|
|
|
exitOnError(err)
|
2020-07-08 01:31:35 +00:00
|
|
|
exitOnError(handleCmd(cmdArgs, config.Runtime.AlpmHandle))
|
2020-07-05 14:58:35 +00:00
|
|
|
os.Exit(cleanup(config.Runtime.AlpmHandle))
|
2018-03-22 15:46:48 +00:00
|
|
|
}
|