diff --git a/clean.go b/clean.go index d68000b5..8d1865a3 100644 --- a/clean.go +++ b/clean.go @@ -67,13 +67,13 @@ func syncClean(parser *arguments) error { } } - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { if err = show(passToPacman(parser)); err != nil { return err } } - if !(mode == ModeAUR || mode == ModeAny) { + if !(mode == modeAUR || mode == modeAny) { return nil } diff --git a/cmd.go b/cmd.go index 168a901d..992662ab 100644 --- a/cmd.go +++ b/cmd.go @@ -192,8 +192,7 @@ func handleVersion() { func handlePrint() (err error) { switch { case cmdArgs.existsArg("d", "defaultconfig"): - var tmpConfig Configuration - tmpConfig.defaultSettings() + tmpConfig := defaultSettings() tmpConfig.expandEnv() fmt.Printf("%v", tmpConfig) case cmdArgs.existsArg("g", "currentconfig"): @@ -238,8 +237,8 @@ func handleGetpkgbuild() error { } func handleYogurt() error { - config.SearchMode = NumberMenu - return numberMenu(cmdArgs.targets) + config.SearchMode = numberMenu + return displayNumberMenu(cmdArgs.targets) } func handleSync() error { @@ -247,9 +246,9 @@ func handleSync() error { if cmdArgs.existsArg("s", "search") { if cmdArgs.existsArg("q", "quiet") { - config.SearchMode = Minimal + config.SearchMode = minimal } else { - config.SearchMode = Detailed + config.SearchMode = detailed } return syncSearch(targets) } @@ -286,7 +285,7 @@ func handleRemove() error { } // NumberMenu presents a CLI for selecting packages to install. -func numberMenu(pkgS []string) (err error) { +func displayNumberMenu(pkgS []string) (err error) { var ( aurErr, repoErr error aq aurQuery @@ -296,11 +295,11 @@ func numberMenu(pkgS []string) (err error) { pkgS = removeInvalidTargets(pkgS) - if mode == ModeAUR || mode == ModeAny { + if mode == modeAUR || mode == modeAny { aq, aurErr = narrowSearch(pkgS, true) lenaq = len(aq) } - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq, repoErr = queryRepo(pkgS) lenpq = len(pq) if repoErr != nil { @@ -312,18 +311,18 @@ func numberMenu(pkgS []string) (err error) { return fmt.Errorf("No packages match search") } - if config.SortMode == BottomUp { - if mode == ModeAUR || mode == ModeAny { + if config.SortMode == bottomUp { + if mode == modeAUR || mode == modeAny { aq.printSearch(lenpq + 1) } - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq.printSearch() } } else { - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq.printSearch() } - if mode == ModeAUR || mode == ModeAny { + if mode == modeAUR || mode == modeAny { aq.printSearch(lenpq + 1) } } @@ -353,7 +352,7 @@ func numberMenu(pkgS []string) (err error) { for i, pkg := range pq { target := len(pq) - i - if config.SortMode == TopDown { + if config.SortMode == topDown { target = i + 1 } @@ -364,7 +363,7 @@ func numberMenu(pkgS []string) (err error) { for i, pkg := range aq { target := len(aq) - i + len(pq) - if config.SortMode == TopDown { + if config.SortMode == topDown { target = i + 1 + len(pq) } diff --git a/config.go b/config.go index 5090f9a7..27591810 100644 --- a/config.go +++ b/config.go @@ -15,25 +15,21 @@ import ( // Verbosity settings for search const ( - NumberMenu = iota - Detailed - Minimal -) + numberMenu = iota + detailed + minimal -// Describes Sorting method for numberdisplay -const ( - BottomUp = iota - TopDown + // Describes Sorting method for numberdisplay + bottomUp = iota + topDown + + modeAUR targetMode = iota + modeRepo + modeAny ) type targetMode int -const ( - ModeAUR targetMode = iota - ModeRepo - ModeAny -) - // Configuration stores yay's config. type Configuration struct { AURURL string `json:"aururl"` @@ -108,7 +104,7 @@ var vcsFile string var shouldSaveConfig bool // YayConf holds the current config values for yay. -var config Configuration +var config *Configuration // AlpmConf holds the current config values for pacman. var pacmanConf *pacmanconf.Config @@ -117,7 +113,7 @@ var pacmanConf *pacmanconf.Config var alpmHandle *alpm.Handle // Mode is used to restrict yay to AUR or repo only modes -var mode = ModeAny +var mode = modeAny var hideMenus = false @@ -137,51 +133,54 @@ func (config *Configuration) saveConfig() error { return err } -func (config *Configuration) defaultSettings() { - buildDir := "$HOME/.cache/yay" - if os.Getenv("XDG_CACHE_HOME") != "" { - buildDir = "$XDG_CACHE_HOME/yay" +func defaultSettings() *Configuration { + config := &Configuration{ + AURURL: "https://aur.archlinux.org", + BuildDir: "$HOME/.cache/yay", + CleanAfter: false, + Editor: "", + EditorFlags: "", + Devel: false, + MakepkgBin: "makepkg", + MakepkgConf: "", + NoConfirm: false, + PacmanBin: "pacman", + PGPFetch: true, + PacmanConf: "/etc/pacman.conf", + GpgFlags: "", + MFlags: "", + GitFlags: "", + SortMode: bottomUp, + CompletionInterval: 7, + SortBy: "votes", + SudoLoop: false, + TarBin: "bsdtar", + GitBin: "git", + GpgBin: "gpg", + TimeUpdate: false, + RequestSplitN: 150, + ReDownload: "no", + ReBuild: "no", + AnswerClean: "", + AnswerDiff: "", + AnswerEdit: "", + AnswerUpgrade: "", + RemoveMake: "ask", + GitClone: true, + Provides: true, + UpgradeMenu: true, + CleanMenu: true, + DiffMenu: true, + EditMenu: false, + UseAsk: false, + CombinedUpgrade: false, } - config.AURURL = "https://aur.archlinux.org" - config.BuildDir = buildDir - config.CleanAfter = false - config.Editor = "" - config.EditorFlags = "" - config.Devel = false - config.MakepkgBin = "makepkg" - config.MakepkgConf = "" - config.NoConfirm = false - config.PacmanBin = "pacman" - config.PGPFetch = true - config.PacmanConf = "/etc/pacman.conf" - config.GpgFlags = "" - config.MFlags = "" - config.GitFlags = "" - config.SortMode = BottomUp - config.CompletionInterval = 7 - config.SortBy = "votes" - config.SudoLoop = false - config.TarBin = "bsdtar" - config.GitBin = "git" - config.GpgBin = "gpg" - config.TimeUpdate = false - config.RequestSplitN = 150 - config.ReDownload = "no" - config.ReBuild = "no" - config.AnswerClean = "" - config.AnswerDiff = "" - config.AnswerEdit = "" - config.AnswerUpgrade = "" - config.RemoveMake = "ask" - config.GitClone = true - config.Provides = true - config.UpgradeMenu = true - config.CleanMenu = true - config.DiffMenu = true - config.EditMenu = false - config.UseAsk = false - config.CombinedUpgrade = false + if os.Getenv("XDG_CACHE_HOME") != "" { + config.BuildDir = "$XDG_CACHE_HOME/yay" + } + + return config } func (config *Configuration) expandEnv() { @@ -329,7 +328,7 @@ func toUsage(usages []string) alpm.Usage { return alpm.UsageAll } - var ret alpm.Usage = 0 + var ret alpm.Usage for _, usage := range usages { switch usage { case "Sync": diff --git a/depSolver.go b/depSolver.go index 044cb9b9..ecb9fd48 100644 --- a/depSolver.go +++ b/depSolver.go @@ -89,7 +89,7 @@ func (ds *depSolver) resolveTargets(pkgs []string) error { var singleDb *alpm.Db // aur/ prefix means we only check the aur - if target.Db == "aur" || mode == ModeAUR { + if target.Db == "aur" || mode == modeAUR { ds.Targets = append(ds.Targets, target) aurTargets = append(aurTargets, target.DepString()) continue @@ -139,7 +139,7 @@ func (ds *depSolver) resolveTargets(pkgs []string) error { ds.Targets = append(ds.Targets, target) } - if len(aurTargets) > 0 && (mode == ModeAny || mode == ModeAUR) { + if len(aurTargets) > 0 && (mode == modeAny || mode == modeAUR) { return ds.resolveAURPackages(aurTargets, true) } diff --git a/install.go b/install.go index 083a0cdc..67a46287 100644 --- a/install.go +++ b/install.go @@ -26,7 +26,7 @@ func install(parser *arguments) error { warnings := &aurWarnings{} removeMake := false - if mode == ModeAny || mode == ModeRepo { + if mode == modeAny || mode == modeRepo { if config.CombinedUpgrade { if parser.existsArg("y", "refresh") { err = earlyRefresh(parser) @@ -66,7 +66,7 @@ func install(parser *arguments) error { arguments.op = "S" arguments.clearTargets() - if mode == ModeAUR { + if mode == modeAUR { arguments.delArg("u", "sysupgrade") } @@ -150,7 +150,7 @@ func install(parser *arguments) error { arguments.addTarget(pkg) } - if len(ds.Aur) == 0 && len(arguments.targets) == 0 && (!parser.existsArg("u", "sysupgrade") || mode == ModeAUR) { + if len(ds.Aur) == 0 && len(arguments.targets) == 0 && (!parser.existsArg("u", "sysupgrade") || mode == modeAUR) { fmt.Println(" there is nothing to do") return nil } @@ -378,7 +378,7 @@ func earlyPacmanCall(parser *arguments) error { return err } - if mode == ModeRepo { + if mode == modeRepo { arguments.targets = targets } else { //separate aur and repo targets diff --git a/main.go b/main.go index a47c989c..90ad17c2 100644 --- a/main.go +++ b/main.go @@ -208,7 +208,7 @@ func main() { } exitOnError(setPaths()) - config.defaultSettings() + config = defaultSettings() exitOnError(initHomeDirs()) exitOnError(initConfig()) exitOnError(cmdArgs.parseCommandLine()) diff --git a/parser.go b/parser.go index 5e3ceebe..180c77e5 100644 --- a/parser.go +++ b/parser.go @@ -161,7 +161,7 @@ func (parser *arguments) needRoot() bool { if parser.existsArg("i", "info") { return false } - if parser.existsArg("c", "clean") && mode == ModeAUR { + if parser.existsArg("c", "clean") && mode == modeAUR { return false } return true @@ -514,9 +514,9 @@ func handleConfig(option, value string) bool { case "notimeupdate": config.TimeUpdate = false case "topdown": - config.SortMode = TopDown + config.SortMode = topDown case "bottomup": - config.SortMode = BottomUp + config.SortMode = bottomUp case "completioninterval": n, err := strconv.Atoi(value) if err == nil { @@ -630,9 +630,9 @@ func handleConfig(option, value string) bool { case "nocombinedupgrade": config.CombinedUpgrade = false case "a", "aur": - mode = ModeAUR + mode = modeAUR case "repo": - mode = ModeRepo + mode = modeRepo case "removemake": config.RemoveMake = "yes" case "noremovemake": diff --git a/print.go b/print.go index 9df7d609..76a5dd8f 100644 --- a/print.go +++ b/print.go @@ -64,13 +64,13 @@ func (q aurQuery) printSearch(start int) { for i, res := range q { var toprint string - if config.SearchMode == NumberMenu { - if config.SortMode == BottomUp { + if config.SearchMode == numberMenu { + if config.SortMode == bottomUp { toprint += magenta(strconv.Itoa(len(q)+start-i-1) + " ") } else { toprint += magenta(strconv.Itoa(start+i) + " ") } - } else if config.SearchMode == Minimal { + } else if config.SearchMode == minimal { fmt.Println(res.Name) continue } @@ -104,13 +104,13 @@ func (q aurQuery) printSearch(start int) { func (s repoQuery) printSearch() { for i, res := range s { var toprint string - if config.SearchMode == NumberMenu { - if config.SortMode == BottomUp { + if config.SearchMode == numberMenu { + if config.SortMode == bottomUp { toprint += magenta(strconv.Itoa(len(s)-i) + " ") } else { toprint += magenta(strconv.Itoa(i+1) + " ") } - } else if config.SearchMode == Minimal { + } else if config.SearchMode == minimal { fmt.Println(res.Name()) continue } @@ -452,7 +452,7 @@ func printNewsFeed() error { return err } - if config.SortMode == BottomUp { + if config.SortMode == bottomUp { for i := len(rss.Channel.Items) - 1; i >= 0; i-- { rss.Channel.Items[i].print(buildTime) } diff --git a/query.go b/query.go index 0ee90b2f..ad58c0a5 100644 --- a/query.go +++ b/query.go @@ -49,7 +49,7 @@ func (q aurQuery) Less(i, j int) bool { result = q[i].PackageBaseID < q[j].PackageBaseID } - if config.SortMode == BottomUp { + if config.SortMode == bottomUp { return !result } @@ -165,28 +165,28 @@ func syncSearch(pkgS []string) (err error) { var aq aurQuery var pq repoQuery - if mode == ModeAUR || mode == ModeAny { + if mode == modeAUR || mode == modeAny { aq, aurErr = narrowSearch(pkgS, true) } - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq, repoErr = queryRepo(pkgS) if repoErr != nil { return err } } - if config.SortMode == BottomUp { - if mode == ModeAUR || mode == ModeAny { + if config.SortMode == bottomUp { + if mode == modeAUR || mode == modeAny { aq.printSearch(1) } - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq.printSearch() } } else { - if mode == ModeRepo || mode == ModeAny { + if mode == modeRepo || mode == modeAny { pq.printSearch() } - if mode == ModeAUR || mode == ModeAny { + if mode == modeAUR || mode == modeAny { aq.printSearch(1) } } @@ -271,7 +271,7 @@ func queryRepo(pkgInputN []string) (s repoQuery, err error) { return nil }) - if config.SortMode == BottomUp { + if config.SortMode == bottomUp { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { s[i], s[j] = s[j], s[i] } @@ -291,10 +291,10 @@ func packageSlices(toCheck []string) (aur []string, repo []string, err error) { db, name := splitDbFromName(_pkg) found := false - if db == "aur" || mode == ModeAUR { + if db == "aur" || mode == modeAUR { aur = append(aur, _pkg) continue - } else if db != "" || mode == ModeRepo { + } else if db != "" || mode == modeRepo { repo = append(repo, _pkg) continue } diff --git a/upgrade.go b/upgrade.go index 3b739369..de918103 100644 --- a/upgrade.go +++ b/upgrade.go @@ -123,7 +123,7 @@ func upList(warnings *aurWarnings) (upSlice, upSlice, error) { aurdata := make(map[string]*rpc.Pkg) - if mode == ModeAny || mode == ModeRepo { + if mode == modeAny || mode == modeRepo { fmt.Println(bold(cyan("::") + bold(" Searching databases for updates..."))) wg.Add(1) go func() { @@ -133,7 +133,7 @@ func upList(warnings *aurWarnings) (upSlice, upSlice, error) { }() } - if mode == ModeAny || mode == ModeAUR { + if mode == modeAny || mode == modeAUR { fmt.Println(bold(cyan("::") + bold(" Searching AUR for updates..."))) var _aurdata []*rpc.Pkg diff --git a/utils.go b/utils.go index 28a48c09..3a3b741c 100644 --- a/utils.go +++ b/utils.go @@ -114,12 +114,12 @@ func removeInvalidTargets(targets []string) []string { for _, target := range targets { db, _ := splitDbFromName(target) - if db == "aur" && mode == ModeRepo { + if db == "aur" && mode == modeRepo { fmt.Printf("%s %s %s\n", bold(yellow(arrow)), cyan(target), bold("Can't use target with option --repo -- skipping")) continue } - if db != "aur" && db != "" && mode == ModeAUR { + if db != "aur" && db != "" && mode == modeAUR { fmt.Printf("%s %s %s\n", bold(yellow(arrow)), cyan(target), bold("Can't use target with option --aur -- skipping")) continue }