From 374691184f4ef9fccf8ef6d2e514816f8e82ebbd Mon Sep 17 00:00:00 2001 From: Jguer Date: Wed, 5 Oct 2016 17:04:16 +0100 Subject: [PATCH] Bug fixing flags, -Qstats implementation --- Makefile | 10 +++-- actions.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++------- aur/aur.go | 34 +++++++++++----- pacman.go | 31 +++++++------- yay.go | 26 ++++-------- 5 files changed, 156 insertions(+), 62 deletions(-) diff --git a/Makefile b/Makefile index f46c56e7..6d39de4a 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,18 @@ # Prepend our _vendor directory to the system GOPATH # so that import path resolution will prioritize # our third party snapshots. -LDFLAGS=-ldflags "-s -w" -GOFILES=$(shell ls *.go) +VERSION := $(shell git rev-list --count master) +LDFLAGS=-ldflags "-s -w -X main.version=${VERSION}" +GOFILES := $(shell ls *.go | grep -v /vendor/) BINARY=./bin/yay default: build +install: + go install -v ${LDFLAGS} ${GO_FILES} + build: - go build -v -o ${BINARY} ${LDFLAGS} ${GOFILES} + go build -v -o ${BINARY} ${LDFLAGS} ${GO_FILES} release: go build -v -o ${BINARY} ./src/main.go diff --git a/actions.go b/actions.go index f7ebdcb6..2d3175eb 100644 --- a/actions.go +++ b/actions.go @@ -2,21 +2,20 @@ package main import ( "bufio" - "bytes" - "errors" "fmt" "github.com/Jguer/go-alpm" "github.com/Jguer/yay/aur" + "math" "os" "os/exec" "strconv" "strings" ) -func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags string) (err error) { +func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags []string) (err error) { var num int var numberString string - var pacBuffer bytes.Buffer + var args []string a, err := aur.Search(pkgName, true) r, err := SearchPackages(pkgName, conf) @@ -25,11 +24,14 @@ func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags string) (er } if len(r.Results) == 0 && a.Resultcount == 0 { - return errors.New("No Packages match search") + return fmt.Errorf("No Packages match search.") } r.PrintSearch(0) a.PrintSearch(len(r.Results)) + args = append(args, "pacman") + args = append(args, "-S") + fmt.Printf("\x1B[32m%s\033[0m\nNumbers:", "Type numbers to install. Separate each number with a space.") reader := bufio.NewReader(os.Stdin) numberString, err = reader.ReadString('\n') @@ -51,18 +53,15 @@ func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags string) (er if num > len(r.Results)-1 { aurInstall = append(aurInstall, a.Results[num-len(r.Results)]) } else { - pacBuffer.WriteString(r.Results[num].Name) - pacBuffer.WriteString(" ") + args = append(args, r.Results[num].Name) } } - if pacBuffer.String() != "" { + args = append(args, flags...) + + if len(args) > 2 { var cmd *exec.Cmd - if flags == "" { - cmd = exec.Command("sudo", "pacman", "-S", strings.TrimSpace(pacBuffer.String())) - } else { - cmd = exec.Command("sudo", "pacman", "-S", strings.TrimSpace(pacBuffer.String()), flags) - } + cmd = exec.Command("sudo", args...) cmd.Stdout = os.Stdout cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr @@ -80,7 +79,7 @@ func searchAndInstall(pkgName string, conf *alpm.PacmanConfig, flags string) (er } // updateAndInstall handles updating the cache and installing updates -func updateAndInstall(conf *alpm.PacmanConfig, flags string) error { +func updateAndInstall(conf *alpm.PacmanConfig, flags []string) error { errp := UpdatePackages(flags) erra := aur.UpdatePackages(BuildDir, conf, flags) @@ -106,3 +105,93 @@ func searchMode(pkg string, conf *alpm.PacmanConfig) (err error) { return nil } + +func stats(conf *alpm.PacmanConfig) error { + var tS int64 // TotalSize + var nPkg int + var ePkg int + var pkgs [10]alpm.Package + h, err := conf.CreateHandle() + defer h.Release() + if err != nil { + return err + } + + localDb, err := h.LocalDb() + if err != nil { + return err + } + + var k int + for e, pkg := range localDb.PkgCache().Slice() { + tS += pkg.ISize() + k = -1 + nPkg++ + if pkg.Reason() == 0 { + ePkg++ + } + if e < 10 { + pkgs[e] = pkg + continue + } + + for i, pkw := range pkgs { + if k == -1 { + if pkw.ISize() < pkg.ISize() { + k = i + } + } else { + if pkw.ISize() < pkgs[k].ISize() && pkw.ISize() < pkg.ISize() { + k = i + } + } + } + + if k != -1 { + pkgs[k] = pkg + } + } + + fmt.Printf("\n Yay version r%s\n", version) + fmt.Println("\x1B[1;34m===========================================\x1B[0m") + fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", nPkg) + fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", ePkg) + fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", Size(tS)) + fmt.Println("\x1B[1;34m===========================================\x1B[0m") + fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m") + for _, pkg := range pkgs { + fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkg.Name(), Size(pkg.ISize())) + } + 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 +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)]) +} diff --git a/aur/aur.go b/aur/aur.go index a46f8cf6..cdc1ca48 100644 --- a/aur/aur.go +++ b/aur/aur.go @@ -14,6 +14,8 @@ import ( "strings" ) +var version = "undefined" + // TarBin describes the default installation point of tar command // Probably will replace untar with code solution. const TarBin string = "/usr/bin/tar" @@ -156,7 +158,7 @@ func Info(pkg string) (r Query, err error) { } // Install sends system commands to make and install a package from pkgName -func Install(pkg string, baseDir string, conf *alpm.PacmanConfig, flags string) (err error) { +func Install(pkg string, baseDir string, conf *alpm.PacmanConfig, flags []string) (err error) { info, err := Info(pkg) if err != nil { return @@ -171,7 +173,7 @@ func Install(pkg string, baseDir string, conf *alpm.PacmanConfig, flags string) } // UpdatePackages handles AUR updates -func UpdatePackages(baseDir string, conf *alpm.PacmanConfig, flags string) error { +func UpdatePackages(baseDir string, conf *alpm.PacmanConfig, flags []string) error { h, err := conf.CreateHandle() defer h.Release() if err != nil { @@ -232,7 +234,7 @@ func UpdatePackages(baseDir string, conf *alpm.PacmanConfig, flags string) error } // Install updated packages - if !strings.Contains(flags, "noconfirm") { + if NoConfirm(flags) == false { fmt.Println("\033[1m\x1b[32m==> Proceed with upgrade\033[0m\033[1m (Y/n)\033[0m") var response string fmt.Scanln(&response) @@ -249,7 +251,7 @@ func UpdatePackages(baseDir string, conf *alpm.PacmanConfig, flags string) error } // Install handles install from Result -func (a *Result) Install(baseDir string, conf *alpm.PacmanConfig, flags string) (err error) { +func (a *Result) Install(baseDir string, conf *alpm.PacmanConfig, flags []string) (err error) { // No need to use filepath.separators because it won't run on inferior platforms err = os.MkdirAll(baseDir+"builds", 0755) if err != nil { @@ -281,7 +283,7 @@ func (a *Result) Install(baseDir string, conf *alpm.PacmanConfig, flags string) dir.WriteString(a.Name) dir.WriteString("/") - if _, err := os.Stat(dir.String() + "PKGBUILD"); err == nil { + if NoConfirm(flags) == false { fmt.Println("\033[1m\x1b[32m==> Edit PKGBUILD?\033[0m\033[1m (y/N)\033[0m") fmt.Scanln(&response) if strings.ContainsAny(response, "y & Y") { @@ -299,11 +301,10 @@ func (a *Result) Install(baseDir string, conf *alpm.PacmanConfig, flags string) } var makepkgcmd *exec.Cmd - if flags == "" { - makepkgcmd = exec.Command(MakepkgBin, "-sri") - } else { - makepkgcmd = exec.Command(MakepkgBin, "-sri", flags) - } + var args []string + args = append(args, "-sri") + args = append(args, flags...) + makepkgcmd = exec.Command(MakepkgBin, args...) makepkgcmd.Stdout = os.Stdout makepkgcmd.Stderr = os.Stderr makepkgcmd.Stdin = os.Stdin @@ -402,3 +403,16 @@ func IspkgInRepo(pkgName string, conf *alpm.PacmanConfig) (bool, error) { } return false, nil } + +// NoConfirm returns true if prompts should be ignored +func NoConfirm(flags []string) bool { + noconf := false + for _, flag := range flags { + if strings.Contains(flag, "noconfirm") { + noconf = true + break + } + } + + return noconf +} diff --git a/pacman.go b/pacman.go index 0d86c608..847be3ab 100644 --- a/pacman.go +++ b/pacman.go @@ -36,7 +36,7 @@ func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) { } // InstallPackage handles package install -func InstallPackage(pkgs []string, conf *alpm.PacmanConfig, flags string) error { +func InstallPackage(pkgs []string, conf *alpm.PacmanConfig, flags []string) error { h, err := conf.CreateHandle() defer h.Release() if err != nil { @@ -71,9 +71,7 @@ func InstallPackage(pkgs []string, conf *alpm.PacmanConfig, flags string) error } } - if flags != "" { - args = append(args, flags) - } + args = append(args, flags...) if repocnt != 0 { var cmd *exec.Cmd @@ -92,13 +90,14 @@ func InstallPackage(pkgs []string, conf *alpm.PacmanConfig, flags string) error } // UpdatePackages handles cache update and upgrade -func UpdatePackages(flags string) error { +func UpdatePackages(flags []string) error { var cmd *exec.Cmd - if flags == "" { - cmd = exec.Command("sudo", "pacman", "-Syu") - } else { - cmd = exec.Command("sudo", "pacman", "-Syu", flags) - } + var args []string + + args = append(args, "pacman", "-Syu") + args = append(args, flags...) + + cmd = exec.Command("sudo", args...) cmd.Stdout = os.Stdout cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr @@ -159,20 +158,20 @@ func (s *RepoSearch) PrintSearch(mode int) { } } -func passToPacman(op string, pkg string, flags string) error { +func passToPacman(op string, pkgs []string, flags []string) error { var cmd *exec.Cmd var args []string args = append(args, op) - if pkg != "" { - args = append(args, pkg) + if len(pkgs) != 0 { + args = append(args, pkgs...) } - if flags != "" { - args = append(args, flags) + if len(flags) != 0 { + args = append(args, flags...) } - if strings.Contains(op, "Q") { + if strings.Contains(op, "-Q") { cmd = exec.Command("pacman", args...) } else { args = append([]string{"pacman"}, args...) diff --git a/yay.go b/yay.go index f27359d6..3c79353d 100644 --- a/yay.go +++ b/yay.go @@ -1,13 +1,12 @@ package main import ( - "bytes" - "errors" "fmt" "os" - "strings" ) +var version string + // PacmanConf describes the default pacman config file const PacmanConf string = "/etc/pacman.conf" @@ -19,7 +18,7 @@ const SearchMode int = -1 func operation() (operation string, err error) { if len(os.Args) < 2 { - return "noop", errors.New("No operation specified.") + return "noop", fmt.Errorf("No operation specified.") } for _, arg := range os.Args[1:] { if arg[0] == '-' && arg[1] != '-' { @@ -39,25 +38,18 @@ func packages() ([]string, error) { return ps, nil } -func flags() (flags string, err error) { - var fs []string +func flags() (fs []string, err error) { for _, arg := range os.Args[1:] { if arg[0] == '-' && arg[1] == '-' { fs = append(fs, arg) } } - if len(fs) == 0 { - return "", nil - } - - flags = strings.Join(fs, " ") return } func main() { var err error - var pkgstring bytes.Buffer conf, err := readConfig(PacmanConf) op, err := operation() @@ -71,6 +63,8 @@ func main() { flag, _ := flags() switch op { + case "-Qstats": + err = stats(&conf) case "-Ss": for _, pkg := range pkgs { err = searchMode(pkg, &conf) @@ -84,13 +78,7 @@ func main() { err = searchAndInstall(pkg, &conf, flag) } default: - for i, pkg := range pkgs { - pkgstring.WriteString(pkg) - if i != len(pkgs)-1 { - pkgstring.WriteString(" ") - } - } - err = passToPacman(op, pkgstring.String(), flag) + err = passToPacman(op, pkgs, flag) } if err != nil {