2018-09-15 19:17:03 +02:00
|
|
|
package main // import "github.com/Jguer/yay"
|
2018-03-22 16:46:48 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-22 17:39:27 +01:00
|
|
|
"encoding/json"
|
2020-05-04 09:24:32 +02:00
|
|
|
"errors"
|
2018-03-22 17:39:27 +01:00
|
|
|
"fmt"
|
2018-03-22 16:46:48 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-23 17:53:20 +02:00
|
|
|
alpm "github.com/Jguer/go-alpm"
|
2019-06-17 14:22:30 +02:00
|
|
|
pacmanconf "github.com/Morganamilo/go-pacmanconf"
|
2020-05-04 09:24:32 +02:00
|
|
|
"github.com/leonelquinteros/gotext"
|
|
|
|
|
|
|
|
"github.com/Jguer/yay/v9/pkg/text"
|
2018-03-22 16:46:48 +01:00
|
|
|
)
|
|
|
|
|
2018-06-05 00:31:50 +02:00
|
|
|
func setPaths() error {
|
2018-08-12 05:11:34 +02:00
|
|
|
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
|
|
|
|
configHome = filepath.Join(configHome, "yay")
|
|
|
|
} else if configHome = os.Getenv("HOME"); configHome != "" {
|
|
|
|
configHome = filepath.Join(configHome, ".config/yay")
|
2018-03-22 16:46:48 +01:00
|
|
|
} else {
|
2020-05-08 18:13:51 +02:00
|
|
|
return errors.New(gotext.Get("%s and %s unset", "XDG_CONFIG_HOME", "HOME"))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-12 05:11:34 +02:00
|
|
|
if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
|
|
|
|
cacheHome = filepath.Join(cacheHome, "yay")
|
|
|
|
} else if cacheHome = os.Getenv("HOME"); cacheHome != "" {
|
|
|
|
cacheHome = filepath.Join(cacheHome, ".cache/yay")
|
2018-03-22 16:46:48 +01:00
|
|
|
} else {
|
2020-05-08 18:13:51 +02:00
|
|
|
return errors.New(gotext.Get("%s and %s unset", "XDG_CACHE_HOME", "HOME"))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 02:32:31 +02:00
|
|
|
configFile = filepath.Join(configHome, configFileName)
|
|
|
|
vcsFile = filepath.Join(cacheHome, vcsFileName)
|
2018-06-05 00:31:50 +02:00
|
|
|
|
|
|
|
return nil
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-04 09:24:32 +02:00
|
|
|
gotext.Configure(localePath, os.Getenv("LANG"), "yay")
|
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
func initConfig() error {
|
2018-08-12 03:40:43 +02:00
|
|
|
cfile, err := os.Open(configFile)
|
|
|
|
if !os.IsNotExist(err) && err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to open config file '%s': %s", configFile, err))
|
2018-08-12 03:40:43 +02:00
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
defer cfile.Close()
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
decoder := json.NewDecoder(cfile)
|
|
|
|
if err = decoder.Decode(&config); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to read config file '%s': %s", configFile, err))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
2018-08-02 22:36:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:54:20 +02:00
|
|
|
aurdest := os.Getenv("AURDEST")
|
|
|
|
if aurdest != "" {
|
|
|
|
config.BuildDir = aurdest
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-02 22:36:42 +02:00
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
func initVCS() error {
|
|
|
|
vfile, err := os.Open(vcsFile)
|
|
|
|
if !os.IsNotExist(err) && err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFile, err))
|
2018-08-02 22:36:42 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
defer vfile.Close()
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
decoder := json.NewDecoder(vfile)
|
|
|
|
if err = decoder.Decode(&savedInfo); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFile, err))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
return nil
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
func initHomeDirs() error {
|
|
|
|
if _, err := os.Stat(configHome); os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(configHome, 0755); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to create config directory '%s': %s", configHome, err))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
2018-08-02 22:36:42 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
if _, err := os.Stat(cacheHome); os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(cacheHome, 0755); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to create cache directory '%s': %s", cacheHome, err))
|
2018-08-12 03:40:43 +02:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
2018-08-02 22:36:42 +02:00
|
|
|
return err
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-12 03:40:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBuildDir() error {
|
|
|
|
if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
|
2018-08-12 03:40:43 +02:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 22:36:42 +02:00
|
|
|
|
|
|
|
return nil
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
func initAlpm() error {
|
|
|
|
var err error
|
2018-09-04 16:47:22 +02:00
|
|
|
var stderr string
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2018-09-04 16:47:22 +02:00
|
|
|
root := "/"
|
|
|
|
if value, _, exists := cmdArgs.getArg("root", "r"); exists {
|
|
|
|
root = value
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf, stderr, err = pacmanconf.PacmanConf("--config", config.PacmanConf, "--root", root)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s", stderr)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-09-04 16:47:22 +02:00
|
|
|
if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
|
|
|
|
pacmanConf.DBPath = value
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
if value, _, exists := cmdArgs.getArg("arch"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf.Architecture = value
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
if value, _, exists := cmdArgs.getArg("ignore"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, strings.Split(value, ",")...)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, strings.Split(value, ",")...)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
// TODO
|
|
|
|
// current system does not allow duplicate arguments
|
|
|
|
// but pacman allows multiple cachedirs to be passed
|
|
|
|
// for now only handle one cache dir
|
2018-12-19 22:13:46 +01:00
|
|
|
if value, _, exists := cmdArgs.getArg("cachedir"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf.CacheDir = []string{value}
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
|
2018-09-04 16:47:22 +02:00
|
|
|
pacmanConf.GPGDir = value
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 21:10:04 +01:00
|
|
|
if err := initAlpmHandle(); err != nil {
|
2018-08-02 22:36:42 +02:00
|
|
|
return err
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 17:07:04 +01:00
|
|
|
switch value, _, _ := cmdArgs.getArg("color"); value {
|
|
|
|
case "always":
|
2018-03-22 16:46:48 +01:00
|
|
|
useColor = true
|
2019-03-04 17:07:04 +01:00
|
|
|
case "auto":
|
2018-09-27 17:50:59 +02:00
|
|
|
useColor = isTty()
|
2019-03-04 17:07:04 +01:00
|
|
|
case "never":
|
2018-03-22 16:46:48 +01:00
|
|
|
useColor = false
|
2019-03-04 17:07:04 +01:00
|
|
|
default:
|
2018-09-27 17:50:59 +02:00
|
|
|
useColor = pacmanConf.Color && isTty()
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
return nil
|
2018-06-22 15:44:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 22:36:42 +02:00
|
|
|
func initAlpmHandle() error {
|
2018-06-22 15:44:38 +02:00
|
|
|
if alpmHandle != nil {
|
2020-05-02 16:17:20 +02:00
|
|
|
if errRelease := alpmHandle.Release(); errRelease != nil {
|
|
|
|
return errRelease
|
2018-06-22 15:44:38 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
var err error
|
2019-02-04 17:56:02 +01:00
|
|
|
if alpmHandle, err = alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath); err != nil {
|
2020-05-04 09:24:32 +02:00
|
|
|
return errors.New(gotext.Get("unable to CreateHandle: %s", err))
|
2018-06-22 15:44:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:17:20 +02:00
|
|
|
if err := configureAlpm(); err != nil {
|
2018-09-04 16:47:22 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-22 15:44:38 +02:00
|
|
|
alpmHandle.SetQuestionCallback(questionCallback)
|
2018-07-26 14:35:19 +02:00
|
|
|
alpmHandle.SetLogCallback(logCallback)
|
2018-08-02 22:36:42 +02:00
|
|
|
return nil
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 21:45:41 +02:00
|
|
|
func exitOnError(err error) {
|
2018-03-22 16:46:48 +01:00
|
|
|
if err != nil {
|
2018-08-02 21:45:41 +02:00
|
|
|
if str := err.Error(); str != "" {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, str)
|
2018-08-02 21:45:41 +02:00
|
|
|
}
|
|
|
|
cleanup()
|
|
|
|
os.Exit(1)
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
2018-08-02 21:45:41 +02:00
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2018-08-02 21:45:41 +02:00
|
|
|
func cleanup() int {
|
|
|
|
if alpmHandle != nil {
|
|
|
|
if err := alpmHandle.Release(); err != nil {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-08-02 21:45:41 +02:00
|
|
|
return 1
|
2018-03-23 20:49:51 +01:00
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 21:45:41 +02:00
|
|
|
return 0
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
2018-08-02 21:45:41 +02:00
|
|
|
func main() {
|
2020-05-04 09:24:32 +02:00
|
|
|
initGotext()
|
2019-03-05 09:08:37 +01:00
|
|
|
if os.Geteuid() == 0 {
|
2020-05-04 09:24:32 +02:00
|
|
|
text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-02 21:45:41 +02:00
|
|
|
exitOnError(setPaths())
|
2018-09-14 21:18:59 +02:00
|
|
|
config = defaultSettings()
|
2018-08-12 03:40:43 +02:00
|
|
|
exitOnError(initHomeDirs())
|
2018-08-02 21:45:41 +02:00
|
|
|
exitOnError(initConfig())
|
2018-08-12 03:40:43 +02:00
|
|
|
exitOnError(cmdArgs.parseCommandLine())
|
2018-09-05 00:05:35 +02:00
|
|
|
if shouldSaveConfig {
|
2019-10-13 13:15:51 +02:00
|
|
|
err := config.saveConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2018-09-05 00:05:35 +02:00
|
|
|
}
|
|
|
|
config.expandEnv()
|
2018-08-12 03:40:43 +02:00
|
|
|
exitOnError(initBuildDir())
|
2018-08-02 21:45:41 +02:00
|
|
|
exitOnError(initVCS())
|
|
|
|
exitOnError(initAlpm())
|
|
|
|
exitOnError(handleCmd())
|
|
|
|
os.Exit(cleanup())
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|