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"
|
|
|
|
)
|
|
|
|
|
2018-06-05 00:31:50 +02:00
|
|
|
func setPaths() error {
|
|
|
|
if _configHome, set := os.LookupEnv("XDG_CONFIG_HOME"); set {
|
2018-06-05 15:17:07 +02:00
|
|
|
if _configHome == "" {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("XDG_CONFIG_HOME set but empty")
|
2018-06-05 15:17:07 +02:00
|
|
|
}
|
2018-06-05 15:14:49 +02:00
|
|
|
configHome = filepath.Join(_configHome, "yay")
|
2018-06-05 00:31:50 +02:00
|
|
|
} else if _configHome, set := os.LookupEnv("HOME"); set {
|
2018-06-05 15:17:07 +02:00
|
|
|
if _configHome == "" {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("HOME set but empty")
|
2018-06-05 15:17:07 +02:00
|
|
|
}
|
2018-06-05 15:14:49 +02:00
|
|
|
configHome = filepath.Join(_configHome, ".config/yay")
|
2018-03-22 16:46:48 +01:00
|
|
|
} else {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("XDG_CONFIG_HOME and HOME unset")
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 00:31:50 +02:00
|
|
|
if _cacheHome, set := os.LookupEnv("XDG_CACHE_HOME"); set {
|
2018-06-05 15:17:07 +02:00
|
|
|
if _cacheHome == "" {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("XDG_CACHE_HOME set but empty")
|
2018-06-05 15:17:07 +02:00
|
|
|
}
|
2018-06-05 00:31:50 +02:00
|
|
|
cacheHome = filepath.Join(_cacheHome, "yay")
|
|
|
|
} else if _cacheHome, set := os.LookupEnv("HOME"); set {
|
2018-06-05 15:17:07 +02:00
|
|
|
if _cacheHome == "" {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("XDG_CACHE_HOME set but empty")
|
2018-06-05 15:17:07 +02:00
|
|
|
}
|
2018-06-05 00:31:50 +02:00
|
|
|
cacheHome = filepath.Join(_cacheHome, ".cache/yay")
|
2018-03-22 16:46:48 +01:00
|
|
|
} else {
|
2018-06-09 15:33:28 +02:00
|
|
|
return fmt.Errorf("XDG_CACHE_HOME and HOME unset")
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func initConfig() (err error) {
|
|
|
|
defaultSettings(&config)
|
|
|
|
|
|
|
|
if _, err = os.Stat(configFile); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(filepath.Dir(configFile), 0755)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Unable to create config directory:\n%s\n"+
|
|
|
|
"The error was:\n%s", filepath.Dir(configFile), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Save the default config if nothing is found
|
|
|
|
config.saveConfig()
|
|
|
|
} else {
|
|
|
|
cfile, errf := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
|
|
|
|
if errf != nil {
|
|
|
|
fmt.Printf("Error reading config: %s\n", err)
|
|
|
|
} else {
|
|
|
|
defer cfile.Close()
|
|
|
|
decoder := json.NewDecoder(cfile)
|
|
|
|
err = decoder.Decode(&config)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Loading default Settings.\nError reading config:",
|
|
|
|
err)
|
|
|
|
defaultSettings(&config)
|
|
|
|
}
|
2018-04-28 12:53:10 +02:00
|
|
|
if _, err = os.Stat(config.BuildDir); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(config.BuildDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Unable to create BuildDir directory:\n%s\n"+
|
|
|
|
"The error was:\n%s", config.BuildDir, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initVCS() (err error) {
|
|
|
|
if _, err = os.Stat(vcsFile); os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(filepath.Dir(vcsFile), 0755)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Unable to create vcs directory:\n%s\n"+
|
|
|
|
"The error was:\n%s", filepath.Dir(configFile), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
|
|
|
|
if err == nil {
|
|
|
|
defer vfile.Close()
|
|
|
|
decoder := json.NewDecoder(vfile)
|
|
|
|
_ = decoder.Decode(&savedInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initAlpm() (err error) {
|
|
|
|
var value string
|
|
|
|
var exists bool
|
|
|
|
//var double bool
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("config")
|
|
|
|
if exists {
|
|
|
|
config.PacmanConf = value
|
|
|
|
}
|
|
|
|
|
|
|
|
alpmConf, err = readAlpmConfig(config.PacmanConf)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Unable to read Pacman conf: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("dbpath", "b")
|
|
|
|
if exists {
|
|
|
|
alpmConf.DBPath = value
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("root", "r")
|
|
|
|
if exists {
|
|
|
|
alpmConf.RootDir = value
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("arch")
|
|
|
|
if exists {
|
|
|
|
alpmConf.Architecture = value
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("ignore")
|
|
|
|
if exists {
|
|
|
|
alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("ignoregroup")
|
|
|
|
if exists {
|
|
|
|
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
|
|
|
|
value, _, exists = cmdArgs.getArg("cachdir")
|
|
|
|
if exists {
|
|
|
|
alpmConf.CacheDir = []string{value}
|
|
|
|
}
|
|
|
|
|
|
|
|
value, _, exists = cmdArgs.getArg("gpgdir")
|
|
|
|
if exists {
|
|
|
|
alpmConf.GPGDir = value
|
|
|
|
}
|
|
|
|
|
|
|
|
alpmHandle, err = alpmConf.CreateHandle()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Unable to CreateHandle: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:57:02 +02:00
|
|
|
value, _, _ = cmdArgs.getArg("color")
|
2018-03-22 16:46:48 +01:00
|
|
|
if value == "always" || value == "auto" {
|
|
|
|
useColor = true
|
|
|
|
} else if value == "never" {
|
|
|
|
useColor = false
|
|
|
|
} else {
|
|
|
|
useColor = alpmConf.Options&alpm.ConfColor > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
alpmHandle.SetQuestionCallback(questionCallback)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var status int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if 0 == os.Geteuid() {
|
|
|
|
fmt.Println("Please avoid running yay as root/sudo.")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmdArgs.parseCommandLine()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:31:50 +02:00
|
|
|
err = setPaths()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
}
|
2018-03-22 16:46:48 +01:00
|
|
|
|
|
|
|
err = initConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
err = initVCS()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = initAlpm()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handleCmd()
|
|
|
|
if err != nil {
|
2018-03-23 20:49:51 +01:00
|
|
|
if err.Error() != "" {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:46:48 +01:00
|
|
|
status = 1
|
|
|
|
goto cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
//cleanup
|
2018-05-12 18:16:48 +02:00
|
|
|
//from here on out don't exit if an error occurs
|
2018-03-22 16:46:48 +01:00
|
|
|
//if we fail to save the configuration
|
|
|
|
//at least continue on and try clean up other parts
|
|
|
|
|
|
|
|
if alpmHandle != nil {
|
|
|
|
err = alpmHandle.Release()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
status = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(status)
|
|
|
|
}
|