ソースを参照

fix(config): pack pacmanConf into runtime

jguer 4 年 前
コミット
968b61ac57
9 ファイル変更47 行追加36 行削除
  1. 1 1
      clean.go
  2. 2 5
      config.go
  3. 6 5
      exec.go
  4. 3 2
      go.mod
  5. 15 4
      go.sum
  6. 1 1
      install.go
  7. 12 14
      main.go
  8. 5 4
      main_test.go
  9. 2 0
      pkg/settings/runtime.go

+ 1 - 1
clean.go

@@ -68,7 +68,7 @@ func syncClean(parser *settings.Arguments) error {
 
 	_, removeAll, _ := parser.GetArg("c", "clean")
 
-	for _, v := range pacmanConf.CleanMethod {
+	for _, v := range config.Runtime.PacmanConf.CleanMethod {
 		if v == "KeepInstalled" {
 			keepInstalled = true
 		} else if v == "KeepCurrent" {

+ 2 - 5
config.go

@@ -8,7 +8,7 @@ import (
 	"strings"
 
 	alpm "github.com/Jguer/go-alpm"
-	pacmanconf "github.com/Morganamilo/go-pacmanconf"
+	"github.com/Morganamilo/go-pacmanconf"
 	"github.com/leonelquinteros/gotext"
 
 	"github.com/Jguer/yay/v10/pkg/settings"
@@ -32,9 +32,6 @@ var savedInfo vcsInfo
 // YayConf holds the current config values for yay.
 var config *settings.Configuration
 
-// AlpmConf holds the current config values for pacman.
-var pacmanConf *pacmanconf.Config
-
 // AlpmHandle is the alpm handle used by yay.
 var alpmHandle *alpm.Handle
 
@@ -174,7 +171,7 @@ func toUsage(usages []string) alpm.Usage {
 	return ret
 }
 
-func configureAlpm() error {
+func configureAlpm(pacmanConf *pacmanconf.Config) error {
 	// TODO: set SigLevel
 	// sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
 	// localFileSigLevel := alpm.SigUseDefault

+ 6 - 5
exec.go

@@ -63,17 +63,18 @@ func updateSudo() {
 }
 
 // waitLock will lock yay checking the status of db.lck until it does not exist
-func waitLock() {
-	if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
+func waitLock(dbPath string) {
+	lockDBPath := filepath.Join(dbPath, "db.lck")
+	if _, err := os.Stat(lockDBPath); err != nil {
 		return
 	}
 
-	text.Warnln(gotext.Get("%s is present.", filepath.Join(pacmanConf.DBPath, "db.lck")))
+	text.Warnln(gotext.Get("%s is present.", lockDBPath))
 	text.Warn(gotext.Get("There may be another Pacman instance running. Waiting..."))
 
 	for {
 		time.Sleep(3 * time.Second)
-		if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
+		if _, err := os.Stat(lockDBPath); err != nil {
 			fmt.Println()
 			return
 		}
@@ -101,7 +102,7 @@ func passToPacman(args *settings.Arguments) *exec.Cmd {
 	argArr = append(argArr, args.Targets...)
 
 	if args.NeedRoot(config.Runtime) {
-		waitLock()
+		waitLock(config.Runtime.PacmanConf.DBPath)
 	}
 	return exec.Command(argArr[0], argArr[1:]...)
 }

+ 3 - 2
go.mod

@@ -7,8 +7,9 @@ require (
 	github.com/leonelquinteros/gotext v1.4.0
 	github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656
 	github.com/pkg/errors v0.9.1
-	golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9
-	golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
+	github.com/stretchr/testify v1.6.1
+	golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
+	golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
 )
 
 go 1.14

+ 15 - 4
go.sum

@@ -4,19 +4,30 @@ github.com/Morganamilo/go-pacmanconf v0.0.0-20180910220353-9c5265e1b14f h1:ptFKy
 github.com/Morganamilo/go-pacmanconf v0.0.0-20180910220353-9c5265e1b14f/go.mod h1:Hk55m330jNiwxRodIlMCvw5iEyoRUCIY64W1p9D+tHc=
 github.com/Morganamilo/go-srcinfo v1.0.0 h1:Wh4nEF+HJWo+29hnxM18Q2hi+DUf0GejS13+Wg+dzmI=
 github.com/Morganamilo/go-srcinfo v1.0.0/go.mod h1:MP6VGY1NNpVUmYIEgoM9acix95KQqIRyqQ0hCLsyYUY=
+github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/leonelquinteros/gotext v1.4.0 h1:2NHPCto5IoMXbrT0bldPrxj0qM5asOCwtb1aUQZ1tys=
 github.com/leonelquinteros/gotext v1.4.0/go.mod h1:yZGXREmoGTtBvZHNcc+Yfug49G/2spuF/i/Qlsvz1Us=
 github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656 h1:j679+jxcDkCFblYk+I+G71HQTFxM3PacYbVCiYmhRhU=
 github.com/mikkeloscar/aur v0.0.0-20200113170522-1cb4e2949656/go.mod h1:nYOKcK8tIj69ZZ8uDOWoiT+L25NvlOQaraDqTec/idA=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
+github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
-golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
-golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
+golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

+ 1 - 1
install.go

@@ -85,7 +85,7 @@ func install(parser *settings.Arguments) (err error) {
 
 	// we may have done -Sy, our handle now has an old
 	// database.
-	err = initAlpmHandle()
+	err = initAlpmHandle(config.Runtime.PacmanConf)
 	if err != nil {
 		return err
 	}

+ 12 - 14
main.go

@@ -74,18 +74,15 @@ func initBuildDir() error {
 	return nil
 }
 
-func initAlpm(pacmanConfigPath string) error {
-	var err error
-	var stderr string
-
+func initAlpm(pacmanConfigPath string) (*pacmanconf.Config, error) {
 	root := "/"
 	if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
 		root = value
 	}
 
-	pacmanConf, stderr, err = pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
+	pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
 	if err != nil {
-		return fmt.Errorf("%s", stderr)
+		return nil, fmt.Errorf("%s", stderr)
 	}
 
 	if value, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
@@ -116,8 +113,8 @@ func initAlpm(pacmanConfigPath string) error {
 		pacmanConf.GPGDir = value
 	}
 
-	if err := initAlpmHandle(); err != nil {
-		return err
+	if err := initAlpmHandle(pacmanConf); err != nil {
+		return nil, err
 	}
 
 	switch value, _, _ := cmdArgs.GetArg("color"); value {
@@ -131,10 +128,10 @@ func initAlpm(pacmanConfigPath string) error {
 		text.UseColor = pacmanConf.Color && isTty()
 	}
 
-	return nil
+	return pacmanConf, nil
 }
 
-func initAlpmHandle() error {
+func initAlpmHandle(pacmanConf *pacmanconf.Config) error {
 	if alpmHandle != nil {
 		if errRelease := alpmHandle.Release(); errRelease != nil {
 			return errRelease
@@ -146,7 +143,7 @@ func initAlpmHandle() error {
 		return errors.New(gotext.Get("unable to CreateHandle: %s", err))
 	}
 
-	if err := configureAlpm(); err != nil {
+	if err := configureAlpm(pacmanConf); err != nil {
 		return err
 	}
 
@@ -189,15 +186,16 @@ func main() {
 	exitOnError(initConfig(runtime.ConfigPath))
 	exitOnError(cmdArgs.ParseCommandLine(config))
 	if config.Runtime.SaveConfig {
-		err := config.SaveConfig(runtime.ConfigPath)
-		if err != nil {
+		errS := config.SaveConfig(runtime.ConfigPath)
+		if errS != nil {
 			fmt.Fprintln(os.Stderr, err)
 		}
 	}
 	config.ExpandEnv()
 	exitOnError(initBuildDir())
 	exitOnError(initVCS(runtime.VCSPath))
-	exitOnError(initAlpm(config.PacmanConf))
+	config.Runtime.PacmanConf, err = initAlpm(config.PacmanConf)
+	exitOnError(err)
 	exitOnError(handleCmd())
 	os.Exit(cleanup())
 }

+ 5 - 4
main_test.go

@@ -3,6 +3,8 @@ package main
 import (
 	"reflect"
 	"testing"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func expect(t *testing.T, field string, a interface{}, b interface{}, err error) {
@@ -14,10 +16,9 @@ func expect(t *testing.T, field string, a interface{}, b interface{}, err error)
 }
 
 func TestInitAlpm(t *testing.T) {
-	err := initAlpm("testdata/pacman.conf")
-	if err != nil {
-		t.Fatal(err)
-	}
+	pacmanConf, err := initAlpm("testdata/pacman.conf")
+	assert.Nil(t, err)
+	assert.NotNil(t, pacmanConf)
 
 	h := alpmHandle
 

+ 2 - 0
pkg/settings/runtime.go

@@ -4,6 +4,7 @@ import (
 	"os"
 	"path/filepath"
 
+	"github.com/Morganamilo/go-pacmanconf"
 	"github.com/leonelquinteros/gotext"
 	"github.com/pkg/errors"
 )
@@ -30,6 +31,7 @@ type Runtime struct {
 	CompletionPath string
 	ConfigPath     string
 	VCSPath        string
+	PacmanConf     *pacmanconf.Config
 }
 
 func MakeRuntime() (*Runtime, error) {