Bladeren bron

Use go-pacmanconf for config parsing

This moves the config parsing from out of alpm and into the
go-pacmanconf libary plus some boilerplate code to get it into our alpm
config.

This makes sense as many config options such as UseColor and CleanMethod
have nothing to do with alpm and only relate to pacman.

pacman-conf is used instead of direct config parsing. This tool resolves
defaults and includes for us, so we don't need to handle it.

It is now safe to drop all the config parsing from go-alpm.
morganamilo 6 jaren geleden
bovenliggende
commit
b2f636d93b
4 gewijzigde bestanden met toevoegingen van 135 en 29 verwijderingen
  1. 1 1
      clean.go
  2. 111 13
      config.go
  3. 2 2
      exec.go
  4. 21 13
      main.go

+ 1 - 1
clean.go

@@ -59,7 +59,7 @@ func syncClean(parser *arguments) error {
 
 	_, removeAll, _ := parser.getArg("c", "clean")
 
-	for _, v := range alpmConf.CleanMethod {
+	for _, v := range pacmanConf.CleanMethod {
 		if v == "KeepInstalled" {
 			keepInstalled = true
 		} else if v == "KeepCurrent" {

+ 111 - 13
config.go

@@ -9,6 +9,7 @@ import (
 	"os/exec"
 	"strings"
 
+	pacmanconf "github.com/Morganamilo/go-pacmanconf"
 	alpm "github.com/jguer/go-alpm"
 )
 
@@ -110,7 +111,7 @@ var shouldSaveConfig bool
 var config Configuration
 
 // AlpmConf holds the current config values for pacman.
-var alpmConf alpm.PacmanConfig
+var pacmanConf *pacmanconf.Config
 
 // AlpmHandle is the alpm handle used by yay.
 var alpmHandle *alpm.Handle
@@ -118,18 +119,6 @@ var alpmHandle *alpm.Handle
 // Mode is used to restrict yay to AUR or repo only modes
 var mode = ModeAny
 
-func readAlpmConfig(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
-}
-
 // SaveConfig writes yay config to file.
 func (config *Configuration) saveConfig() error {
 	marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
@@ -332,3 +321,112 @@ func (config Configuration) String() string {
 	}
 	return buf.String()
 }
+
+func toUsage(usages []string) alpm.Usage {
+	if len(usages) == 0 {
+		return alpm.UsageAll
+	}
+
+	var ret alpm.Usage = 0
+	for _, usage := range usages {
+		switch usage {
+		case "Sync":
+			ret |= alpm.UsageSync
+		case "Search":
+			ret |= alpm.UsageSearch
+		case "Install":
+			ret |= alpm.UsageInstall
+		case "Upgrade":
+			ret |= alpm.UsageUpgrade
+		case "All":
+			ret |= alpm.UsageAll
+		}
+	}
+
+	return ret
+}
+
+func configureAlpm(conf *pacmanconf.Config) error {
+	var err error
+
+	// TODO: set SigLevel
+	sigLevel := alpm.SigPackage | alpm.SigPackageOptional | alpm.SigDatabase | alpm.SigDatabaseOptional
+	localFileSigLevel := alpm.SigUseDefault
+	remoteFileSigLevel := alpm.SigUseDefault
+
+	for _, repo := range pacmanConf.Repos {
+		// TODO: set SigLevel
+		db, err := alpmHandle.RegisterSyncDb(repo.Name, sigLevel)
+		if err != nil {
+			return err
+		}
+
+		db.SetServers(repo.Servers)
+		db.SetUsage(toUsage(repo.Usage))
+	}
+
+	if err = alpmHandle.SetCacheDirs(pacmanConf.CacheDir...); err != nil {
+		return err
+	}
+
+	// add hook directories 1-by-1 to avoid overwriting the system directory
+	for _, dir := range pacmanConf.HookDir {
+		if err = alpmHandle.AddHookDir(dir); err != nil {
+			return err
+		}
+	}
+
+	if err = alpmHandle.SetGPGDir(pacmanConf.GPGDir); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetLogFile(pacmanConf.LogFile); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetIgnorePkgs(pacmanConf.IgnorePkg...); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetIgnoreGroups(pacmanConf.IgnoreGroup...); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetArch(pacmanConf.Architecture); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetNoUpgrades(pacmanConf.NoUpgrade...); err != nil {
+		return err
+	}
+
+	if alpmHandle.SetNoExtracts(pacmanConf.NoExtract...); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetDefaultSigLevel(sigLevel); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetLocalFileSigLevel(localFileSigLevel); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetRemoteFileSigLevel(remoteFileSigLevel); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetDeltaRatio(pacmanConf.UseDelta); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetUseSyslog(pacmanConf.UseSyslog); err != nil {
+		return err
+	}
+
+	if err = alpmHandle.SetCheckSpace(pacmanConf.CheckSpace); err != nil {
+		return err
+	}
+
+	return nil
+}

+ 2 - 2
exec.go

@@ -56,7 +56,7 @@ 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(alpmConf.DBPath, "db.lck")); err != nil {
+	if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
 		return
 	}
 
@@ -64,7 +64,7 @@ func waitLock() {
 
 	for {
 		time.Sleep(3 * time.Second)
-		if _, err := os.Stat(filepath.Join(alpmConf.DBPath, "db.lck")); err != nil {
+		if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
 			fmt.Println()
 			return
 		}

+ 21 - 13
main.go

@@ -7,6 +7,7 @@ import (
 	"path/filepath"
 	"strings"
 
+	pacmanconf "github.com/Morganamilo/go-pacmanconf"
 	alpm "github.com/jguer/go-alpm"
 )
 
@@ -101,29 +102,32 @@ func initBuildDir() error {
 
 func initAlpm() error {
 	var err error
+	var stderr string
 
-	if alpmConf, err = readAlpmConfig(config.PacmanConf); err != nil {
-		return fmt.Errorf("Unable to read Pacman conf: %s", err)
+	root := "/"
+	if value, _, exists := cmdArgs.getArg("root", "r"); exists {
+		root = value
 	}
 
-	if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
-		alpmConf.DBPath = value
+	pacmanConf, stderr, err = pacmanconf.PacmanConf("--config", config.PacmanConf, "--root", root)
+	if err != nil {
+		return fmt.Errorf("%s", stderr)
 	}
 
-	if value, _, exists := cmdArgs.getArg("root", "r"); exists {
-		alpmConf.RootDir = value
+	if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
+		pacmanConf.DBPath = value
 	}
 
 	if value, _, exists := cmdArgs.getArg("arch"); exists {
-		alpmConf.Architecture = value
+		pacmanConf.Architecture = value
 	}
 
 	if value, _, exists := cmdArgs.getArg("ignore"); exists {
-		alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
+		pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, strings.Split(value, ",")...)
 	}
 
 	if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
-		alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
+		pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, strings.Split(value, ",")...)
 	}
 
 	//TODO
@@ -131,11 +135,11 @@ func initAlpm() error {
 	//but pacman allows multiple cachdirs to be passed
 	//for now only handle one cache dir
 	if value, _, exists := cmdArgs.getArg("cachdir"); exists {
-		alpmConf.CacheDir = []string{value}
+		pacmanConf.CacheDir = []string{value}
 	}
 
 	if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
-		alpmConf.GPGDir = value
+		pacmanConf.GPGDir = value
 	}
 
 	if err = initAlpmHandle(); err != nil {
@@ -147,7 +151,7 @@ func initAlpm() error {
 	} else if value == "never" {
 		useColor = false
 	} else {
-		useColor = alpmConf.Options&alpm.ConfColor > 0
+		useColor = pacmanConf.Color
 	}
 
 	return nil
@@ -162,10 +166,14 @@ func initAlpmHandle() error {
 		}
 	}
 
-	if alpmHandle, err = alpmConf.CreateHandle(); err != nil {
+	if alpmHandle, err = alpm.Init(pacmanConf.RootDir, pacmanConf.DBPath); err != nil {
 		return fmt.Errorf("Unable to CreateHandle: %s", err)
 	}
 
+	if err = configureAlpm(pacmanConf); err != nil {
+		return err
+	}
+
 	alpmHandle.SetQuestionCallback(questionCallback)
 	alpmHandle.SetLogCallback(logCallback)
 	return nil