2017-08-02 19:24:03 +02:00
|
|
|
package main
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-09-05 04:51:09 +02:00
|
|
|
"os/user"
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
|
|
|
)
|
|
|
|
|
2017-05-07 03:43:49 +02: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 18:34:40 +02:00
|
|
|
type Configuration struct {
|
2017-07-07 01:25:49 +02:00
|
|
|
BuildDir string `json:"buildDir"`
|
|
|
|
Editor string `json:"editor"`
|
|
|
|
MakepkgBin string `json:"makepkgbin"`
|
|
|
|
PacmanBin string `json:"pacmanbin"`
|
|
|
|
PacmanConf string `json:"pacmanconf"`
|
2017-10-19 04:30:37 +02:00
|
|
|
TarBin string `json:"tarbin"`
|
2017-07-07 01:25:49 +02:00
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
SearchMode int `json:"-"`
|
|
|
|
SortMode int `json:"sortmode"`
|
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
2017-10-19 04:30:37 +02:00
|
|
|
NoConfirm bool `json:"noconfirm"`
|
|
|
|
Devel bool `json:"devel"`
|
2017-12-04 07:24:20 +01:00
|
|
|
CleanAfter bool `json:"cleanAfter"`
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 11:49:16 +01:00
|
|
|
var version = "2.297"
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-04 11:26:53 +02:00
|
|
|
// baseURL givers the AUR default address.
|
|
|
|
const baseURL string = "https://aur.archlinux.org"
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
var specialDBsauce = false
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
var savedInfo infos
|
2017-07-06 09:30:48 +02:00
|
|
|
|
2017-10-18 04:12:16 +02:00
|
|
|
// configfile holds yay config file path.
|
|
|
|
var configFile string
|
|
|
|
|
|
|
|
// vcsfile holds yay vcs info file path.
|
|
|
|
var vcsFile string
|
|
|
|
|
|
|
|
//completion file
|
|
|
|
var completionFile string
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// Updated returns if database has been updated
|
|
|
|
var updated bool
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2018-01-31 18:41:13 +01:00
|
|
|
// changedConfig holds whether or not the config has changed
|
|
|
|
var changedConfig bool
|
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// YayConf holds the current config values for yay.
|
|
|
|
var config Configuration
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// AlpmConf holds the current config values for pacman.
|
2017-10-18 04:38:19 +02:00
|
|
|
var alpmConf alpm.PacmanConfig
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2017-08-02 19:24:03 +02:00
|
|
|
// AlpmHandle is the alpm handle used by yay.
|
2017-10-18 04:38:19 +02:00
|
|
|
var alpmHandle *alpm.Handle
|
2017-05-06 18:34:40 +02: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 03:43:49 +02:00
|
|
|
// SaveConfig writes yay config to file.
|
2017-08-02 19:24:03 +02:00
|
|
|
func (config *Configuration) saveConfig() error {
|
|
|
|
config.NoConfirm = false
|
|
|
|
marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
|
2017-11-21 00:41:55 +01:00
|
|
|
in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2017-05-07 03:43:49 +02: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 18:34:40 +02:00
|
|
|
func defaultSettings(config *Configuration) {
|
2017-09-05 04:51:09 +02:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
config.BuildDir = fmt.Sprintf("/tmp/yaytmp-%s/", u.Uid)
|
2017-12-04 03:39:23 +01:00
|
|
|
config.CleanAfter = false
|
2017-05-06 18:34:40 +02:00
|
|
|
config.Editor = ""
|
2017-05-09 03:09:43 +02:00
|
|
|
config.Devel = false
|
2017-05-06 18:34:40 +02:00
|
|
|
config.MakepkgBin = "/usr/bin/makepkg"
|
2017-05-07 03:43:49 +02:00
|
|
|
config.NoConfirm = false
|
2017-05-06 18:34:40 +02:00
|
|
|
config.PacmanBin = "/usr/bin/pacman"
|
|
|
|
config.PacmanConf = "/etc/pacman.conf"
|
2017-05-07 03:43:49 +02:00
|
|
|
config.SortMode = BottomUp
|
2017-05-06 18:34:40 +02:00
|
|
|
config.TarBin = "/usr/bin/bsdtar"
|
2017-05-25 01:28:35 +02:00
|
|
|
config.TimeUpdate = false
|
2017-07-07 01:25:49 +02:00
|
|
|
config.RequestSplitN = 150
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Editor returns the preferred system editor.
|
2017-08-02 19:24:03 +02:00
|
|
|
func editor() string {
|
2017-05-06 18:34:40 +02:00
|
|
|
switch {
|
2017-08-04 11:26:53 +02:00
|
|
|
case config.Editor != "":
|
|
|
|
editor, err := exec.LookPath(config.Editor)
|
2017-05-06 18:34:40 +02: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:
|
2018-01-26 16:04:10 +01:00
|
|
|
fmt.Println(boldRedFgBlackBg("Warning:"),
|
|
|
|
boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
|
|
|
|
fmt.Println("Please add $EDITOR or to your environment variables.")
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
editorLoop:
|
2018-01-26 16:04:10 +01:00
|
|
|
fmt.Print(greenFg("Edit PKGBUILD with:"))
|
2017-05-06 18:34:40 +02:00
|
|
|
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 19:24:03 +02:00
|
|
|
func continueTask(s string, def string) (cont bool) {
|
2017-08-04 11:26:53 +02:00
|
|
|
if config.NoConfirm {
|
2017-05-06 18:34:40 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
var postFix string
|
|
|
|
|
|
|
|
if def == "nN" {
|
|
|
|
postFix = "[Y/n] "
|
|
|
|
} else {
|
|
|
|
postFix = "[y/N] "
|
|
|
|
}
|
|
|
|
|
|
|
|
var response string
|
2018-02-05 21:05:58 +01:00
|
|
|
fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|