yay/actions.go

258 lines
5.6 KiB
Go
Raw Normal View History

2016-11-29 03:21:20 +01:00
package yay
2016-09-13 03:06:24 +02:00
import (
"bufio"
"fmt"
"math"
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
"github.com/jguer/yay/aur"
2016-12-01 01:36:58 +01:00
pac "github.com/jguer/yay/pacman"
2016-09-13 03:06:24 +02:00
)
2016-11-29 02:53:17 +01:00
// BuildDir is the root for package building
const BuildDir string = "/tmp/yaytmp/"
// SearchMode is search without numbers.
const SearchMode int = -1
2016-12-02 13:19:03 +01:00
// SortMode NumberMenu and Search
var SortMode = DownTop
// Determines NumberMenu and Search Order
const (
DownTop = iota
TopDown
)
// Config copies settings over to AUR and Pacman packages
func Config() {
aur.SortMode = SortMode
pac.SortMode = SortMode
2016-12-02 13:19:03 +01:00
}
2016-11-29 02:53:17 +01:00
// NumberMenu presents a CLI for selecting packages to install.
func NumberMenu(pkgName string, flags []string) (err error) {
2016-09-13 03:06:24 +02:00
var num int
var numberString string
var args []string
2016-09-13 03:06:24 +02:00
2016-12-02 13:19:03 +01:00
a, nA, err := aur.Search(pkgName, true)
if err != nil {
fmt.Println("Error during AUR search:", err)
}
2016-12-02 13:19:03 +01:00
r, nR, err := pac.Search(pkgName)
2016-09-13 03:06:24 +02:00
if err != nil {
return
}
2016-12-02 13:19:03 +01:00
if nR == 0 && nA == 0 {
return fmt.Errorf("no packages match search")
}
if aur.SortMode == aur.DownTop {
a.PrintSearch(nR)
r.PrintSearch(0)
} else {
r.PrintSearch(0)
a.PrintSearch(nR)
2016-09-13 03:06:24 +02:00
}
2016-10-11 01:02:43 +02:00
args = append(args, "pacman", "-S")
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)
numberString, err = reader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
2016-10-02 19:52:48 +02:00
var aurInstall []aur.Result
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
2016-12-02 13:19:03 +01:00
if num > nA+nR-1 || num < 0 {
continue
} else if num > nR-1 {
if aur.SortMode == aur.DownTop {
aurInstall = append(aurInstall, a[nA+nR-num-1])
} else {
aurInstall = append(aurInstall, a[num-nR])
}
2016-09-13 03:06:24 +02:00
} else {
2016-12-02 13:19:03 +01:00
if aur.SortMode == aur.DownTop {
args = append(args, r[nR-num-1].Name)
} else {
args = append(args, r[num].Name)
}
2016-09-13 03:06:24 +02:00
}
}
args = append(args, flags...)
if len(args) > 2 {
2016-10-02 22:50:23 +02:00
var cmd *exec.Cmd
cmd = exec.Command("sudo", args...)
2016-10-02 22:50:23 +02:00
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err = cmd.Run()
}
2016-10-02 19:52:48 +02:00
for _, aurpkg := range aurInstall {
2016-12-01 01:36:58 +01:00
err = aurpkg.Install(BuildDir, flags)
2016-10-02 19:52:48 +02:00
if err != nil {
// Do not abandon program, we might still be able to install the rest
fmt.Println(err)
}
}
2016-09-13 03:06:24 +02:00
return
}
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-01 01:36:58 +01:00
for _, aurpkg := range q {
err = aurpkg.Install(BuildDir, flags)
if err != nil {
fmt.Println("Error installing", aurpkg.Name, ":", err)
}
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)
erra := aur.Upgrade(BuildDir, flags)
if errp != nil {
return errp
}
return erra
}
2016-11-29 02:53:17 +01:00
// Search presents a query to the local repos and to the AUR.
func Search(pkg string) (err error) {
2016-12-01 01:36:58 +01:00
a, _, err := aur.Search(pkg, true)
2016-09-13 03:06:24 +02:00
if err != nil {
return err
}
2016-12-02 13:19:03 +01:00
r, _, err := pac.Search(pkg)
if err != nil {
return err
}
2016-09-13 03:06:24 +02:00
if SortMode == aur.DownTop {
2016-12-02 13:19:03 +01:00
a.PrintSearch(SearchMode)
r.PrintSearch(SearchMode)
} else {
r.PrintSearch(SearchMode)
a.PrintSearch(SearchMode)
}
2016-09-13 03:06:24 +02:00
return nil
}
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-01 01:36:58 +01:00
pkgmap, info, err := pac.Statistics()
if err != nil {
return err
}
2016-12-01 04:10:55 +01:00
fmt.Printf("\n Yay version r%s\n", version)
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)
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))
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
2016-12-01 01:36:58 +01:00
for name, psize := range pkgmap {
fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", name, size(psize))
}
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
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 {
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...)
}
if strings.Contains(op, "-Q") {
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
}