Simplified code

This commit is contained in:
Jguer 2017-05-01 02:23:26 +01:00
parent acf2a33a68
commit c258986edc
3 changed files with 50 additions and 84 deletions

View file

@ -5,13 +5,17 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/jguer/yay/pacman"
"github.com/jguer/yay/util" "github.com/jguer/yay/util"
) )
// Query is a collection of Results // Query is a collection of Results
type Query []Result type Query []Result
type returned struct {
Results Query `json:"results"`
ResultCount int `json:"resultcount"`
}
func (q Query) Len() int { func (q Query) Len() int {
return len(q) return len(q)
} }
@ -62,10 +66,6 @@ func (q Query) PrintSearch(start int) {
// Info returns an AUR search with package details // Info returns an AUR search with package details
func Info(pkg string) (Query, int, error) { func Info(pkg string) (Query, int, error) {
type returned struct {
Results Query `json:"results"`
ResultCount int `json:"resultcount"`
}
r := returned{} r := returned{}
err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &r) err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=info&arg[]="+pkg, &r)
@ -75,10 +75,6 @@ func Info(pkg string) (Query, int, error) {
// MultiInfo takes a slice of strings and returns a slice with the info of each package // MultiInfo takes a slice of strings and returns a slice with the info of each package
func MultiInfo(pkgS []string) (Query, int, error) { func MultiInfo(pkgS []string) (Query, int, error) {
type returned struct {
Results Query `json:"results"`
ResultCount int `json:"resultcount"`
}
r := returned{} r := returned{}
var pkg string var pkg string
@ -93,18 +89,16 @@ func MultiInfo(pkgS []string) (Query, int, error) {
// Search returns an AUR search // Search returns an AUR search
func Search(pkgS []string, sortS bool) (Query, int, error) { func Search(pkgS []string, sortS bool) (Query, int, error) {
type returned struct {
Results Query `json:"results"`
ResultCount int `json:"resultcount"`
}
r := returned{} r := returned{}
err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkgS[0], &r) err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkgS[0], &r)
var aq Query var aq Query
n := 0 n := 0
setter := pacman.PFactory(pFSetTrue)
var fri int
h, _ := util.Conf.CreateHandle()
localDb, _ := h.LocalDb()
var fri int
for _, res := range r.Results { for _, res := range r.Results {
match := true match := true
for _, pkgN := range pkgS[1:] { for _, pkgN := range pkgS[1:] {
@ -118,33 +112,21 @@ func Search(pkgS []string, sortS bool) (Query, int, error) {
n++ n++
aq = append(aq, res) aq = append(aq, res)
fri = len(aq) - 1 fri = len(aq) - 1
setter(aq[fri].Name, &aq[fri], false) _, err := localDb.PkgByName(res.Name)
if err == nil {
aq[fri].Installed = true
}
} }
} }
if aq != nil {
setter(aq[fri].Name, &aq[fri], true)
}
if sortS { if sortS {
sort.Sort(aq) sort.Sort(aq)
} }
h.Release()
return aq, n, err return aq, n, err
} }
// This is very dirty but it works so good.
func pFSetTrue(res interface{}) {
f, ok := res.(*Result)
if !ok {
fmt.Println("Unable to convert back to Result")
return
}
f.Installed = true
return
}
// MissingPackage warns if the Query was unable to find a package // MissingPackage warns if the Query was unable to find a package
func (q Query) MissingPackage(pkgS []string) { func (q Query) MissingPackage(pkgS []string) {
for _, depName := range pkgS { for _, depName := range pkgS {

View file

@ -23,27 +23,6 @@ type Result struct {
Installed bool Installed bool
} }
// PacmanConf describes the default pacman config file
const PacmanConf string = "/etc/pacman.conf"
var conf alpm.PacmanConfig
func init() {
conf, _ = readConfig(PacmanConf)
}
func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
file, err := os.Open(pacmanconf)
if err != nil {
return
}
conf, err = alpm.ParseConfig(file)
if err != nil {
return
}
return
}
// UpdatePackages handles cache update and upgrade // UpdatePackages handles cache update and upgrade
func UpdatePackages(flags []string) error { func UpdatePackages(flags []string) error {
args := append([]string{"pacman", "-Syu"}, flags...) args := append([]string{"pacman", "-Syu"}, flags...)
@ -56,7 +35,7 @@ func UpdatePackages(flags []string) error {
// Search handles repo searches. Creates a RepoSearch struct. // Search handles repo searches. Creates a RepoSearch struct.
func Search(pkgInputN []string) (s Query, n int, err error) { func Search(pkgInputN []string) (s Query, n int, err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
} }
@ -167,28 +146,9 @@ func (s Query) PrintSearch() {
} }
} }
// PFactory execute an action over a series of packages without reopening the handle everytime.
// Everybody told me it wouln't work. It does. It's just not pretty.
// When it worked: https://youtu.be/a4Z5BdEL0Ag?t=1m11s
func PFactory(action func(interface{})) func(name string, object interface{}, rel bool) {
h, _ := conf.CreateHandle()
localDb, _ := h.LocalDb()
return func(name string, object interface{}, rel bool) {
_, err := localDb.PkgByName(name)
if err == nil {
action(object)
}
if rel {
h.Release()
}
}
}
// PackageSlices separates an input slice into aur and repo slices // PackageSlices separates an input slice into aur and repo slices
func PackageSlices(toCheck []string) (aur []string, repo []string, err error) { func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -226,7 +186,7 @@ func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
// BuildDependencies finds packages, on the second run // BuildDependencies finds packages, on the second run
// compares with a baselist and avoids searching those // compares with a baselist and avoids searching those
func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool, last bool) (repo []string, notFound []string) { func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool, last bool) (repo []string, notFound []string) {
h, _ := conf.CreateHandle() h, _ := util.Conf.CreateHandle()
localDb, _ := h.LocalDb() localDb, _ := h.LocalDb()
dbList, _ := h.SyncDbs() dbList, _ := h.SyncDbs()
@ -266,7 +226,7 @@ func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool
// DepSatisfier receives a string slice, returns a slice of packages found in // DepSatisfier receives a string slice, returns a slice of packages found in
// repos and one of packages not found in repos. Leaves out installed packages. // repos and one of packages not found in repos. Leaves out installed packages.
func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) { func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -324,7 +284,7 @@ func CleanRemove(pkgName []string) (err error) {
args := []string{"pacman", "-Rnsc"} args := []string{"pacman", "-Rnsc"}
args = append(args, pkgName...) args = append(args, pkgName...)
args = append(args, "--noconfirm") args = append(args, "--noutil.Conf.rm")
cmd := exec.Command("sudo", args...) cmd := exec.Command("sudo", args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
@ -337,7 +297,7 @@ func ForeignPackages() (foreign map[string]*struct {
Version string Version string
Date int64 Date int64
}, n int, err error) { }, n int, err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -390,7 +350,7 @@ func Statistics() (info struct {
var nPkg int var nPkg int
var ePkg int var ePkg int
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -422,7 +382,7 @@ func Statistics() (info struct {
// BiggestPackages prints the name of the ten biggest packages in the system. // BiggestPackages prints the name of the ten biggest packages in the system.
func BiggestPackages() { func BiggestPackages() {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -449,7 +409,7 @@ func BiggestPackages() {
// HangingPackages returns a list of packages installed as deps // HangingPackages returns a list of packages installed as deps
// and unneeded by the system // and unneeded by the system
func HangingPackages() (hanging []string, err error) { func HangingPackages() (hanging []string, err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -480,7 +440,7 @@ func HangingPackages() (hanging []string, err error) {
// SliceHangingPackages returns a list of packages installed as deps // SliceHangingPackages returns a list of packages installed as deps
// and unneeded by the system from a provided list of package names. // and unneeded by the system from a provided list of package names.
func SliceHangingPackages(pkgS []string) (hanging []string) { func SliceHangingPackages(pkgS []string) (hanging []string) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -517,7 +477,7 @@ big:
// GetPkgbuild downloads pkgbuild from the ABS. // GetPkgbuild downloads pkgbuild from the ABS.
func GetPkgbuild(pkgN string, path string) (err error) { func GetPkgbuild(pkgN string, path string) (err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return
@ -549,7 +509,7 @@ func GetPkgbuild(pkgN string, path string) (err error) {
//CreatePackageList appends Repo packages to completion cache //CreatePackageList appends Repo packages to completion cache
func CreatePackageList(out *os.File) (err error) { func CreatePackageList(out *os.File) (err error) {
h, err := conf.CreateHandle() h, err := util.Conf.CreateHandle()
defer h.Release() defer h.Release()
if err != nil { if err != nil {
return return

View file

@ -7,6 +7,8 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
alpm "github.com/jguer/go-alpm"
) )
// TarBin describes the default installation point of tar command. // TarBin describes the default installation point of tar command.
@ -46,6 +48,16 @@ const (
TopDown TopDown
) )
// PacmanConf describes the default pacman config file
const PacmanConf string = "/etc/pacman.conf"
// Conf describes the default alpm config
var Conf alpm.PacmanConfig
func init() {
Conf, _ = readConfig(PacmanConf)
}
// ContinueTask prompts if user wants to continue task. // ContinueTask prompts if user wants to continue task.
//If NoConfirm is set the action will continue without user input. //If NoConfirm is set the action will continue without user input.
func ContinueTask(s string, def string) (cont bool) { func ContinueTask(s string, def string) (cont bool) {
@ -149,3 +161,15 @@ func Editor() string {
return editor return editor
} }
} }
func readConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
file, err := os.Open(pacmanconf)
if err != nil {
return
}
conf, err = alpm.ParseConfig(file)
if err != nil {
return
}
return
}