mirror of
https://github.com/Jguer/yay.git
synced 2024-11-06 17:17:22 +01:00
104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jguer/yay/aur"
|
|
pac "github.com/jguer/yay/pacman"
|
|
"github.com/jguer/yay/util"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// Complete provides completion info for shells
|
|
func Complete() (err error) {
|
|
path := os.Getenv("HOME") + "/.cache/yay/aur_" + util.Shell + ".cache"
|
|
|
|
if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
|
|
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay", 0755)
|
|
|
|
out, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if aur.CreateAURList(out) != nil {
|
|
defer os.Remove(path)
|
|
}
|
|
err = pac.CreatePackageList(out)
|
|
|
|
out.Close()
|
|
return err
|
|
}
|
|
|
|
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer in.Close()
|
|
|
|
_, err = io.Copy(os.Stdout, in)
|
|
return err
|
|
}
|
|
|
|
// 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
|
|
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)])
|
|
}
|