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"
|
|
|
|
|
|
|
|
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"`
|
|
|
|
PacmanBin string `json:"pacmanbin"`
|
|
|
|
PacmanConf string `json:"pacmanconf"`
|
2017-10-19 02:30:37 +00:00
|
|
|
TarBin string `json:"tarbin"`
|
2017-07-06 23:25:49 +00:00
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
SearchMode int `json:"-"`
|
|
|
|
SortMode int `json:"sortmode"`
|
2018-02-16 13:14:59 +00:00
|
|
|
SudoLoop bool `json:"sudoloop"`
|
2017-07-06 23:25:49 +00:00
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
2018-02-16 17:44:54 +00:00
|
|
|
NoConfirm bool `json:"-"`
|
2017-10-19 02:30:37 +00:00
|
|
|
Devel bool `json:"devel"`
|
2017-12-04 06:24:20 +00:00
|
|
|
CleanAfter bool `json:"cleanAfter"`
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 10:49:16 +00:00
|
|
|
var version = "2.297"
|
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 savedInfo infos
|
2017-07-06 07:30:48 +00:00
|
|
|
|
2017-10-18 02:12:16 +00: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 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
|
|
|
|
2018-01-31 17:41:13 +00:00
|
|
|
// changedConfig holds whether or not the config has changed
|
|
|
|
var changedConfig bool
|
|
|
|
|
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.
|
2017-10-18 02:38:19 +00:00
|
|
|
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.
|
2017-10-18 02:38:19 +00:00
|
|
|
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-11-20 23:41:55 +00:00
|
|
|
in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 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) {
|
2018-02-07 11:18:19 +00:00
|
|
|
config.BuildDir = fmt.Sprintf("%s/.cache/yay/", os.Getenv("HOME"))
|
2017-12-04 02:39:23 +00:00
|
|
|
config.CleanAfter = false
|
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
|
2018-02-16 13:14:59 +00:00
|
|
|
config.SudoLoop = false
|
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:
|
2018-01-26 15:04:10 +00:00
|
|
|
fmt.Println(boldRedFgBlackBg("Warning:"),
|
|
|
|
boldYellowFgBlackBg("$EDITOR"), whiteFgBlackBg("is not set"))
|
|
|
|
fmt.Println("Please add $EDITOR or to your environment variables.")
|
2017-05-06 16:34:40 +00:00
|
|
|
|
|
|
|
editorLoop:
|
2018-01-26 15:04:10 +00:00
|
|
|
fmt.Print(greenFg("Edit PKGBUILD with:"))
|
2017-05-06 16:34:40 +00: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 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" {
|
2018-02-15 00:58:07 +00:00
|
|
|
postFix = " [Y/n] "
|
2017-05-06 16:34:40 +00:00
|
|
|
} else {
|
2018-02-15 00:58:07 +00:00
|
|
|
postFix = " [y/N] "
|
2017-05-06 16:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var response string
|
2018-02-05 20:05:58 +00:00
|
|
|
fmt.Print(boldGreenFg(arrow+" "+s+" "), boldWhiteFg(postFix))
|
2017-05-06 16:34:40 +00: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
|
|
|
|
}
|