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 {
|
2017-07-07 01:25:49 +02:00
|
|
|
BuildDir string `json:"buildDir"`
|
|
|
|
Editor string `json:"editor"`
|
2018-04-09 21:50:18 +02:00
|
|
|
EditorFlags string `json:"editorflags"`
|
2017-07-07 01:25:49 +02:00
|
|
|
MakepkgBin string `json:"makepkgbin"`
|
|
|
|
PacmanBin string `json:"pacmanbin"`
|
|
|
|
PacmanConf string `json:"pacmanconf"`
|
2017-10-19 04:30:37 +02:00
|
|
|
TarBin string `json:"tarbin"`
|
2018-03-07 22:32:55 +01:00
|
|
|
ReDownload string `json:"redownload"`
|
2018-03-14 02:48:33 +01:00
|
|
|
ReBuild string `json:"rebuild"`
|
2018-04-04 22:26:07 +02:00
|
|
|
AnswerClean string `json:"answerclean"`
|
2018-06-11 20:13:40 +02:00
|
|
|
AnswerDiff string `json:"answerdiff"`
|
2018-04-04 22:26:07 +02:00
|
|
|
AnswerEdit string `json:"answeredit"`
|
|
|
|
AnswerUpgrade string `json:"answerupgrade"`
|
2018-03-07 17:29:52 +01:00
|
|
|
GitBin string `json:"gitbin"`
|
2018-03-11 04:00:45 +01:00
|
|
|
GpgBin string `json:"gpgbin"`
|
2018-03-17 21:40:24 +01:00
|
|
|
GpgFlags string `json:"gpgflags"`
|
2018-03-07 18:12:30 +01:00
|
|
|
MFlags string `json:"mflags"`
|
2018-04-04 22:26:07 +02:00
|
|
|
SortBy string `json:"sortby"`
|
2018-03-29 01:36:53 +02:00
|
|
|
GitFlags string `json:"gitflags"`
|
2017-07-07 01:25:49 +02:00
|
|
|
RequestSplitN int `json:"requestsplitn"`
|
|
|
|
SearchMode int `json:"-"`
|
|
|
|
SortMode int `json:"sortmode"`
|
2018-02-16 14:14:59 +01:00
|
|
|
SudoLoop bool `json:"sudoloop"`
|
2017-07-07 01:25:49 +02:00
|
|
|
TimeUpdate bool `json:"timeupdate"`
|
2018-02-16 18:44:54 +01:00
|
|
|
NoConfirm bool `json:"-"`
|
2017-10-19 04:30:37 +02:00
|
|
|
Devel bool `json:"devel"`
|
2017-12-04 07:24:20 +01:00
|
|
|
CleanAfter bool `json:"cleanAfter"`
|
2018-03-29 01:36:53 +02:00
|
|
|
GitClone bool `json:"gitclone"`
|
2018-05-29 18:39:38 +02:00
|
|
|
Provides bool `json:"provides"`
|
2018-05-31 17:36:36 +02:00
|
|
|
PGPFetch bool `json:"pgpfetch"`
|
2018-06-11 20:48:20 +02:00
|
|
|
UpgradeMenu bool `json:"upgrademenu"`
|
2018-06-10 16:56:57 +02:00
|
|
|
CleanMenu bool `json:"cleanmenu"`
|
|
|
|
DiffMenu bool `json:"diffmenu"`
|
|
|
|
EditMenu bool `json:"editmenu"`
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 15:17:14 +02:00
|
|
|
var version = "5.688"
|
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"
|
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-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
|
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.
|
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" {
|
2018-02-15 01:58:07 +01:00
|
|
|
postFix = " [Y/n] "
|
2017-05-06 18:34:40 +02:00
|
|
|
} else {
|
2018-02-15 01:58:07 +01:00
|
|
|
postFix = " [y/N] "
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var response string
|
2018-04-11 17:47:00 +02:00
|
|
|
fmt.Print(bold(green(arrow)+" "+s), bold(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
|
|
|
|
}
|
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()
|
|
|
|
}
|