2017-08-02 19:24:03 +02:00
|
|
|
package main
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
import (
|
2018-04-04 22:26:07 +02:00
|
|
|
"bufio"
|
2018-02-20 15:24:48 +01:00
|
|
|
"bytes"
|
2017-05-06 18:34:40 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-04-09 21:50:18 +02:00
|
|
|
"strings"
|
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
|
|
|
|
)
|
|
|
|
|
2018-05-31 17:23:15 +02:00
|
|
|
type targetMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ModeAUR targetMode = iota
|
|
|
|
ModeRepo
|
|
|
|
ModeAny
|
|
|
|
)
|
|
|
|
|
2017-05-07 03:43:49 +02:00
|
|
|
// Configuration stores yay's config.
|
2017-05-06 18:34:40 +02:00
|
|
|
type Configuration struct {
|
2018-06-22 15:44:38 +02:00
|
|
|
BuildDir string `json:"buildDir"`
|
|
|
|
Editor string `json:"editor"`
|
|
|
|
EditorFlags string `json:"editorflags"`
|
|
|
|
MakepkgBin string `json:"makepkgbin"`
|
2018-07-18 01:53:53 +02:00
|
|
|
MakepkgConf string `json:"makepkgconf"`
|
2018-06-22 15:44:38 +02:00
|
|
|
PacmanBin string `json:"pacmanbin"`
|
|
|
|
PacmanConf string `json:"pacmanconf"`
|
|
|
|
TarBin string `json:"tarbin"`
|
|
|
|
ReDownload string `json:"redownload"`
|
|
|
|
ReBuild string `json:"rebuild"`
|
|
|
|
AnswerClean string `json:"answerclean"`
|
|
|
|
AnswerDiff string `json:"answerdiff"`
|
|
|
|
AnswerEdit string `json:"answeredit"`
|
|
|
|
AnswerUpgrade string `json:"answerupgrade"`
|
|
|
|
GitBin string `json:"gitbin"`
|
|
|
|
GpgBin string `json:"gpgbin"`
|
|
|
|
GpgFlags string `json:"gpgflags"`
|
|
|
|
MFlags string `json:"mflags"`
|
|
|
|
SortBy string `json:"sortby"`
|
|
|
|
GitFlags string `json:"gitflags"`
|
2018-07-21 15:20:00 +02:00
|
|
|
RemoveMake string `json:"removemake"`
|
2018-06-22 15:44:38 +02:00
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
SearchMode int `json:"-"`
|
|
|
|
SortMode int `json:"sortmode"`
|
|
|
|
SudoLoop bool `json:"sudoloop"`
|
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
|
|
|
NoConfirm bool `json:"-"`
|
|
|
|
Devel bool `json:"devel"`
|
|
|
|
CleanAfter bool `json:"cleanAfter"`
|
|
|
|
GitClone bool `json:"gitclone"`
|
|
|
|
Provides bool `json:"provides"`
|
|
|
|
PGPFetch bool `json:"pgpfetch"`
|
|
|
|
UpgradeMenu bool `json:"upgrademenu"`
|
|
|
|
CleanMenu bool `json:"cleanmenu"`
|
|
|
|
DiffMenu bool `json:"diffmenu"`
|
|
|
|
EditMenu bool `json:"editmenu"`
|
|
|
|
CombinedUpgrade bool `json:"combinedupgrade"`
|
|
|
|
UseAsk bool `json:"useask"`
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:39 +02:00
|
|
|
var version = "7.885"
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2018-03-06 00:53:03 +01:00
|
|
|
// configFileName holds the name of the config file.
|
|
|
|
const configFileName string = "config.json"
|
|
|
|
|
|
|
|
// vcsFileName holds the name of the vcs file.
|
|
|
|
const vcsFileName string = "vcs.json"
|
|
|
|
|
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
|
|
|
|
2018-03-21 06:34:54 +01:00
|
|
|
// useColor enables/disables colored printing
|
|
|
|
var useColor bool
|
|
|
|
|
2018-03-07 06:20:10 +01:00
|
|
|
// configHome handles config directory home
|
|
|
|
var configHome string
|
|
|
|
|
|
|
|
// cacheHome handles cache home
|
|
|
|
var cacheHome string
|
|
|
|
|
2018-03-05 23:15:34 +01:00
|
|
|
// savedInfo holds the current vcs info
|
|
|
|
var savedInfo vcsInfo
|
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
|
|
|
|
|
2018-03-07 23:09:03 +01:00
|
|
|
// shouldSaveConfig holds whether or not the config should be saved
|
|
|
|
var shouldSaveConfig bool
|
2018-01-31 18:41:13 +01:00
|
|
|
|
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
|
|
|
|
2018-05-31 17:23:15 +02:00
|
|
|
// Mode is used to restrict yay to AUR or repo only modes
|
|
|
|
var mode targetMode = ModeAny
|
|
|
|
|
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 {
|
|
|
|
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) {
|
2018-04-17 02:32:31 +02:00
|
|
|
config.BuildDir = cacheHome
|
2017-12-04 03:39:23 +01:00
|
|
|
config.CleanAfter = false
|
2017-05-06 18:34:40 +02:00
|
|
|
config.Editor = ""
|
2018-04-09 21:50:18 +02:00
|
|
|
config.EditorFlags = ""
|
2017-05-09 03:09:43 +02:00
|
|
|
config.Devel = false
|
2018-03-07 17:29:52 +01:00
|
|
|
config.MakepkgBin = "makepkg"
|
2018-07-18 01:53:53 +02:00
|
|
|
config.MakepkgConf = ""
|
2017-05-07 03:43:49 +02:00
|
|
|
config.NoConfirm = false
|
2018-03-07 17:29:52 +01:00
|
|
|
config.PacmanBin = "pacman"
|
2018-05-31 17:36:36 +02:00
|
|
|
config.PGPFetch = true
|
2017-05-06 18:34:40 +02:00
|
|
|
config.PacmanConf = "/etc/pacman.conf"
|
2018-03-17 21:40:24 +01:00
|
|
|
config.GpgFlags = ""
|
2018-03-07 18:12:30 +01:00
|
|
|
config.MFlags = ""
|
2018-03-29 01:36:53 +02:00
|
|
|
config.GitFlags = ""
|
2017-05-07 03:43:49 +02:00
|
|
|
config.SortMode = BottomUp
|
2018-04-04 22:26:07 +02:00
|
|
|
config.SortBy = "votes"
|
2018-02-16 14:14:59 +01:00
|
|
|
config.SudoLoop = false
|
2018-03-07 17:29:52 +01:00
|
|
|
config.TarBin = "bsdtar"
|
|
|
|
config.GitBin = "git"
|
2018-03-11 04:00:45 +01:00
|
|
|
config.GpgBin = "gpg"
|
2017-05-25 01:28:35 +02:00
|
|
|
config.TimeUpdate = false
|
2017-07-07 01:25:49 +02:00
|
|
|
config.RequestSplitN = 150
|
2018-03-07 22:32:55 +01:00
|
|
|
config.ReDownload = "no"
|
2018-03-14 02:48:33 +01:00
|
|
|
config.ReBuild = "no"
|
2018-04-04 22:26:07 +02:00
|
|
|
config.AnswerClean = ""
|
2018-06-11 20:13:40 +02:00
|
|
|
config.AnswerDiff = ""
|
2018-04-04 22:26:07 +02:00
|
|
|
config.AnswerEdit = ""
|
|
|
|
config.AnswerUpgrade = ""
|
2018-07-21 15:20:00 +02:00
|
|
|
config.RemoveMake = "ask"
|
2018-03-29 01:36:53 +02:00
|
|
|
config.GitClone = true
|
2018-05-29 18:39:38 +02:00
|
|
|
config.Provides = true
|
2018-06-11 20:48:20 +02:00
|
|
|
config.UpgradeMenu = true
|
2018-06-11 20:50:03 +02:00
|
|
|
config.CleanMenu = true
|
2018-06-10 16:56:57 +02:00
|
|
|
config.DiffMenu = true
|
|
|
|
config.EditMenu = false
|
2018-06-22 16:17:34 +02:00
|
|
|
config.UseAsk = false
|
2018-06-22 15:44:38 +02:00
|
|
|
config.CombinedUpgrade = false
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Editor returns the preferred system editor.
|
2018-04-09 21:50:18 +02:00
|
|
|
func editor() (string, []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 {
|
2018-04-09 21:50:18 +02:00
|
|
|
return editor, strings.Fields(config.EditorFlags)
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("EDITOR") != "":
|
2018-04-09 21:50:18 +02:00
|
|
|
editorArgs := strings.Fields(os.Getenv("EDITOR"))
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
2017-05-06 18:34:40 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2018-04-09 21:50:18 +02:00
|
|
|
return editor, editorArgs[1:]
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case os.Getenv("VISUAL") != "":
|
2018-04-09 21:50:18 +02:00
|
|
|
editorArgs := strings.Fields(os.Getenv("VISUAL"))
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
2017-05-06 18:34:40 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else {
|
2018-04-09 21:50:18 +02:00
|
|
|
return editor, editorArgs[1:]
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
2018-04-09 22:02:44 +02:00
|
|
|
fmt.Println()
|
2018-04-11 05:43:51 +02:00
|
|
|
fmt.Println(bold(red(arrow)), bold(cyan("$EDITOR")), bold("is not set"))
|
|
|
|
fmt.Println(bold(red(arrow)) + bold(" Please add ") + bold(cyan("$EDITOR")) + bold(" or ") + bold(cyan("$VISUAL")) + bold(" to your environment variables."))
|
2018-04-09 22:02:44 +02:00
|
|
|
|
|
|
|
for {
|
2018-04-11 05:43:51 +02:00
|
|
|
fmt.Print(green(bold(arrow + " Edit PKGBUILD with: ")))
|
2018-04-09 22:02:44 +02:00
|
|
|
editorInput, err := getInput("")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
editorArgs := strings.Fields(editorInput)
|
|
|
|
|
|
|
|
editor, err := exec.LookPath(editorArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return editor, editorArgs[1:]
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContinueTask prompts if user wants to continue task.
|
|
|
|
//If NoConfirm is set the action will continue without user input.
|
2018-07-22 01:46:55 +02:00
|
|
|
func continueTask(s string, cont bool) bool {
|
2017-08-04 11:26:53 +02:00
|
|
|
if config.NoConfirm {
|
2018-07-22 01:46:55 +02:00
|
|
|
return cont
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
2018-07-22 01:46:55 +02:00
|
|
|
|
|
|
|
var response string
|
2017-05-06 18:34:40 +02:00
|
|
|
var postFix string
|
2018-07-22 01:46:55 +02:00
|
|
|
yes := "yes"
|
|
|
|
no := "no"
|
|
|
|
y := string([]rune(yes)[0])
|
|
|
|
n := string([]rune(no)[0])
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2018-07-22 01:46:55 +02:00
|
|
|
if cont {
|
|
|
|
postFix = fmt.Sprintf(" [%s/%s] ", strings.ToUpper(y), n)
|
2017-05-06 18:34:40 +02:00
|
|
|
} else {
|
2018-07-22 01:46:55 +02:00
|
|
|
postFix = fmt.Sprintf(" [%s/%s] ", y, strings.ToUpper(n))
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 17:47:00 +02:00
|
|
|
fmt.Print(bold(green(arrow)+" "+s), bold(postFix))
|
2017-05-06 18:34:40 +02:00
|
|
|
|
2018-07-22 01:46:55 +02:00
|
|
|
len, err := fmt.Scanln(&response)
|
|
|
|
if err != nil || len == 0 {
|
|
|
|
return cont
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-22 01:46:55 +02:00
|
|
|
response = strings.ToLower(response)
|
|
|
|
return response == yes || response == y
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
2018-02-20 15:24:48 +01:00
|
|
|
|
2018-04-04 22:26:07 +02:00
|
|
|
func getInput(defaultValue string) (string, error) {
|
2018-04-04 23:22:33 +02:00
|
|
|
if defaultValue != "" || config.NoConfirm {
|
2018-04-04 22:26:07 +02:00
|
|
|
fmt.Println(defaultValue)
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
buf, overflow, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if overflow {
|
|
|
|
return "", fmt.Errorf("Input too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:24:48 +01:00
|
|
|
func (config Configuration) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
if err := enc.Encode(config); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|