yay/main.go

211 lines
4.8 KiB
Go
Raw Normal View History

2018-03-22 16:46:48 +01:00
package main
import (
2018-03-22 17:39:27 +01:00
"encoding/json"
"fmt"
2018-03-22 16:46:48 +01:00
"os"
"path/filepath"
"strings"
alpm "github.com/jguer/go-alpm"
)
func setPaths() error {
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 {
return fmt.Errorf("XDG_CONFIG_HOME and HOME unset")
2018-03-22 16:46:48 +01: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 {
return fmt.Errorf("XDG_CACHE_HOME and HOME unset")
2018-03-22 16:46:48 +01:00
}
configFile = filepath.Join(configHome, configFileName)
vcsFile = filepath.Join(cacheHome, vcsFileName)
return nil
2018-03-22 16:46:48 +01:00
}
func initConfig() error {
cfile, err := os.Open(configFile)
if !os.IsNotExist(err) && err != nil {
return fmt.Errorf("Failed to open config file '%s': %s", configFile, err)
}
2018-03-22 16:46:48 +01:00
defer cfile.Close()
if !os.IsNotExist(err) {
decoder := json.NewDecoder(cfile)
if err = decoder.Decode(&config); err != nil {
return fmt.Errorf("Failed to read config '%s': %s", configFile, err)
2018-03-22 16:46:48 +01:00
}
}
return nil
}
func initVCS() error {
vfile, err := os.Open(vcsFile)
if !os.IsNotExist(err) && err != nil {
return fmt.Errorf("Failed to open vcs file '%s': %s", vcsFile, err)
}
defer vfile.Close()
if !os.IsNotExist(err) {
decoder := json.NewDecoder(vfile)
if err = decoder.Decode(&savedInfo); err != nil {
return fmt.Errorf("Failed to read vcs '%s': %s", vcsFile, err)
2018-03-22 16:46:48 +01:00
}
}
return nil
2018-03-22 16:46:48 +01:00
}
func initHomeDirs() error {
if _, err := os.Stat(configHome); os.IsNotExist(err) {
if err = os.MkdirAll(configHome, 0755); err != nil {
return fmt.Errorf("Failed to create config directory '%s': %s", configHome, err)
2018-03-22 16:46:48 +01:00
}
} else if err != nil {
return err
}
if _, err := os.Stat(cacheHome); os.IsNotExist(err) {
if err = os.MkdirAll(cacheHome, 0755); err != nil {
return fmt.Errorf("Failed to create cache directory '%s': %s", cacheHome, err)
}
} else if err != nil {
return err
2018-03-22 16:46:48 +01:00
}
return nil
}
func initBuildDir() error {
if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
return fmt.Errorf("Failed to create BuildDir directory '%s': %s", config.BuildDir, err)
}
} else if err != nil {
return err
}
return nil
2018-03-22 16:46:48 +01:00
}
func initAlpm() error {
var err error
2018-03-22 16:46:48 +01:00
if alpmConf, err = readAlpmConfig(config.PacmanConf); err != nil {
return fmt.Errorf("Unable to read Pacman conf: %s", err)
2018-03-22 16:46:48 +01:00
}
if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.DBPath = value
}
if value, _, exists := cmdArgs.getArg("root", "r"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.RootDir = value
}
if value, _, exists := cmdArgs.getArg("arch"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.Architecture = value
}
if value, _, exists := cmdArgs.getArg("ignore"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
}
if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
}
//TODO
//current system does not allow duplicate arguments
//but pacman allows multiple cachdirs to be passed
//for now only handle one cache dir
if value, _, exists := cmdArgs.getArg("cachdir"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.CacheDir = []string{value}
}
if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
2018-03-22 16:46:48 +01:00
alpmConf.GPGDir = value
}
if err = initAlpmHandle(); err != nil {
return err
2018-03-22 16:46:48 +01:00
}
if value, _, _ := cmdArgs.getArg("color"); value == "always" || value == "auto" {
2018-03-22 16:46:48 +01:00
useColor = true
} else if value == "never" {
useColor = false
} else {
useColor = alpmConf.Options&alpm.ConfColor > 0
}
return nil
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 15:44:38 +02:00
}
func initAlpmHandle() error {
var err error
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 15:44:38 +02:00
if alpmHandle != nil {
if err := alpmHandle.Release(); err != nil {
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 15:44:38 +02:00
return err
}
}
2018-03-22 16:46:48 +01:00
if alpmHandle, err = alpmConf.CreateHandle(); err != nil {
return fmt.Errorf("Unable to CreateHandle: %s", err)
Separate Pacman upgrade and AUR Upgrade by default Currently When performing a system upgrade, Yay will first refresh the database then perform the repo and AUR upgrade. This allows Yay to add some features such as better batch interaction, showing potential dependency problems before the upgrade starts and combined menus showing AUR and repo upgrades together. There has been discussion that this approach is a bad idea. The main issue people have is that the separation of the database refresh and the upgrade could lead to a partial upgrade if Yay fails between the two stages. Personally I do not like this argument, there are valid reasons to Yay to fail between these points. For example there may be dependency or conflict issues during the AUR upgrade. Yay can detect these before any installing actually starts and exit, just like how pacman will when there are dependency problems. If Yay does fail between these points, for the previously mentioned reasons or even a crash then a simple refresh will not cause a partial upgrade by itself. It is then the user's responsibility to either resolve these issues or instead perform an upgrade using pacman directly. My opinions aside, The discussions on the Arch wiki has reached a decision, this method is not recommended. So to follow the decided best practises this behaviour has been disabled by default. This behaviour can be toggled using the --[no]combinedupgrade flag It should be noted that Yay's upgrade menu will not show repo packages unless --combinedupgrade is used.
2018-06-22 15:44:38 +02:00
}
alpmHandle.SetQuestionCallback(questionCallback)
2018-07-26 14:35:19 +02:00
alpmHandle.SetLogCallback(logCallback)
return nil
2018-03-22 16:46:48 +01:00
}
func exitOnError(err error) {
2018-03-22 16:46:48 +01:00
if err != nil {
if str := err.Error(); str != "" {
fmt.Println(str)
}
cleanup()
os.Exit(1)
2018-03-22 16:46:48 +01:00
}
}
2018-03-22 16:46:48 +01:00
func cleanup() int {
if alpmHandle != nil {
if err := alpmHandle.Release(); err != nil {
fmt.Println(err)
return 1
}
2018-03-22 16:46:48 +01:00
}
return 0
}
2018-03-22 16:46:48 +01:00
func main() {
if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
2018-03-22 16:46:48 +01:00
}
exitOnError(setPaths())
defaultSettings(&config)
exitOnError(initHomeDirs())
exitOnError(initConfig())
exitOnError(cmdArgs.parseCommandLine())
exitOnError(initBuildDir())
exitOnError(initVCS())
exitOnError(initAlpm())
exitOnError(handleCmd())
os.Exit(cleanup())
2018-03-22 16:46:48 +01:00
}