浏览代码

More porting from util to config

Jguer 8 年之前
父节点
当前提交
0473084ed2
共有 8 个文件被更改,包括 73 次插入123 次删除
  1. 2 2
      actions.go
  2. 4 4
      aur/aur.go
  3. 14 13
      aur/result.go
  4. 26 16
      config/config.go
  5. 18 77
      pacman/pacman.go
  6. 3 7
      query.go
  7. 2 2
      utils.go
  8. 4 2
      yay.go

+ 2 - 2
actions.go

@@ -5,8 +5,8 @@ import (
 	"os"
 
 	aur "github.com/jguer/yay/aur"
+	"github.com/jguer/yay/config"
 	pac "github.com/jguer/yay/pacman"
-	"github.com/jguer/yay/util"
 )
 
 // Install handles package installs
@@ -43,7 +43,7 @@ func cleanDependencies(pkgs []string) error {
 	}
 
 	if len(hanging) != 0 {
-		if !util.ContinueTask("Confirm Removal?", "nN") {
+		if !config.ContinueTask("Confirm Removal?", "nN") {
 			return nil
 		}
 		err = pac.CleanRemove(hanging)

+ 4 - 4
aur/aur.go

@@ -8,8 +8,8 @@ import (
 	"sort"
 	"strings"
 
+	"github.com/jguer/yay/config"
 	"github.com/jguer/yay/pacman"
-	"github.com/jguer/yay/util"
 	rpc "github.com/mikkeloscar/aur"
 )
 
@@ -126,7 +126,7 @@ func Upgrade(flags []string) error {
 	}
 
 	// Install updated packages
-	if !util.ContinueTask("Proceed with upgrade?", "nN") {
+	if !config.ContinueTask("Proceed with upgrade?", "nN") {
 		return nil
 	}
 
@@ -149,7 +149,7 @@ func GetPkgbuild(pkgN string, dir string) (err error) {
 	}
 
 	fmt.Printf("\x1b[1;32m==>\x1b[1;33m %s \x1b[1;32mfound in AUR.\x1b[0m\n", pkgN)
-	util.DownloadAndUnpack(BaseURL+aq[0].URLPath, dir, false)
+	config.DownloadAndUnpack(BaseURL+aq[0].URLPath, dir, false)
 	return
 }
 
@@ -167,7 +167,7 @@ func CreateAURList(out *os.File) (err error) {
 	for scanner.Scan() {
 		fmt.Print(scanner.Text())
 		out.WriteString(scanner.Text())
-		if util.Shell == "fish" {
+		if config.YayConf.Shell == "fish" {
 			fmt.Print("\tAUR\n")
 			out.WriteString("\tAUR\n")
 		} else {

+ 14 - 13
aur/result.go

@@ -5,12 +5,13 @@ import (
 	"os"
 	"os/exec"
 
+	"github.com/jguer/yay/config"
 	"github.com/jguer/yay/pacman"
 	"github.com/jguer/yay/util"
 	rpc "github.com/mikkeloscar/aur"
 )
 
-// Dependencies returns package dependencies not installed belonging to AUR
+// PkgDependencies returns package dependencies not installed belonging to AUR
 // 0 is Repo, 1 is Foreign.
 func PkgDependencies(a *rpc.Pkg) (runDeps [2][]string, makeDeps [2][]string, err error) {
 	var q Query
@@ -64,26 +65,26 @@ func printDeps(repoDeps []string, aurDeps []string) {
 	}
 }
 
-// Install handles install from Info Result.
+// PkgInstall handles install from Info Result.
 func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
 	fmt.Printf("\x1b[1;32m==> Installing\x1b[33m %s\x1b[0m\n", a.Name)
 	if a.Maintainer == "" {
 		fmt.Println("\x1b[1;31;40m==> Warning:\x1b[0;;40m This package is orphaned.\x1b[0m")
 	}
-	dir := util.BaseDir + a.PackageBase + "/"
+	dir := config.YayConf.BuildDir + a.PackageBase + "/"
 
 	if _, err = os.Stat(dir); os.IsExist(err) {
-		if !util.ContinueTask("Directory exists. Clean Build?", "yY") {
-			_ = os.RemoveAll(util.BaseDir + a.PackageBase)
+		if !config.ContinueTask("Directory exists. Clean Build?", "yY") {
+			_ = os.RemoveAll(config.YayConf.BuildDir + a.PackageBase)
 		}
 	}
 
-	if err = util.DownloadAndUnpack(BaseURL+a.URLPath, util.BaseDir, false); err != nil {
+	if err = config.DownloadAndUnpack(BaseURL+a.URLPath, config.YayConf.BuildDir, false); err != nil {
 		return
 	}
 
-	if !util.ContinueTask("Edit PKGBUILD?", "yY") {
-		editcmd := exec.Command(util.Editor(), dir+"PKGBUILD")
+	if !config.ContinueTask("Edit PKGBUILD?", "yY") {
+		editcmd := exec.Command(config.Editor(), dir+"PKGBUILD")
 		editcmd.Stdin, editcmd.Stdout, editcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
 		editcmd.Run()
 	}
@@ -99,7 +100,7 @@ func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
 	finalmdeps = append(finalmdeps, makeDeps[1]...)
 
 	if len(aurDeps) != 0 || len(repoDeps) != 0 {
-		if !util.ContinueTask("Continue?", "nN") {
+		if !config.ContinueTask("Continue?", "nN") {
 			return finalmdeps, fmt.Errorf("user did not like the dependencies")
 		}
 	}
@@ -107,7 +108,7 @@ func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
 	aurQ, _ := rpc.Info(aurDeps)
 	if len(aurQ) != len(aurDeps) {
 		(Query)(aurQ).MissingPackage(aurDeps)
-		if !util.ContinueTask("Continue?", "nN") {
+		if !config.ContinueTask("Continue?", "nN") {
 			return finalmdeps, fmt.Errorf("unable to install dependencies")
 		}
 	}
@@ -145,14 +146,14 @@ func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) {
 
 	args := []string{"-sri"}
 	args = append(args, flags...)
-	makepkgcmd := exec.Command(util.MakepkgBin, args...)
+	makepkgcmd := exec.Command(config.YayConf.MakepkgBin, args...)
 	makepkgcmd.Stdin, makepkgcmd.Stdout, makepkgcmd.Stderr = os.Stdin, os.Stdout, os.Stderr
 	err = makepkgcmd.Run()
 	return
 }
 
 // PrintInfo prints package info like pacman -Si.
-func AURPrintInfo(a *rpc.Pkg) {
+func PrintInfo(a *rpc.Pkg) {
 	fmt.Println("\x1b[1;37mRepository      :\x1b[0m", "aur")
 	fmt.Println("\x1b[1;37mName            :\x1b[0m", a.Name)
 	fmt.Println("\x1b[1;37mVersion         :\x1b[0m", a.Version)
@@ -214,7 +215,7 @@ func RemoveMakeDeps(depS []string) (err error) {
 	hanging := pacman.SliceHangingPackages(depS)
 
 	if len(hanging) != 0 {
-		if !util.ContinueTask("Confirm Removal?", "nN") {
+		if !config.ContinueTask("Confirm Removal?", "nN") {
 			return nil
 		}
 		err = pacman.CleanRemove(hanging)

+ 26 - 16
config/config.go

@@ -17,7 +17,8 @@ type Configuration struct {
 	BuildDir   string
 	Editor     string
 	MakepkgBin string
-	NoConfirm  bool
+	Shell      string
+	noConfirm  bool
 	PacmanBin  string
 	PacmanConf string
 	SortMode   string
@@ -31,30 +32,39 @@ var YayConf Configuration
 var AlpmConf alpm.PacmanConfig
 
 // AlpmHandle is the alpm handle used by yay
-var AlpmHandle alpm.Handle
+var AlpmHandle *alpm.Handle
 
 func init() {
+	var err error
 	configfile := os.Getenv("HOME") + "/.config/yay/config.json"
 
-	if _, err := os.Stat(configfile); os.IsNotExist(err) {
+	if _, err = os.Stat(configfile); os.IsNotExist(err) {
 		_ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
+		defaultSettings(&YayConf)
+	} else {
+		file, err := os.Open(configfile)
+		if err != nil {
+			fmt.Println("Error reading config:", err)
+		} else {
+			decoder := json.NewDecoder(file)
+			err = decoder.Decode(&YayConf)
+			if err != nil {
+				fmt.Println("Loading default Settings\nError reading config:", err)
+				defaultSettings(&YayConf)
+			}
+		}
 	}
 
-	file, err := os.Open(configfile)
-	if err != nil {
-		fmt.Println("Error reading config:", err)
-		return
-	}
-
-	decoder := json.NewDecoder(file)
-	err = decoder.Decode(&YayConf)
+	AlpmConf, err = readAlpmConfig(YayConf.PacmanConf)
 	if err != nil {
-		fmt.Println("Error reading config:", err)
+		fmt.Println("Unable to read Pacman conf", err)
+		os.Exit(1)
 	}
 
-	AlpmConf, err = readAlpmConfig(YayConf.PacmanConf)
+	AlpmHandle, err = AlpmConf.CreateHandle()
 	if err != nil {
-		fmt.Println("Unable to read Pacman conf", err)
+		fmt.Println("Unable to CreateHandle", err)
+		os.Exit(1)
 	}
 }
 
@@ -74,7 +84,7 @@ func defaultSettings(config *Configuration) {
 	config.BuildDir = "/tmp/yaytmp/"
 	config.Editor = ""
 	config.MakepkgBin = "/usr/bin/makepkg"
-	config.NoConfirm = false
+	config.noConfirm = false
 	config.PacmanBin = "/usr/bin/pacman"
 	config.PacmanConf = "/etc/pacman.conf"
 	config.SortMode = "BottomUp"
@@ -132,7 +142,7 @@ func Editor() string {
 // 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 YayConf.NoConfirm {
+	if YayConf.noConfirm {
 		return true
 	}
 	var postFix string

+ 18 - 77
pacman/pacman.go

@@ -7,6 +7,7 @@ import (
 	"strings"
 
 	"github.com/jguer/go-alpm"
+	"github.com/jguer/yay/config"
 	"github.com/jguer/yay/util"
 )
 
@@ -35,16 +36,11 @@ 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 := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-	}
-
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -148,13 +144,7 @@ func (s Query) PrintSearch() {
 
 // PackageSlices separates an input slice into aur and repo slices
 func PackageSlices(toCheck []string) (aur []string, repo []string, err error) {
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -186,10 +176,8 @@ 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, _ := util.Conf.CreateHandle()
-
-	localDb, _ := h.LocalDb()
-	dbList, _ := h.SyncDbs()
+	localDb, _ := config.AlpmHandle.LocalDb()
+	dbList, _ := config.AlpmHandle.SyncDbs()
 
 	f := func(c rune) bool {
 		return c == '>' || c == '<' || c == '=' || c == ' '
@@ -197,7 +185,6 @@ func BuildDependencies(baselist []string) func(toCheck []string, isBaseList bool
 
 	return func(toCheck []string, isBaseList bool, close bool) (repo []string, notFound []string) {
 		if close {
-			h.Release()
 			return
 		}
 
@@ -226,17 +213,11 @@ 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 := util.Conf.CreateHandle()
-	defer h.Release()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
-
-	localDb, err := h.LocalDb()
-	if err != nil {
-		return
-	}
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -297,17 +278,12 @@ func ForeignPackages() (foreign map[string]*struct {
 	Version string
 	Date    int64
 }, n int, err error) {
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
 
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -350,13 +326,7 @@ func Statistics() (info struct {
 	var nPkg int
 	var ePkg int
 
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
@@ -382,13 +352,8 @@ func Statistics() (info struct {
 
 // BiggestPackages prints the name of the ten biggest packages in the system.
 func BiggestPackages() {
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
 
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
@@ -409,13 +374,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 := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
@@ -440,13 +399,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 := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	localDb, err := h.LocalDb()
+	localDb, err := config.AlpmHandle.LocalDb()
 	if err != nil {
 		return
 	}
@@ -477,13 +430,7 @@ big:
 
 // GetPkgbuild downloads pkgbuild from the ABS.
 func GetPkgbuild(pkgN string, path string) (err error) {
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -500,7 +447,7 @@ func GetPkgbuild(pkgN string, path string) (err error) {
 				return fmt.Errorf("Not in standard repositories")
 			}
 			fmt.Printf("\x1b[1;32m==>\x1b[1;33m %s \x1b[1;32mfound in ABS.\x1b[0m\n", pkgN)
-			errD := util.DownloadAndUnpack(url, path, true)
+			errD := config.DownloadAndUnpack(url, path, true)
 			return errD
 		}
 	}
@@ -509,13 +456,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 := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-		return
-	}
-
-	dbList, err := h.SyncDbs()
+	dbList, err := config.AlpmHandle.SyncDbs()
 	if err != nil {
 		return
 	}
@@ -523,7 +464,7 @@ func CreatePackageList(out *os.File) (err error) {
 	p := func(pkg alpm.Package) error {
 		fmt.Print(pkg.Name())
 		out.WriteString(pkg.Name())
-		if util.Shell == "fish" {
+		if config.YayConf.Shell == "fish" {
 			fmt.Print("\t" + pkg.DB().Name() + "\n")
 			out.WriteString("\t" + pkg.DB().Name() + "\n")
 		} else {

+ 3 - 7
query.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 
 	"github.com/jguer/yay/aur"
+	"github.com/jguer/yay/config"
 	pac "github.com/jguer/yay/pacman"
 	"github.com/jguer/yay/util"
 	rpc "github.com/mikkeloscar/aur"
@@ -11,12 +12,7 @@ import (
 
 // PrintSearch handles printing search results in a given format
 func printAURSearch(q aur.Query, start int) {
-	h, err := util.Conf.CreateHandle()
-	defer h.Release()
-	if err != nil {
-	}
-
-	localDb, _ := h.LocalDb()
+	localDb, _ := config.AlpmHandle.LocalDb()
 
 	for i, res := range q {
 		var toprint string
@@ -84,7 +80,7 @@ func syncInfo(pkgS []string, flags []string) (err error) {
 	}
 
 	for _, aurP := range q {
-		aur.AURPrintInfo(&aurP)
+		aur.PrintInfo(&aurP)
 	}
 
 	if len(repoS) != 0 {

+ 2 - 2
utils.go

@@ -10,8 +10,8 @@ import (
 	"time"
 
 	"github.com/jguer/yay/aur"
+	"github.com/jguer/yay/config"
 	pac "github.com/jguer/yay/pacman"
-	"github.com/jguer/yay/util"
 )
 
 // PassToPacman outsorces execution to pacman binary without modifications.
@@ -42,7 +42,7 @@ func passToPacman(op string, pkgs []string, flags []string) error {
 
 // Complete provides completion info for shells
 func complete() (err error) {
-	path := os.Getenv("HOME") + "/.cache/yay/aur_" + util.Shell + ".cache"
+	path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.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)

+ 4 - 2
yay.go

@@ -8,6 +8,7 @@ import (
 	"strings"
 
 	"github.com/jguer/yay/aur"
+	"github.com/jguer/yay/config"
 	pac "github.com/jguer/yay/pacman"
 	"github.com/jguer/yay/util"
 )
@@ -67,11 +68,11 @@ func parser() (op string, options []string, packages []string, err error) {
 				util.SortMode = util.TopDown
 
 			case "--complete":
-				util.Shell = "sh"
+				config.YayConf.Shell = "sh"
 				complete()
 				os.Exit(0)
 			case "--fcomplete":
-				util.Shell = "fish"
+				config.YayConf.Shell = "fish"
 				complete()
 				os.Exit(0)
 			case "--help":
@@ -136,6 +137,7 @@ func main() {
 		err = passToPacman(op, pkgs, options)
 	}
 
+	config.AlpmHandle.Release()
 	if err != nil {
 		fmt.Println(err)
 		os.Exit(1)