2016-11-29 03:21:20 +01:00
|
|
|
package yay
|
2016-09-13 03:06:24 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2017-04-09 03:01:06 +02:00
|
|
|
"io"
|
2016-10-05 18:04:16 +02:00
|
|
|
"math"
|
2017-04-09 03:01:06 +02:00
|
|
|
"net/http"
|
2016-09-13 03:06:24 +02:00
|
|
|
"os"
|
2016-10-02 22:50:23 +02:00
|
|
|
"os/exec"
|
2016-09-13 03:06:24 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-10-17 00:02:48 +02:00
|
|
|
|
2017-02-05 05:15:44 +01:00
|
|
|
aur "github.com/jguer/yay/aur"
|
2016-12-01 01:36:58 +01:00
|
|
|
pac "github.com/jguer/yay/pacman"
|
2017-01-05 17:13:52 +01:00
|
|
|
"github.com/jguer/yay/util"
|
2016-09-13 03:06:24 +02:00
|
|
|
)
|
|
|
|
|
2016-11-29 02:53:17 +01:00
|
|
|
// NumberMenu presents a CLI for selecting packages to install.
|
2017-02-18 16:29:40 +01:00
|
|
|
func NumberMenu(pkgS []string, flags []string) (err error) {
|
2016-09-13 03:06:24 +02:00
|
|
|
var num int
|
|
|
|
|
2017-02-18 16:29:40 +01:00
|
|
|
aq, numaq, err := aur.Search(pkgS, true)
|
2016-12-02 15:58:03 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error during AUR search:", err)
|
|
|
|
}
|
2017-02-18 16:29:40 +01:00
|
|
|
pq, numpq, err := pac.Search(pkgS)
|
2016-09-13 03:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-05 05:15:44 +01:00
|
|
|
if numpq == 0 && numaq == 0 {
|
2016-12-02 13:19:03 +01:00
|
|
|
return fmt.Errorf("no packages match search")
|
|
|
|
}
|
|
|
|
|
2017-01-05 17:13:52 +01:00
|
|
|
if util.SortMode == util.BottomUp {
|
2017-02-05 05:15:44 +01:00
|
|
|
aq.PrintSearch(numpq)
|
|
|
|
pq.PrintSearch()
|
2016-12-02 13:19:03 +01:00
|
|
|
} else {
|
2017-02-05 05:15:44 +01:00
|
|
|
pq.PrintSearch()
|
|
|
|
aq.PrintSearch(numpq)
|
2016-09-13 03:06:24 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 00:02:48 +02:00
|
|
|
fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
|
2016-09-13 03:06:24 +02:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
2017-02-17 12:55:20 +01:00
|
|
|
numberBuf, overflow, err := reader.ReadLine()
|
|
|
|
if err != nil || overflow {
|
2016-09-13 03:06:24 +02:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-17 12:55:20 +01:00
|
|
|
numberString := string(numberBuf)
|
2016-12-10 00:33:21 +01:00
|
|
|
var aurInstall []string
|
2016-12-05 01:26:01 +01:00
|
|
|
var repoInstall []string
|
2016-09-13 03:06:24 +02:00
|
|
|
result := strings.Fields(numberString)
|
|
|
|
for _, numS := range result {
|
|
|
|
num, err = strconv.Atoi(numS)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install package
|
2017-02-05 05:15:44 +01:00
|
|
|
if num > numaq+numpq-1 || num < 0 {
|
2016-12-02 13:19:03 +01:00
|
|
|
continue
|
2017-02-05 05:15:44 +01:00
|
|
|
} else if num > numpq-1 {
|
2017-01-05 17:13:52 +01:00
|
|
|
if util.SortMode == util.BottomUp {
|
2017-02-05 05:15:44 +01:00
|
|
|
aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
|
2016-12-02 13:19:03 +01:00
|
|
|
} else {
|
2017-02-05 05:15:44 +01:00
|
|
|
aurInstall = append(aurInstall, aq[num-numpq].Name)
|
2016-12-02 13:19:03 +01:00
|
|
|
}
|
2016-09-13 03:06:24 +02:00
|
|
|
} else {
|
2017-01-05 17:13:52 +01:00
|
|
|
if util.SortMode == util.BottomUp {
|
2017-02-05 05:15:44 +01:00
|
|
|
repoInstall = append(repoInstall, pq[numpq-num-1].Name)
|
2016-12-02 13:19:03 +01:00
|
|
|
} else {
|
2017-02-05 05:15:44 +01:00
|
|
|
repoInstall = append(repoInstall, pq[num].Name)
|
2016-12-02 13:19:03 +01:00
|
|
|
}
|
2016-09-13 03:06:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 01:26:01 +01:00
|
|
|
if len(repoInstall) != 0 {
|
|
|
|
pac.Install(repoInstall, flags)
|
2016-10-02 22:50:23 +02:00
|
|
|
}
|
2016-10-02 19:52:48 +02:00
|
|
|
|
2016-12-10 00:33:21 +01:00
|
|
|
if len(aurInstall) != 0 {
|
|
|
|
q, n, err := aur.MultiInfo(aurInstall)
|
2016-10-02 19:52:48 +02:00
|
|
|
if err != nil {
|
2016-12-10 00:33:21 +01:00
|
|
|
return err
|
|
|
|
} else if n != len(aurInstall) {
|
2016-12-17 01:40:51 +01:00
|
|
|
q.MissingPackage(aurInstall)
|
2016-12-10 00:33:21 +01:00
|
|
|
}
|
|
|
|
|
2016-12-17 03:29:52 +01:00
|
|
|
var finalrm []string
|
2016-12-10 00:33:21 +01:00
|
|
|
for _, aurpkg := range q {
|
2016-12-17 03:29:52 +01:00
|
|
|
finalmdeps, err := aurpkg.Install(flags)
|
|
|
|
finalrm = append(finalrm, finalmdeps...)
|
2016-12-10 00:33:21 +01:00
|
|
|
if err != nil {
|
|
|
|
// Do not abandon program, we might still be able to install the rest
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2016-10-02 19:52:48 +02:00
|
|
|
}
|
2016-12-17 03:29:52 +01:00
|
|
|
|
|
|
|
if len(finalrm) != 0 {
|
|
|
|
aur.RemoveMakeDeps(finalrm)
|
|
|
|
}
|
2016-10-02 19:52:48 +02:00
|
|
|
}
|
2016-12-10 00:33:21 +01:00
|
|
|
|
|
|
|
return nil
|
2016-09-13 03:06:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 02:53:17 +01:00
|
|
|
// Install handles package installs
|
|
|
|
func Install(pkgs []string, flags []string) error {
|
2016-12-01 01:36:58 +01:00
|
|
|
aurs, repos, _ := pac.PackageSlices(pkgs)
|
|
|
|
|
2016-12-02 19:03:31 +01:00
|
|
|
err := pac.Install(repos, flags)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error installing repo packages.")
|
|
|
|
}
|
2016-12-01 01:36:58 +01:00
|
|
|
|
|
|
|
q, n, err := aur.MultiInfo(aurs)
|
2016-12-02 19:03:31 +01:00
|
|
|
if len(aurs) != n || err != nil {
|
2016-12-01 01:36:58 +01:00
|
|
|
fmt.Println("Unable to get info on some packages")
|
2016-11-29 02:53:17 +01:00
|
|
|
}
|
|
|
|
|
2016-12-17 03:29:52 +01:00
|
|
|
var finalrm []string
|
2016-12-01 01:36:58 +01:00
|
|
|
for _, aurpkg := range q {
|
2016-12-17 03:29:52 +01:00
|
|
|
finalmdeps, err := aurpkg.Install(flags)
|
|
|
|
finalrm = append(finalrm, finalmdeps...)
|
2016-12-01 01:36:58 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error installing", aurpkg.Name, ":", err)
|
|
|
|
}
|
2016-11-29 02:53:17 +01:00
|
|
|
}
|
|
|
|
|
2016-12-17 03:29:52 +01:00
|
|
|
if len(finalrm) != 0 {
|
|
|
|
aur.RemoveMakeDeps(finalrm)
|
|
|
|
}
|
|
|
|
|
2016-11-29 02:53:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade handles updating the cache and installing updates.
|
|
|
|
func Upgrade(flags []string) error {
|
2016-12-01 01:36:58 +01:00
|
|
|
errp := pac.UpdatePackages(flags)
|
2016-12-10 00:33:21 +01:00
|
|
|
erra := aur.Upgrade(flags)
|
2016-10-02 18:23:55 +02:00
|
|
|
|
|
|
|
if errp != nil {
|
|
|
|
return errp
|
|
|
|
}
|
|
|
|
|
|
|
|
return erra
|
|
|
|
}
|
|
|
|
|
2017-02-18 16:29:40 +01:00
|
|
|
// SyncSearch presents a query to the local repos and to the AUR.
|
|
|
|
func SyncSearch(pkgS []string) (err error) {
|
|
|
|
aq, _, err := aur.Search(pkgS, true)
|
2016-09-13 03:06:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-18 16:29:40 +01:00
|
|
|
pq, _, err := pac.Search(pkgS)
|
2016-12-02 13:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-13 03:06:24 +02:00
|
|
|
|
2017-01-05 17:13:52 +01:00
|
|
|
if util.SortMode == util.BottomUp {
|
2017-02-05 05:15:44 +01:00
|
|
|
aq.PrintSearch(0)
|
|
|
|
pq.PrintSearch()
|
2016-12-02 13:19:03 +01:00
|
|
|
} else {
|
2017-02-05 05:15:44 +01:00
|
|
|
pq.PrintSearch()
|
|
|
|
aq.PrintSearch(0)
|
2016-12-02 13:19:03 +01:00
|
|
|
}
|
2016-09-13 03:06:24 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-05 18:04:16 +02:00
|
|
|
|
2017-02-18 16:29:40 +01:00
|
|
|
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
|
|
|
|
func SyncInfo(pkgS []string, flags []string) (err error) {
|
2016-12-16 03:13:17 +01:00
|
|
|
aurS, repoS, err := pac.PackageSlices(pkgS)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q, _, err := aur.MultiInfo(aurS)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, aurP := range q {
|
|
|
|
aurP.PrintInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(repoS) != 0 {
|
|
|
|
err = PassToPacman("-Si", repoS, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-29 02:53:17 +01:00
|
|
|
// LocalStatistics returns installed packages statistics.
|
2016-12-01 03:26:58 +01:00
|
|
|
func LocalStatistics(version string) error {
|
2016-12-15 02:18:07 +01:00
|
|
|
info, err := pac.Statistics()
|
2016-10-05 18:04:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-10 00:33:21 +01:00
|
|
|
foreignS, foreign, _ := pac.ForeignPackages()
|
2016-12-09 03:12:13 +01:00
|
|
|
|
2016-12-01 04:10:55 +01:00
|
|
|
fmt.Printf("\n Yay version r%s\n", version)
|
2016-10-05 18:04:16 +02:00
|
|
|
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
|
2016-12-01 01:36:58 +01:00
|
|
|
fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", info.Totaln)
|
2016-12-09 03:12:13 +01:00
|
|
|
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", foreign)
|
2016-12-01 01:36:58 +01:00
|
|
|
fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", info.Expln)
|
|
|
|
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", size(info.TotalSize))
|
2016-10-05 18:04:16 +02:00
|
|
|
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
|
|
|
|
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
|
2016-12-15 02:18:07 +01:00
|
|
|
pac.BiggestPackages()
|
2016-10-05 18:04:16 +02:00
|
|
|
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
|
|
|
|
|
2016-12-10 00:33:21 +01:00
|
|
|
keys := make([]string, len(foreignS))
|
|
|
|
i := 0
|
|
|
|
for k := range foreignS {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
q, _, err := aur.MultiInfo(keys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range q {
|
|
|
|
if res.Maintainer == "" {
|
2017-02-25 23:53:06 +01:00
|
|
|
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is orphaned.\x1b[0m\n", res.Name)
|
2016-12-10 00:33:21 +01:00
|
|
|
}
|
2016-12-16 03:13:17 +01:00
|
|
|
if res.OutOfDate != 0 {
|
2017-02-25 23:53:06 +01:00
|
|
|
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is out-of-date in AUR.\x1b[0m\n", res.Name)
|
2016-12-16 03:13:17 +01:00
|
|
|
}
|
2016-12-10 00:33:21 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:04:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function by pyk https://github.com/pyk/byten
|
|
|
|
func index(s int64) float64 {
|
|
|
|
x := math.Log(float64(s)) / math.Log(1024)
|
|
|
|
return math.Floor(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function by pyk https://github.com/pyk/byten
|
|
|
|
func countSize(s int64, i float64) float64 {
|
|
|
|
return float64(s) / math.Pow(1024, math.Floor(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size return a formated string from file size
|
|
|
|
// Function by pyk https://github.com/pyk/byten
|
2016-11-29 02:53:17 +01:00
|
|
|
func size(s int64) string {
|
2016-10-05 18:04:16 +02:00
|
|
|
|
|
|
|
symbols := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
|
|
|
|
i := index(s)
|
|
|
|
if s < 10 {
|
|
|
|
return fmt.Sprintf("%dB", s)
|
|
|
|
}
|
|
|
|
size := countSize(s, i)
|
|
|
|
format := "%.0f"
|
|
|
|
if size < 10 {
|
|
|
|
format = "%.1f"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(format+"%s", size, symbols[int(i)])
|
|
|
|
}
|
2016-12-02 13:19:03 +01:00
|
|
|
|
|
|
|
// PassToPacman outsorces execution to pacman binary without modifications.
|
|
|
|
func PassToPacman(op string, pkgs []string, flags []string) error {
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
var args []string
|
|
|
|
|
|
|
|
args = append(args, op)
|
|
|
|
if len(pkgs) != 0 {
|
|
|
|
args = append(args, pkgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(flags) != 0 {
|
|
|
|
args = append(args, flags...)
|
|
|
|
}
|
|
|
|
|
2016-12-16 03:13:17 +01:00
|
|
|
if strings.Contains(op, "-Q") || op == "-Si" {
|
2016-12-02 13:19:03 +01:00
|
|
|
cmd = exec.Command("pacman", args...)
|
|
|
|
} else {
|
|
|
|
args = append([]string{"pacman"}, args...)
|
|
|
|
cmd = exec.Command("sudo", args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
return err
|
|
|
|
}
|
2016-12-17 03:29:52 +01:00
|
|
|
|
|
|
|
// CleanDependencies removels all dangling dependencies in system
|
|
|
|
func CleanDependencies(pkgs []string) error {
|
|
|
|
hanging, err := pac.HangingPackages()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hanging) != 0 {
|
2017-01-05 17:13:52 +01:00
|
|
|
if !util.ContinueTask("Confirm Removal?", "nN") {
|
2016-12-17 03:29:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err = pac.CleanRemove(hanging)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2017-02-15 02:50:26 +01:00
|
|
|
|
|
|
|
// GetPkgbuild gets the pkgbuild of the package 'pkg' trying the ABS first and then the AUR trying the ABS first and then the AUR.
|
|
|
|
func GetPkgbuild(pkg string) (err error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wd = wd + "/"
|
|
|
|
|
|
|
|
err = pac.GetPkgbuild(pkg, wd)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = aur.GetPkgbuild(pkg, wd)
|
|
|
|
return
|
|
|
|
}
|
2017-04-09 03:01:06 +02:00
|
|
|
|
|
|
|
func Complete() (err error) {
|
|
|
|
// Get the data
|
|
|
|
resp, err := http.Get("https://aur.archlinux.org/packages.gz")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|