diff --git a/actions.go b/actions.go index 456cc87e..d808d41c 100644 --- a/actions.go +++ b/actions.go @@ -13,7 +13,7 @@ import ( func install(pkgs []string, flags []string) error { aurs, repos, _ := pac.PackageSlices(pkgs) - err := pac.Install(repos, flags) + err := config.PassToPacman("-S", repos, flags) if err != nil { fmt.Println("Error installing repo packages.") } @@ -25,7 +25,7 @@ func install(pkgs []string, flags []string) error { // Upgrade handles updating the cache and installing updates. func upgrade(flags []string) error { - errp := pac.UpdatePackages(flags) + errp := config.PassToPacman("-Syu", nil, flags) erra := aur.Upgrade(flags) if errp != nil { diff --git a/aur/query.go b/aur/query.go index 45ee406c..80b00088 100644 --- a/aur/query.go +++ b/aur/query.go @@ -3,7 +3,7 @@ package aur import ( "fmt" - "github.com/jguer/yay/util" + "github.com/jguer/yay/config" rpc "github.com/mikkeloscar/aur" ) @@ -15,7 +15,7 @@ func (q Query) Len() int { } func (q Query) Less(i, j int) bool { - if util.SortMode == util.BottomUp { + if config.YayConf.SortMode == config.BottomUp { return q[i].NumVotes < q[j].NumVotes } return q[i].NumVotes > q[j].NumVotes diff --git a/aur/result.go b/aur/result.go index db523450..e4efe18a 100644 --- a/aur/result.go +++ b/aur/result.go @@ -7,7 +7,6 @@ import ( "github.com/jguer/yay/config" "github.com/jguer/yay/pacman" - "github.com/jguer/yay/util" rpc "github.com/mikkeloscar/aur" ) @@ -114,14 +113,14 @@ func PkgInstall(a *rpc.Pkg, flags []string) (finalmdeps []string, err error) { } var depArgs []string - if util.NoConfirm { + if config.YayConf.NoConfirm { depArgs = []string{"--asdeps", "--noconfirm"} } else { depArgs = []string{"--asdeps"} } // Repo dependencies if len(repoDeps) != 0 { - errR := pacman.Install(repoDeps, depArgs) + errR := config.PassToPacman("-S", repoDeps, depArgs) if errR != nil { return finalmdeps, errR } diff --git a/config/config.go b/config/config.go index d8dd73da..84ade406 100644 --- a/config/config.go +++ b/config/config.go @@ -12,17 +12,31 @@ import ( alpm "github.com/jguer/go-alpm" ) -// Configuration stores yay's config +// Verbosity settings for search +const ( + NumberMenu = iota + Detailed + Minimal +) + +// Describes Sorting method for numberdisplay +const ( + BottomUp = iota + TopDown +) + +// Configuration stores yay's config. type Configuration struct { - BuildDir string - Editor string - MakepkgBin string - Shell string - noConfirm bool - PacmanBin string - PacmanConf string - SortMode string - TarBin string + BuildDir string `json:"buildDir"` + Editor string `json:"editor"` + MakepkgBin string `json:"makepkgbin"` + Shell string `json:"-"` + NoConfirm bool `json:"noconfirm"` + PacmanBin string `json:"pacmanbin"` + PacmanConf string `json:"pacmanconf"` + SearchMode int `json:"-"` + SortMode int `json:"sortmode"` + TarBin string `json:"tarbin"` } // YayConf holds the current config values for yay. @@ -31,7 +45,7 @@ var YayConf Configuration // AlpmConf holds the current config values for pacman. var AlpmConf alpm.PacmanConfig -// AlpmHandle is the alpm handle used by yay +// AlpmHandle is the alpm handle used by yay. var AlpmHandle *alpm.Handle func init() { @@ -80,14 +94,32 @@ func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) { return } +// SaveConfig writes yay config to file. +func SaveConfig() error { + YayConf.NoConfirm = false + configfile := os.Getenv("HOME") + "/.config/yay/config.json" + marshalledinfo, _ := json.Marshal(YayConf) + in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0755) + if err != nil { + return err + } + defer in.Close() + _, err = in.Write(marshalledinfo) + if err != nil { + return err + } + err = in.Sync() + return err +} + 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" + config.SortMode = BottomUp config.TarBin = "/usr/bin/bsdtar" } @@ -142,7 +174,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 @@ -219,3 +251,29 @@ func DownloadAndUnpack(url string, path string, trim bool) (err error) { return } + +// PassToPacman outsorces execution to pacman binary without modifications. +func PassToPacman(op string, pkgs []string, flags []string) error { + var cmd *exec.Cmd + var args []string + + args = append(args, op) + if len(pkgs) != 0 { + args = append(args, pkgs...) + } + + if len(flags) != 0 { + args = append(args, flags...) + } + + if strings.Contains(op, "-Q") { + cmd = exec.Command(YayConf.PacmanBin, args...) + } else { + args = append([]string{YayConf.PacmanBin}, args...) + cmd = exec.Command("sudo", args...) + } + + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr + err := cmd.Run() + return err +} diff --git a/pacman/pacman.go b/pacman/pacman.go index ef63fa92..7b32a878 100644 --- a/pacman/pacman.go +++ b/pacman/pacman.go @@ -3,12 +3,10 @@ package pacman import ( "fmt" "os" - "os/exec" "strings" "github.com/jguer/go-alpm" "github.com/jguer/yay/config" - "github.com/jguer/yay/util" ) // Query describes a Repository search. @@ -24,16 +22,6 @@ type Result struct { Installed bool } -// UpdatePackages handles cache update and upgrade -func UpdatePackages(flags []string) error { - args := append([]string{"pacman", "-Syu"}, flags...) - - cmd := exec.Command("sudo", args...) - cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr - err := cmd.Run() - return err -} - // Search handles repo searches. Creates a RepoSearch struct. func Search(pkgInputN []string) (s Query, n int, err error) { localDb, err := config.AlpmHandle.LocalDb() @@ -62,7 +50,7 @@ func Search(pkgInputN []string) (s Query, n int, err error) { } // TopDown functions - if util.SortMode == util.TopDown { + if config.YayConf.SortMode == config.TopDown { initL = func(len int) int { return 0 } @@ -116,13 +104,13 @@ func Search(pkgInputN []string) (s Query, n int, err error) { func (s Query) PrintSearch() { for i, res := range s { var toprint string - if util.SearchVerbosity == util.NumberMenu { - if util.SortMode == util.BottomUp { + if config.YayConf.SearchMode == config.NumberMenu { + if config.YayConf.SortMode == config.BottomUp { toprint += fmt.Sprintf("%d ", len(s)-i-1) } else { toprint += fmt.Sprintf("%d ", i) } - } else if util.SearchVerbosity == util.Minimal { + } else if config.YayConf.SearchMode == config.Minimal { fmt.Println(res.Name) continue } @@ -241,36 +229,14 @@ func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error return } -// Install sends an install command to pacman with the pkgName slice -func Install(pkgName []string, flags []string) (err error) { - if len(pkgName) == 0 { - return nil - } - - args := []string{"pacman", "-S"} - args = append(args, pkgName...) - args = append(args, flags...) - - cmd := exec.Command("sudo", args...) - cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr - cmd.Run() - return nil -} - // CleanRemove sends a full removal command to pacman with the pkgName slice func CleanRemove(pkgName []string) (err error) { if len(pkgName) == 0 { return nil } - args := []string{"pacman", "-Rnsc"} - args = append(args, pkgName...) - args = append(args, "--noutil.Conf.rm") - - cmd := exec.Command("sudo", args...) - cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr - cmd.Run() - return nil + err = config.PassToPacman("-Rsnc", pkgName, []string{"--noutil.Conf.rm"}) + return err } // ForeignPackages returns a map of foreign packages, with their version and date as values. diff --git a/pacman/pacman_test.go b/pacman/pacman_test.go index 7d4f3a98..9a7ff12b 100644 --- a/pacman/pacman_test.go +++ b/pacman/pacman_test.go @@ -1,8 +1,11 @@ package pacman -import "testing" -import "github.com/jguer/yay/util" -import "os" +import ( + "os" + "testing" + + "github.com/jguer/yay/config" +) func benchmarkPrintSearch(search string, b *testing.B) { old := os.Stdout @@ -17,20 +20,20 @@ func benchmarkPrintSearch(search string, b *testing.B) { } func BenchmarkPrintSearchSimpleTopDown(b *testing.B) { - util.SortMode = util.TopDown + config.YayConf.SortMode = config.TopDown benchmarkPrintSearch("chromium", b) } func BenchmarkPrintSearchComplexTopDown(b *testing.B) { - util.SortMode = util.TopDown + config.YayConf.SortMode = config.TopDown benchmarkPrintSearch("linux", b) } func BenchmarkPrintSearchSimpleBottomUp(b *testing.B) { - util.SortMode = util.BottomUp + config.YayConf.SortMode = config.BottomUp benchmarkPrintSearch("chromium", b) } func BenchmarkPrintSearchComplexBottomUp(b *testing.B) { - util.SortMode = util.BottomUp + config.YayConf.SortMode = config.BottomUp benchmarkPrintSearch("linux", b) } @@ -40,20 +43,20 @@ func benchmarkSearch(search string, b *testing.B) { } } func BenchmarkSearchSimpleTopDown(b *testing.B) { - util.SortMode = util.TopDown + config.YayConf.SortMode = config.TopDown benchmarkSearch("chromium", b) } func BenchmarkSearchSimpleBottomUp(b *testing.B) { - util.SortMode = util.BottomUp + config.YayConf.SortMode = config.BottomUp benchmarkSearch("chromium", b) } func BenchmarkSearchComplexTopDown(b *testing.B) { - util.SortMode = util.TopDown + config.YayConf.SortMode = config.TopDown benchmarkSearch("linux", b) } func BenchmarkSearchComplexBottomUp(b *testing.B) { - util.SortMode = util.BottomUp + config.YayConf.SortMode = config.BottomUp benchmarkSearch("linux", b) } diff --git a/query.go b/query.go index cdb1b252..394380c7 100644 --- a/query.go +++ b/query.go @@ -6,7 +6,6 @@ import ( "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" ) @@ -16,13 +15,13 @@ func printAURSearch(q aur.Query, start int) { for i, res := range q { var toprint string - if util.SearchVerbosity == util.NumberMenu { - if util.SortMode == util.BottomUp { + if config.YayConf.SearchMode == config.NumberMenu { + if config.YayConf.SortMode == config.BottomUp { toprint += fmt.Sprintf("%d ", len(q)+start-i-1) } else { toprint += fmt.Sprintf("%d ", start+i) } - } else if util.SearchVerbosity == util.Minimal { + } else if config.YayConf.SearchMode == config.Minimal { fmt.Println(res.Name) continue } @@ -56,7 +55,7 @@ func syncSearch(pkgS []string) (err error) { return err } - if util.SortMode == util.BottomUp { + if config.YayConf.SortMode == config.BottomUp { printAURSearch(aq, 0) pq.PrintSearch() } else { @@ -84,7 +83,7 @@ func syncInfo(pkgS []string, flags []string) (err error) { } if len(repoS) != 0 { - err = passToPacman("-Si", repoS, flags) + err = config.PassToPacman("-Si", repoS, flags) } return diff --git a/util/util.go b/util/util.go deleted file mode 100644 index 79fc4ebe..00000000 --- a/util/util.go +++ /dev/null @@ -1,26 +0,0 @@ -package util - -// SearchVerbosity determines print method used in PrintSearch -var SearchVerbosity = NumberMenu - -// Verbosity settings for search -const ( - NumberMenu = iota - Detailed - Minimal -) - -// Build controls if packages will be built from ABS. -var Build = false - -// NoConfirm ignores prompts. -var NoConfirm = false - -// SortMode determines top down package or down top package display -var SortMode = BottomUp - -// Describes Sorting method for numberdisplay -const ( - BottomUp = iota - TopDown -) diff --git a/utils.go b/utils.go index d42bab01..29ea4b30 100644 --- a/utils.go +++ b/utils.go @@ -5,8 +5,6 @@ import ( "io" "math" "os" - "os/exec" - "strings" "time" "github.com/jguer/yay/aur" @@ -14,32 +12,6 @@ import ( pac "github.com/jguer/yay/pacman" ) -// PassToPacman outsorces execution to pacman binary without modifications. -func passToPacman(op string, pkgs []string, flags []string) error { - var cmd *exec.Cmd - var args []string - - args = append(args, op) - if len(pkgs) != 0 { - args = append(args, pkgs...) - } - - if len(flags) != 0 { - args = append(args, flags...) - } - - if strings.Contains(op, "-Q") { - cmd = exec.Command("pacman", args...) - } else { - args = append([]string{"pacman"}, args...) - cmd = exec.Command("sudo", args...) - } - - cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr - err := cmd.Run() - return err -} - // Complete provides completion info for shells func complete() (err error) { path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.Shell + ".cache" diff --git a/yay.go b/yay.go index ec96b8c9..414678d0 100644 --- a/yay.go +++ b/yay.go @@ -10,7 +10,6 @@ import ( "github.com/jguer/yay/aur" "github.com/jguer/yay/config" pac "github.com/jguer/yay/pacman" - "github.com/jguer/yay/util" ) func usage() { @@ -38,20 +37,19 @@ func usage() { `) } -var version = "1.116" +var version = "2.116" -func parser() (op string, options []string, packages []string, err error) { +func parser() (op string, options []string, packages []string, changedConfig bool, err error) { if len(os.Args) < 2 { err = fmt.Errorf("no operation specified") return } + changedConfig = false op = "yogurt" for _, arg := range os.Args[1:] { if arg[0] == '-' && arg[1] != '-' { switch arg { - case "-b": - util.Build = true default: op = arg } @@ -60,13 +58,12 @@ func parser() (op string, options []string, packages []string, err error) { if arg[0] == '-' && arg[1] == '-' { switch arg { - case "--build": - util.Build = true case "--bottomup": - util.SortMode = util.BottomUp + config.YayConf.SortMode = config.BottomUp + changedConfig = true case "--topdown": - util.SortMode = util.TopDown - + config.YayConf.SortMode = config.TopDown + changedConfig = true case "--complete": config.YayConf.Shell = "sh" complete() @@ -79,7 +76,7 @@ func parser() (op string, options []string, packages []string, err error) { usage() os.Exit(0) case "--noconfirm": - util.NoConfirm = true + config.YayConf.NoConfirm = true fallthrough default: options = append(options, arg) @@ -93,7 +90,7 @@ func parser() (op string, options []string, packages []string, err error) { } func main() { - op, options, pkgs, err := parser() + op, options, pkgs, changedConfig, err := parser() if err != nil { fmt.Println(err) os.Exit(1) @@ -113,9 +110,9 @@ func main() { err = localStatistics(version) case "-Ss", "-Ssq", "-Sqs": if op == "-Ss" { - util.SearchVerbosity = util.Detailed + config.YayConf.SearchMode = config.Detailed } else { - util.SearchVerbosity = util.Minimal + config.YayConf.SearchMode = config.Minimal } if pkgs != nil { @@ -128,13 +125,16 @@ func main() { case "-Si": err = syncInfo(pkgs, options) case "yogurt": - util.SearchVerbosity = util.NumberMenu + config.YayConf.SearchMode = config.NumberMenu if pkgs != nil { err = numberMenu(pkgs, options) } default: - err = passToPacman(op, pkgs, options) + err = config.PassToPacman(op, pkgs, options) + } + if changedConfig { + config.SaveConfig() } config.AlpmHandle.Release() @@ -162,7 +162,7 @@ func numberMenu(pkgS []string, flags []string) (err error) { return fmt.Errorf("no packages match search") } - if util.SortMode == util.BottomUp { + if config.YayConf.SortMode == config.BottomUp { printAURSearch(aq, numpq) pq.PrintSearch() } else { @@ -192,13 +192,13 @@ func numberMenu(pkgS []string, flags []string) (err error) { if num > numaq+numpq-1 || num < 0 { continue } else if num > numpq-1 { - if util.SortMode == util.BottomUp { + if config.YayConf.SortMode == config.BottomUp { aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name) } else { aurInstall = append(aurInstall, aq[num-numpq].Name) } } else { - if util.SortMode == util.BottomUp { + if config.YayConf.SortMode == config.BottomUp { repoInstall = append(repoInstall, pq[numpq-num-1].Name) } else { repoInstall = append(repoInstall, pq[num].Name) @@ -207,7 +207,7 @@ func numberMenu(pkgS []string, flags []string) (err error) { } if len(repoInstall) != 0 { - pac.Install(repoInstall, flags) + config.PassToPacman("-S", repoInstall, flags) } if len(aurInstall) != 0 {