2017-08-02 19:24:03 +02:00
|
|
|
package main
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
import (
|
2018-02-20 15:24:48 +01:00
|
|
|
"bytes"
|
2017-05-06 18:34:40 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
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"`
|
2018-03-07 22:32:55 +01:00
|
|
|
ReDownload string `json:"redownload"`
|
2018-03-07 17:29:52 +01:00
|
|
|
GitBin string `json:"gitbin"`
|
2018-03-07 18:12:30 +01:00
|
|
|
MFlags string `json:"mflags"`
|
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"`
|
2017-05-06 18:34:40 +02:00
|
|
|
}
|
|
|
|
|
2018-02-27 02:51:51 +01:00
|
|
|
var version = "3.373"
|
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"
|
|
|
|
|
|
|
|
// completionFilePrefix holds the prefix used for storing shell completion files.
|
|
|
|
const completionFilePrefix string = "aur_"
|
|
|
|
|
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-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-05 23:15:34 +01:00
|
|
|
// completion file
|
2017-10-18 04:12:16 +02:00
|
|
|
var completionFile string
|
2017-05-06 18:34:40 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
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-03-07 17:29:52 +01: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 = ""
|
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"
|
2017-05-06 18:34:40 +02:00
|
|
|
config.PacmanConf = "/etc/pacman.conf"
|
2018-03-07 18:12:30 +01:00
|
|
|
config.MFlags = ""
|
2017-05-07 03:43:49 +02:00
|
|
|
config.SortMode = BottomUp
|
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"
|
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"
|
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-03-02 05:54:38 +01:00
|
|
|
fmt.Println(bold(red("Warning:")),
|
|
|
|
bold(magenta("$EDITOR")), "is not set")
|
2018-01-26 16:04:10 +01:00
|
|
|
fmt.Println("Please add $EDITOR or to your environment variables.")
|
2017-05-06 18:34:40 +02:00
|
|
|
|
|
|
|
editorLoop:
|
2018-03-02 05:03:09 +01:00
|
|
|
fmt.Print(green("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" {
|
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-03-02 05:54:38 +01: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
|
|
|
|
|
|
|
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()
|
|
|
|
}
|