2017-08-02 17:24:03 +00:00
|
|
|
package main
|
2017-05-06 16:34:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-09-05 02:51:09 +00:00
|
|
|
"os/user"
|
2017-05-06 16:34:40 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
|
|
|
)
|
|
|
|
|
2017-05-07 01:43:49 +00:00
|
|
|
// Verbosity settings for search
|
|
|
|
const (
|
|
|
|
NumberMenu = iota
|
|
|
|
Detailed
|
|
|
|
Minimal
|
|
|
|
)
|
|
|
|
|
|
|
|
// Describes Sorting method for numberdisplay
|
|
|
|
const (
|
|
|
|
BottomUp = iota
|
|
|
|
TopDown
|
|
|
|
)
|
|
|
|
|
|
|
|
// Configuration stores yay's config.
|
2017-05-06 16:34:40 +00:00
|
|
|
type Configuration struct {
|
2017-07-06 23:25:49 +00:00
|
|
|
BuildDir string `json:"buildDir"`
|
|
|
|
Editor string `json:"editor"`
|
|
|
|
MakepkgBin string `json:"makepkgbin"`
|
|
|
|
Shell string `json:"-"`
|
|
|
|
NoConfirm bool `json:"noconfirm"`
|
|
|
|
Devel bool `json:"devel"`
|
|
|
|
PacmanBin string `json:"pacmanbin"`
|
|
|
|
PacmanConf string `json:"pacmanconf"`
|
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
SearchMode int `json:"-"`
|
|
|
|
SortMode int `json:"sortmode"`
|
|
|
|
TarBin string `json:"tarbin"`
|
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
var version = "2.116"
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-04 09:26:53 +00:00
|
|
|
// baseURL givers the AUR default address.
|
|
|
|
const baseURL string = "https://aur.archlinux.org"
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
var specialDBsauce = false
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
var savedInfo infos
|
2017-07-06 07:30:48 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
var configfile string
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// Updated returns if database has been updated
|
|
|
|
var updated bool
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// YayConf holds the current config values for yay.
|
|
|
|
var config Configuration
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// AlpmConf holds the current config values for pacman.
|
|
|
|
var AlpmConf alpm.PacmanConfig
|
2017-05-06 16:34:40 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// AlpmHandle is the alpm handle used by yay.
|
|
|
|
var AlpmHandle *alpm.Handle
|
2017-05-06 16:34:40 +00:00
|
|
|
|
|
|
|
func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
|
|
|
|
file, err := os.Open(pacmanconf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conf, err = alpm.ParseConfig(file)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-07 01:43:49 +00:00
|
|
|
// SaveConfig writes yay config to file.
|
2017-08-02 17:24:03 +00:00
|
|
|
func (config *Configuration) saveConfig() error {
|
|
|
|
config.NoConfirm = false
|
|
|
|
marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
|
2017-10-14 17:01:54 +00:00
|
|
|
in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0644)
|
2017-05-07 01:43:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
_, err = in.Write(marshalledinfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = in.Sync()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-06 16:34:40 +00:00
|
|
|
func defaultSettings(config *Configuration) {
|
2017-09-05 02:51:09 +00:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
config.BuildDir = fmt.Sprintf("/tmp/yaytmp-%s/", u.Uid)
|
2017-05-06 16:34:40 +00:00
|
|
|
config.Editor = ""
|
2017-05-09 01:09:43 +00:00
|
|
|
config.Devel = false
|
2017-05-06 16:34:40 +00:00
|
|
|
config.MakepkgBin = "/usr/bin/makepkg"
|
2017-05-07 01:43:49 +00:00
|
|
|
config.NoConfirm = false
|
2017-05-06 16:34:40 +00:00
|
|
|
config.PacmanBin = "/usr/bin/pacman"
|
|
|
|
config.PacmanConf = "/etc/pacman.conf"
|
2017-05-07 01:43:49 +00:00
|
|
|
config.SortMode = BottomUp
|
2017-05-06 16:34:40 +00:00
|
|
|
config.TarBin = "/usr/bin/bsdtar"
|
2017-05-24 23:28:35 +00:00
|
|
|
config.TimeUpdate = false
|
2017-07-06 23:25:49 +00:00
|
|
|
config.RequestSplitN = 150
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Editor returns the preferred system editor.
|
2017-08-02 17:24:03 +00:00
|
|
|
func editor() string {
|
2017-05-06 16:34:40 +00:00
|
|
|
switch {
|
2017-08-04 09:26:53 +00:00
|
|
|
case config.Editor != "":
|
|
|
|
editor, err := exec.LookPath(config.Editor)
|
2017-05-06 16:34:40 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("EDITOR") != "":
|
|
|
|
editor, err := exec.LookPath(os.Getenv("EDITOR"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("VISUAL") != "":
|
|
|
|
editor, err := exec.LookPath(os.Getenv("VISUAL"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m$EDITOR\x1b[0;37;40m is not set.\x1b[0m\nPlease add $EDITOR or to your environment variables.\n")
|
|
|
|
|
|
|
|
editorLoop:
|
|
|
|
fmt.Printf("\x1b[32m%s\x1b[0m ", "Edit PKGBUILD with:")
|
|
|
|
var editorInput string
|
|
|
|
_, err := fmt.Scanln(&editorInput)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
goto editorLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
editor, err := exec.LookPath(editorInput)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
goto editorLoop
|
|
|
|
}
|
|
|
|
return editor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinueTask prompts if user wants to continue task.
|
|
|
|
//If NoConfirm is set the action will continue without user input.
|
2017-08-02 17:24:03 +00:00
|
|
|
func continueTask(s string, def string) (cont bool) {
|
2017-08-04 09:26:53 +00:00
|
|
|
if config.NoConfirm {
|
2017-05-06 16:34:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
var postFix string
|
|
|
|
|
|
|
|
if def == "nN" {
|
|
|
|
postFix = "[Y/n] "
|
|
|
|
} else {
|
|
|
|
postFix = "[y/N] "
|
|
|
|
}
|
|
|
|
|
|
|
|
var response string
|
|
|
|
fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m", s, postFix)
|
|
|
|
|
|
|
|
n, err := fmt.Scanln(&response)
|
|
|
|
if err != nil || n == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if response == string(def[0]) || response == string(def[1]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-05-07 01:43:49 +00:00
|
|
|
// PassToPacman outsorces execution to pacman binary without modifications.
|
2017-08-02 17:24:03 +00:00
|
|
|
func passToPacman(op string, pkgs []string, flags []string) error {
|
2017-05-07 01:43:49 +00:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
var args []string
|
|
|
|
|
|
|
|
args = append(args, op)
|
|
|
|
if len(pkgs) != 0 {
|
|
|
|
args = append(args, pkgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(flags) != 0 {
|
|
|
|
args = append(args, flags...)
|
|
|
|
}
|
|
|
|
|
2017-07-05 21:30:42 +00:00
|
|
|
if strings.Contains(op, "-Q") || op == "Si" {
|
2017-08-04 09:26:53 +00:00
|
|
|
cmd = exec.Command(config.PacmanBin, args...)
|
2017-05-07 01:43:49 +00:00
|
|
|
} else {
|
2017-08-04 09:26:53 +00:00
|
|
|
args = append([]string{config.PacmanBin}, args...)
|
2017-05-07 01:43:49 +00:00
|
|
|
cmd = exec.Command("sudo", args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
return err
|
|
|
|
}
|