Refactor code. Implemented yay -Ssq.

This commit is contained in:
Jguer 2017-01-05 16:13:52 +00:00
parent c7e0d04ce3
commit 7e2625d362
9 changed files with 132 additions and 151 deletions

View file

@ -11,35 +11,9 @@ import (
"github.com/jguer/yay/aur" "github.com/jguer/yay/aur"
pac "github.com/jguer/yay/pacman" pac "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util"
) )
// SearchMode is search without numbers.
const SearchMode int = -1
// SortMode NumberMenu and Search
var SortMode = DownTop
// NoConfirm ignores prompts.
var NoConfirm = false
// BaseDir is the default building directory for yay
var BaseDir = "/tmp/yaytmp/"
// 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
aur.NoConfirm = NoConfirm
pac.NoConfirm = NoConfirm
aur.BaseDir = BaseDir
}
// NumberMenu presents a CLI for selecting packages to install. // NumberMenu presents a CLI for selecting packages to install.
func NumberMenu(pkgName string, flags []string) (err error) { func NumberMenu(pkgName string, flags []string) (err error) {
var num int var num int
@ -58,11 +32,11 @@ func NumberMenu(pkgName string, flags []string) (err error) {
return fmt.Errorf("no packages match search") return fmt.Errorf("no packages match search")
} }
if aur.SortMode == aur.DownTop { if util.SortMode == util.BottomUp {
a.PrintSearch(nR) a.PrintSearch(nR)
r.PrintSearch(0) r.PrintSearch()
} else { } else {
r.PrintSearch(0) r.PrintSearch()
a.PrintSearch(nR) a.PrintSearch(nR)
} }
@ -87,13 +61,13 @@ func NumberMenu(pkgName string, flags []string) (err error) {
if num > nA+nR-1 || num < 0 { if num > nA+nR-1 || num < 0 {
continue continue
} else if num > nR-1 { } else if num > nR-1 {
if aur.SortMode == aur.DownTop { if util.SortMode == util.BottomUp {
aurInstall = append(aurInstall, a[nA+nR-num-1].Name) aurInstall = append(aurInstall, a[nA+nR-num-1].Name)
} else { } else {
aurInstall = append(aurInstall, a[num-nR].Name) aurInstall = append(aurInstall, a[num-nR].Name)
} }
} else { } else {
if aur.SortMode == aur.DownTop { if util.SortMode == util.BottomUp {
repoInstall = append(repoInstall, r[nR-num-1].Name) repoInstall = append(repoInstall, r[nR-num-1].Name)
} else { } else {
repoInstall = append(repoInstall, r[num].Name) repoInstall = append(repoInstall, r[num].Name)
@ -184,12 +158,12 @@ func Search(pkg string) (err error) {
return err return err
} }
if SortMode == aur.DownTop { if util.SortMode == util.BottomUp {
a.PrintSearch(SearchMode) a.PrintSearch(0)
r.PrintSearch(SearchMode) r.PrintSearch()
} else { } else {
r.PrintSearch(SearchMode) r.PrintSearch()
a.PrintSearch(SearchMode) a.PrintSearch(0)
} }
return nil return nil
@ -326,7 +300,7 @@ func CleanDependencies(pkgs []string) error {
} }
if len(hanging) != 0 { if len(hanging) != 0 {
if !continueTask("Confirm Removal?", "nN") { if !util.ContinueTask("Confirm Removal?", "nN") {
return nil return nil
} }
err = pac.CleanRemove(hanging) err = pac.CleanRemove(hanging)
@ -334,26 +308,3 @@ func CleanDependencies(pkgs []string) error {
return err return err
} }
func continueTask(s string, def string) (cont bool) {
if NoConfirm {
return true
}
var postFix string
if def == "nN" {
postFix = "(Y/n)"
} else {
postFix = "(y/N)"
}
var response string
fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix)
fmt.Scanln(&response)
if response == string(def[0]) || response == string(def[1]) {
return false
}
return true
}

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/jguer/yay/pacman" "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util"
) )
// Install sends system commands to make and install a package from pkgName // Install sends system commands to make and install a package from pkgName
@ -61,7 +62,7 @@ func Upgrade(flags []string) error {
} }
// Install updated packages // Install updated packages
if !continueTask("Proceed with upgrade?", "nN") { if !util.ContinueTask("Proceed with upgrade?", "nN") {
return nil return nil
} }

View file

@ -5,6 +5,7 @@ import (
"sort" "sort"
"github.com/jguer/yay/pacman" "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util"
) )
// Query is a collection of Results // Query is a collection of Results
@ -15,7 +16,7 @@ func (q Query) Len() int {
} }
func (q Query) Less(i, j int) bool { func (q Query) Less(i, j int) bool {
if SortMode == DownTop { if util.SortMode == util.BottomUp {
return q[i].NumVotes < q[j].NumVotes return q[i].NumVotes < q[j].NumVotes
} }
return q[i].NumVotes > q[j].NumVotes return q[i].NumVotes > q[j].NumVotes
@ -29,12 +30,15 @@ func (q Query) Swap(i, j int) {
func (q Query) PrintSearch(start int) { func (q Query) PrintSearch(start int) {
for i, res := range q { for i, res := range q {
var toprint string var toprint string
if start != SearchMode { if util.SearchVerbosity == util.NumberMenu {
if SortMode == DownTop { if util.SortMode == util.BottomUp {
toprint += fmt.Sprintf("%d ", len(q)+start-i-1) toprint += fmt.Sprintf("%d ", len(q)+start-i-1)
} else { } else {
toprint += fmt.Sprintf("%d ", start+i) toprint += fmt.Sprintf("%d ", start+i)
} }
} else if util.SearchVerbosity == util.Minimal {
fmt.Println(res.Name)
continue
} }
toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes) toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes)
if res.Maintainer == "" { if res.Maintainer == "" {
@ -51,6 +55,8 @@ func (q Query) PrintSearch(start int) {
toprint += "\n" + res.Description toprint += "\n" + res.Description
fmt.Println(toprint) fmt.Println(toprint)
} }
return
} }
// Info returns an AUR search with package details // Info returns an AUR search with package details

View file

@ -6,6 +6,7 @@ import (
"os/exec" "os/exec"
"github.com/jguer/yay/pacman" "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util"
) )
// Result describes an AUR package. // Result describes an AUR package.
@ -95,22 +96,22 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) {
if a.Maintainer == "" { if a.Maintainer == "" {
fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m") fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m")
} }
dir := BaseDir + a.PackageBase + "/" dir := util.BaseDir + a.PackageBase + "/"
if _, err = os.Stat(dir); os.IsNotExist(err) { if _, err = os.Stat(dir); os.IsNotExist(err) {
if err = a.setupWorkspace(); err != nil { if err = a.setupWorkspace(); err != nil {
return return
} }
} else { } else {
if !continueTask("Directory exists. Clean Build?", "yY") { if !util.ContinueTask("Directory exists. Clean Build?", "yY") {
os.RemoveAll(BaseDir + a.PackageBase) os.RemoveAll(util.BaseDir + a.PackageBase)
if err = a.setupWorkspace(); err != nil { if err = a.setupWorkspace(); err != nil {
return return
} }
} }
} }
if !continueTask("Edit PKGBUILD?", "yY") { if !util.ContinueTask("Edit PKGBUILD?", "yY") {
editcmd := exec.Command(Editor, dir+"PKGBUILD") editcmd := exec.Command(Editor, dir+"PKGBUILD")
editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
editcmd.Run() editcmd.Run()
@ -127,7 +128,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) {
finalmdeps = append(finalmdeps, makeDeps[1]...) finalmdeps = append(finalmdeps, makeDeps[1]...)
if len(aurDeps) != 0 || len(repoDeps) != 0 { if len(aurDeps) != 0 || len(repoDeps) != 0 {
if !continueTask("Continue?", "nN") { if !util.ContinueTask("Continue?", "nN") {
return finalmdeps, fmt.Errorf("user did not like the dependencies") return finalmdeps, fmt.Errorf("user did not like the dependencies")
} }
} }
@ -135,7 +136,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) {
aurQ, n, err := MultiInfo(aurDeps) aurQ, n, err := MultiInfo(aurDeps)
if n != len(aurDeps) { if n != len(aurDeps) {
aurQ.MissingPackage(aurDeps) aurQ.MissingPackage(aurDeps)
if !continueTask("Continue?", "nN") { if !util.ContinueTask("Continue?", "nN") {
return finalmdeps, fmt.Errorf("unable to install dependencies") return finalmdeps, fmt.Errorf("unable to install dependencies")
} }
} }
@ -169,7 +170,7 @@ func (a *Result) Install(flags []string) (finalmdeps []string, err error) {
var args []string var args []string
args = append(args, "-sri") args = append(args, "-sri")
args = append(args, flags...) args = append(args, flags...)
makepkgcmd = exec.Command(MakepkgBin, args...) makepkgcmd = exec.Command(util.MakepkgBin, args...)
makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err = makepkgcmd.Run() err = makepkgcmd.Run()
return return
@ -238,7 +239,7 @@ func RemoveMakeDeps(depS []string) (err error) {
hanging := pacman.SliceHangingPackages(depS) hanging := pacman.SliceHangingPackages(depS)
if len(hanging) != 0 { if len(hanging) != 0 {
if !continueTask("Confirm Removal?", "nN") { if !util.ContinueTask("Confirm Removal?", "nN") {
return nil return nil
} }
err = pacman.CleanRemove(hanging) err = pacman.CleanRemove(hanging)
@ -249,21 +250,21 @@ func RemoveMakeDeps(depS []string) (err error) {
func (a *Result) setupWorkspace() (err error) { func (a *Result) setupWorkspace() (err error) {
// No need to use filepath.separators because it won't run on inferior platforms // No need to use filepath.separators because it won't run on inferior platforms
err = os.MkdirAll(BaseDir+"builds", 0755) err = os.MkdirAll(util.BaseDir+"builds", 0755)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
tarLocation := BaseDir + a.PackageBase + ".tar.gz" tarLocation := util.BaseDir + a.PackageBase + ".tar.gz"
defer os.Remove(BaseDir + a.PackageBase + ".tar.gz") defer os.Remove(util.BaseDir + a.PackageBase + ".tar.gz")
err = downloadFile(tarLocation, BaseURL+a.URLPath) err = downloadFile(tarLocation, BaseURL+a.URLPath)
if err != nil { if err != nil {
return return
} }
err = exec.Command(TarBin, "-xf", tarLocation, "-C", BaseDir).Run() err = exec.Command(util.TarBin, "-xf", tarLocation, "-C", util.BaseDir).Run()
if err != nil { if err != nil {
return return
} }

View file

@ -2,41 +2,16 @@ package aur
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
) )
// Editor gives the default system editor, uses vi in last case
var Editor = "vi"
// TarBin describes the default installation point of tar command.
const TarBin string = "/usr/bin/tar"
// BaseURL givers the AUR default address. // BaseURL givers the AUR default address.
const BaseURL string = "https://aur.archlinux.org" const BaseURL string = "https://aur.archlinux.org"
// MakepkgBin describes the default installation point of makepkg command. // Editor gives the default system editor, uses vi in last case
const MakepkgBin string = "/usr/bin/makepkg" var Editor = "vi"
// SearchMode is search without numbers.
const SearchMode int = -1
// NoConfirm ignores prompts.
var NoConfirm = false
// SortMode determines top down package or down top package display
var SortMode = DownTop
// BaseDir is the default building directory for yay
var BaseDir = "/tmp/yaytmp/"
// Describes Sorting method for numberdisplay
const (
DownTop = iota
TopDown
)
func init() { func init() {
if os.Getenv("EDITOR") != "" { if os.Getenv("EDITOR") != "" {
@ -74,26 +49,3 @@ func downloadFile(filepath string, url string) (err error) {
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
return err return err
} }
func continueTask(s string, def string) (cont bool) {
if NoConfirm {
return true
}
var postFix string
if def == "nN" {
postFix = "(Y/n)"
} else {
postFix = "(y/N)"
}
var response string
fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix)
fmt.Scanln(&response)
if response == string(def[0]) || response == string(def[1]) {
return false
}
return true
}

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"github.com/jguer/yay" "github.com/jguer/yay"
"github.com/jguer/yay/util"
) )
func usage() { func usage() {
@ -22,6 +23,7 @@ func usage() {
New operations: New operations:
yay -Qstats displays system information yay -Qstats displays system information
yay -Cd remove unneeded dependencies
New options: New options:
--topdown shows repository's packages first and then aur's --topdown shows repository's packages first and then aur's
@ -47,11 +49,11 @@ func parser() (op string, options []string, packages []string, err error) {
if arg == "--help" { if arg == "--help" {
op = arg op = arg
} else if arg == "--topdown" { } else if arg == "--topdown" {
yay.SortMode = yay.TopDown util.SortMode = util.TopDown
} else if arg == "--downtop" { } else if arg == "--bottomup" {
yay.SortMode = yay.DownTop util.SortMode = util.BottomUp
} else if arg == "--noconfirm" { } else if arg == "--noconfirm" {
yay.NoConfirm = true util.NoConfirm = true
options = append(options, arg) options = append(options, arg)
} else { } else {
options = append(options, arg) options = append(options, arg)
@ -77,14 +79,17 @@ func main() {
os.Exit(1) os.Exit(1)
} }
yay.Config()
switch op { switch op {
case "-Cd": case "-Cd":
err = yay.CleanDependencies(pkgs) err = yay.CleanDependencies(pkgs)
case "-Qstats": case "-Qstats":
err = yay.LocalStatistics(version) err = yay.LocalStatistics(version)
case "-Ss": case "-Ss", "-Ssq":
if op == "-Ss" {
util.SearchVerbosity = util.Detailed
} else {
util.SearchVerbosity = util.Minimal
}
for _, pkg := range pkgs { for _, pkg := range pkgs {
err = yay.Search(pkg) err = yay.Search(pkg)
} }
@ -95,6 +100,7 @@ func main() {
case "-Si": case "-Si":
err = yay.SingleSearch(pkgs, options) err = yay.SingleSearch(pkgs, options)
case "yogurt": case "yogurt":
util.SearchVerbosity = util.NumberMenu
for _, pkg := range pkgs { for _, pkg := range pkgs {
err = yay.NumberMenu(pkg, options) err = yay.NumberMenu(pkg, options)
break break

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/jguer/go-alpm" "github.com/jguer/go-alpm"
"github.com/jguer/yay/util"
) )
// RepoSearch describes a Repository search. // RepoSearch describes a Repository search.
@ -25,12 +26,6 @@ type Result struct {
// PacmanConf describes the default pacman config file // PacmanConf describes the default pacman config file
const PacmanConf string = "/etc/pacman.conf" const PacmanConf string = "/etc/pacman.conf"
// NoConfirm ignores prompts.
var NoConfirm = false
// SortMode NumberMenu and Search
var SortMode = DownTop
// Determines NumberMenu and Search Order // Determines NumberMenu and Search Order
const ( const (
DownTop = iota DownTop = iota
@ -90,7 +85,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
var installed bool var installed bool
dbS := dbList.Slice() dbS := dbList.Slice()
var f int var f int
if SortMode == DownTop { if util.SortMode == DownTop {
f = len(dbS) - 1 f = len(dbS) - 1
} else { } else {
f = 0 f = 0
@ -100,7 +95,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
pkgS := dbS[f].PkgCache().Slice() pkgS := dbS[f].PkgCache().Slice()
var i int var i int
if SortMode == DownTop { if util.SortMode == DownTop {
i = len(pkgS) - 1 i = len(pkgS) - 1
} else { } else {
i = 0 i = 0
@ -125,7 +120,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
n++ n++
} }
if SortMode == DownTop { if util.SortMode == DownTop {
if i > 0 { if i > 0 {
i-- i--
} else { } else {
@ -140,7 +135,7 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
} }
} }
if SortMode == DownTop { if util.SortMode == DownTop {
if f > 0 { if f > 0 {
f-- f--
} else { } else {
@ -159,15 +154,18 @@ func Search(pkgName string) (s RepoSearch, n int, err error) {
} }
//PrintSearch receives a RepoSearch type and outputs pretty text. //PrintSearch receives a RepoSearch type and outputs pretty text.
func (s RepoSearch) PrintSearch(mode int) { func (s RepoSearch) PrintSearch() {
for i, res := range s { for i, res := range s {
var toprint string var toprint string
if mode != -1 { if util.SearchVerbosity == util.NumberMenu {
if mode == 0 { if util.SortMode == util.BottomUp {
toprint += fmt.Sprintf("%d ", len(s)-i-1) toprint += fmt.Sprintf("%d ", len(s)-i-1)
} else { } else {
toprint += fmt.Sprintf("%d ", i) toprint += fmt.Sprintf("%d ", i)
} }
} else if util.SearchVerbosity == util.Minimal {
fmt.Println(res.Name)
continue
} }
toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m", toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m",
res.Repository, res.Name, res.Version) res.Repository, res.Name, res.Version)

View file

@ -1,6 +1,7 @@
package pacman package pacman
import "testing" import "testing"
import "github.com/jguer/yay/util"
func benchmarkSearch(search string, b *testing.B) { func benchmarkSearch(search string, b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@ -8,7 +9,13 @@ func benchmarkSearch(search string, b *testing.B) {
} }
} }
func BenchmarkSearchSimpleTopDown(b *testing.B) { SortMode = TopDown; benchmarkSearch("chromium", b) } func BenchmarkSearchSimpleTopDown(b *testing.B) {
func BenchmarkSearchComplexTopDown(b *testing.B) { SortMode = TopDown; benchmarkSearch("linux", b) } util.SortMode = TopDown
func BenchmarkSearchSimpleDownTop(b *testing.B) { SortMode = DownTop; benchmarkSearch("chromium", b) } benchmarkSearch("chromium", b)
func BenchmarkSearchComplexDownTop(b *testing.B) { SortMode = DownTop; benchmarkSearch("linux", b) } }
func BenchmarkSearchComplexTopDown(b *testing.B) { util.SortMode = TopDown; benchmarkSearch("linux", b) }
func BenchmarkSearchSimpleDownTop(b *testing.B) {
util.SortMode = DownTop
benchmarkSearch("chromium", b)
}
func BenchmarkSearchComplexDownTop(b *testing.B) { util.SortMode = DownTop; benchmarkSearch("linux", b) }

59
util/util.go Normal file
View file

@ -0,0 +1,59 @@
package util
import "fmt"
// TarBin describes the default installation point of tar command.
const TarBin string = "/usr/bin/tar"
// MakepkgBin describes the default installation point of makepkg command.
const MakepkgBin string = "/usr/bin/makepkg"
// SearchVerbosity determines print method used in PrintSearch
var SearchVerbosity = NumberMenu
// Verbosity settings for search
const (
NumberMenu = iota
Detailed
Minimal
)
// NoConfirm ignores prompts.
var NoConfirm = false
// SortMode determines top down package or down top package display
var SortMode = BottomUp
// BaseDir is the default building directory for yay
var BaseDir = "/tmp/yaytmp/"
// Describes Sorting method for numberdisplay
const (
BottomUp = iota
TopDown
)
// ContinueTask prompts if user wants to continue task.
//If NoConfirm is set the action will continue without user input.
func ContinueTask(s string, def string) (cont bool) {
if NoConfirm {
return true
}
var postFix string
if def == "nN" {
postFix = "(Y/n)"
} else {
postFix = "(y/N)"
}
var response string
fmt.Printf("\x1b[1;32m==> %s\x1b[1;37m %s\x1b[0m\n", s, postFix)
fmt.Scanln(&response)
if response == string(def[0]) || response == string(def[1]) {
return false
}
return true
}