فهرست منبع

Simplified code

Jguer 8 سال پیش
والد
کامیت
c258986edc
3فایلهای تغییر یافته به همراه50 افزوده شده و 84 حذف شده
  1. 14 32
      aur/query.go
  2. 12 52
      pacman/pacman.go
  3. 24 0
      util/util.go

+ 14 - 32
aur/query.go

@@ -5,13 +5,17 @@ import (
 	"sort"
 	"strings"
 
-	"github.com/jguer/yay/pacman"
 	"github.com/jguer/yay/util"
 )
 
 // Query is a collection of Results
 type Query []Result
 
+type returned struct {
+	Results     Query `json:"results"`
+	ResultCount int   `json:"resultcount"`
+}
+
 func (q Query) Len() int {
 	return len(q)
 }
@@ -62,10 +66,6 @@ func (q Query) PrintSearch(start int) {
 
 // Info returns an AUR search with package details
 func Info(pkg string) (Query, int, error) {
-	type returned struct {
-		Results     Query `json:"results"`
-		ResultCount int   `json:"resultcount"`
-	}
 	r := returned{}
 
 	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
 func MultiInfo(pkgS []string) (Query, int, error) {
-	type returned struct {
-		Results     Query `json:"results"`
-		ResultCount int   `json:"resultcount"`
-	}
 	r := returned{}
 
 	var pkg string
@@ -93,18 +89,16 @@ func MultiInfo(pkgS []string) (Query, int, error) {
 
 // Search returns an AUR search
 func Search(pkgS []string, sortS bool) (Query, int, error) {
-	type returned struct {
-		Results     Query `json:"results"`
-		ResultCount int   `json:"resultcount"`
-	}
 	r := returned{}
 	err := getJSON("https://aur.archlinux.org/rpc/?v=5&type=search&arg="+pkgS[0], &r)
 
 	var aq Query
 	n := 0
-	setter := pacman.PFactory(pFSetTrue)
-	var fri int
 
+	h, _ := util.Conf.CreateHandle()
+	localDb, _ := h.LocalDb()
+
+	var fri int
 	for _, res := range r.Results {
 		match := true
 		for _, pkgN := range pkgS[1:] {
@@ -118,33 +112,21 @@ func Search(pkgS []string, sortS bool) (Query, int, error) {
 			n++
 			aq = append(aq, res)
 			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 {
 		sort.Sort(aq)
 	}
 
+	h.Release()
 	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
 func (q Query) MissingPackage(pkgS []string) {
 	for _, depName := range pkgS {

+ 12 - 52
pacman/pacman.go

@@ -23,27 +23,6 @@ type Result struct {
 	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
 func UpdatePackages(flags []string) error {
 	args := append([]string{"pacman", "-Syu"}, flags...)
@@ -56,7 +35,7 @@ func UpdatePackages(flags []string) error {
 
 // Search handles repo searches. Creates a RepoSearch struct.
 func Search(pkgInputN []string) (s Query, n int, err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	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
 func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -226,7 +186,7 @@ func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
 // BuildDependencies finds packages, on the second run
 // compares with a baselist and avoids searching those
 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()
 	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
 // repos and one of packages not found in repos. Leaves out installed packages.
 func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -324,7 +284,7 @@ func CleanRemove(pkgName []string) (err error) {
 
 	args := []string{"pacman", "-Rnsc"}
 	args = append(args, pkgName...)
-	args = append(args, "--noconfirm")
+	args = append(args, "--noutil.Conf.rm")
 
 	cmd := exec.Command("sudo", args...)
 	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
@@ -337,7 +297,7 @@ func ForeignPackages() (foreign map[string]*struct {
 	Version string
 	Date    int64
 }, n int, err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -390,7 +350,7 @@ func Statistics() (info struct {
 	var nPkg int
 	var ePkg int
 
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -422,7 +382,7 @@ func Statistics() (info struct {
 
 // BiggestPackages prints the name of the ten biggest packages in the system.
 func BiggestPackages() {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -449,7 +409,7 @@ func BiggestPackages() {
 // HangingPackages returns a list of packages installed as deps
 // and unneeded by the system
 func HangingPackages() (hanging []string, err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -480,7 +440,7 @@ func HangingPackages() (hanging []string, err error) {
 // SliceHangingPackages returns a list of packages installed as deps
 // and unneeded by the system from a provided list of package names.
 func SliceHangingPackages(pkgS []string) (hanging []string) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -517,7 +477,7 @@ big:
 
 // GetPkgbuild downloads pkgbuild from the ABS.
 func GetPkgbuild(pkgN string, path string) (err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return
@@ -549,7 +509,7 @@ func GetPkgbuild(pkgN string, path string) (err error) {
 
 //CreatePackageList appends Repo packages to completion cache
 func CreatePackageList(out *os.File) (err error) {
-	h, err := conf.CreateHandle()
+	h, err := util.Conf.CreateHandle()
 	defer h.Release()
 	if err != nil {
 		return

+ 24 - 0
util/util.go

@@ -7,6 +7,8 @@ import (
 	"os"
 	"os/exec"
 	"strings"
+
+	alpm "github.com/jguer/go-alpm"
 )
 
 // TarBin describes the default installation point of tar command.
@@ -46,6 +48,16 @@ const (
 	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.
 //If NoConfirm is set the action will continue without user input.
 func ContinueTask(s string, def string) (cont bool) {
@@ -149,3 +161,15 @@ func Editor() string {
 		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
+}