Skip to content

Commit

Permalink
style(runner): use command runner for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Aug 18, 2020
1 parent ab3c66a commit c1171d4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 83 deletions.
10 changes: 5 additions & 5 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func cleanRemove(cmdArgs *settings.Arguments, pkgNames []string) error {
_ = arguments.AddArg("R")
arguments.AddTarget(pkgNames...)

return show(passToPacman(arguments))
return config.Runtime.CmdRunner.Show(passToPacman(arguments))
}

func syncClean(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
Expand All @@ -75,7 +75,7 @@ func syncClean(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
}

if config.Runtime.Mode == settings.ModeRepo || config.Runtime.Mode == settings.ModeAny {
if err := show(passToPacman(cmdArgs)); err != nil {
if err := config.Runtime.CmdRunner.Show(passToPacman(cmdArgs)); err != nil {
return err
}
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func cleanUntracked() error {

dir := filepath.Join(config.BuildDir, file.Name())
if isGitRepository(dir) {
if err := show(passToGit(dir, "clean", "-fx")); err != nil {
if err := config.Runtime.CmdRunner.Show(passToGit(dir, "clean", "-fx")); err != nil {
text.Warnln(gotext.Get("Unable to clean:"), dir)
return err
}
Expand All @@ -219,12 +219,12 @@ func cleanAfter(bases []dep.Base) {

text.OperationInfoln(gotext.Get("Cleaning (%d/%d): %s", i+1, len(bases), text.Cyan(dir)))

_, stderr, err := capture(passToGit(dir, "reset", "--hard", "HEAD"))
_, stderr, err := config.Runtime.CmdRunner.Capture(passToGit(dir, "reset", "--hard", "HEAD"), 0)
if err != nil {
text.Errorln(gotext.Get("error resetting %s: %s", base.String(), stderr))
}

if err := show(passToGit(dir, "clean", "-fx", "--exclude='*.pkg.*'")); err != nil {
if err := config.Runtime.CmdRunner.Show(passToGit(dir, "clean", "-fx", "--exclude='*.pkg.*'")); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
Expand Down
22 changes: 11 additions & 11 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ func handleCmd(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
handleVersion()
return nil
case "D", "database":
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
case "F", "files":
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
case "Q", "query":
return handleQuery(cmdArgs, dbExecutor)
case "R", "remove":
return handleRemove(cmdArgs, savedInfo)
case "S", "sync":
return handleSync(cmdArgs, dbExecutor)
case "T", "deptest":
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
case "U", "upgrade":
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
case "G", "getpkgbuild":
return handleGetpkgbuild(cmdArgs, dbExecutor)
case "P", "show":
Expand All @@ -181,15 +181,15 @@ func handleQuery(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
if cmdArgs.ExistsArg("u", "upgrades") {
return printUpdateList(cmdArgs, dbExecutor, cmdArgs.ExistsDouble("u", "sysupgrade"))
}
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}

func handleHelp(cmdArgs *settings.Arguments) error {
if cmdArgs.Op == "Y" || cmdArgs.Op == "yay" {
usage()
return nil
}
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}

func handleVersion() {
Expand Down Expand Up @@ -259,7 +259,7 @@ func handleSync(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
return syncSearch(targets, dbExecutor)
}
if cmdArgs.ExistsArg("p", "print", "print-format") {
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}
if cmdArgs.ExistsArg("c", "clean") {
return syncClean(cmdArgs, dbExecutor)
Expand All @@ -268,7 +268,7 @@ func handleSync(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
return syncList(cmdArgs, dbExecutor)
}
if cmdArgs.ExistsArg("g", "groups") {
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}
if cmdArgs.ExistsArg("i", "info") {
return syncInfo(cmdArgs, targets, dbExecutor)
Expand All @@ -280,13 +280,13 @@ func handleSync(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
return install(cmdArgs, dbExecutor, false)
}
if cmdArgs.ExistsArg("y", "refresh") {
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}
return nil
}

func handleRemove(cmdArgs *settings.Arguments, localCache vcsInfo) error {
err := show(passToPacman(cmdArgs))
err := config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
if err == nil {
removeVCSPackage(cmdArgs.Targets, localCache)
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func syncList(cmdArgs *settings.Arguments, dbExecutor db.Executor) error {
}

if (config.Runtime.Mode == settings.ModeAny || config.Runtime.Mode == settings.ModeRepo) && (len(cmdArgs.Targets) != 0 || !aur) {
return show(passToPacman(cmdArgs))
return config.Runtime.CmdRunner.Show(passToPacman(cmdArgs))
}

return nil
Expand Down
31 changes: 16 additions & 15 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const gitDiffRefName = "AUR_SEEN"
// Update the YAY_DIFF_REVIEW ref to HEAD. We use this ref to determine which diff were
// reviewed by the user
func gitUpdateSeenRef(path, name string) error {
_, stderr, err := capture(passToGit(filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"))
_, stderr, err := config.Runtime.CmdRunner.Capture(passToGit(filepath.Join(path, name), "update-ref", gitDiffRefName, "HEAD"), 0)
if err != nil {
return fmt.Errorf("%s %s", stderr, err)
}
Expand All @@ -33,15 +33,15 @@ func gitUpdateSeenRef(path, name string) error {
// Return wether or not we have reviewed a diff yet. It checks for the existence of
// YAY_DIFF_REVIEW in the git ref-list
func gitHasLastSeenRef(path, name string) bool {
_, _, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName))
_, _, err := config.Runtime.CmdRunner.Capture(passToGit(filepath.Join(path, name), "rev-parse", "--quiet", "--verify", gitDiffRefName), 0)
return err == nil
}

// Returns the last reviewed hash. If YAY_DIFF_REVIEW exists it will return this hash.
// If it does not it will return empty tree as no diff have been reviewed yet.
func getLastSeenHash(path, name string) (string, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", gitDiffRefName))
stdout, stderr, err := config.Runtime.CmdRunner.Capture(passToGit(filepath.Join(path, name), "rev-parse", gitDiffRefName), 0)
if err != nil {
return "", fmt.Errorf("%s %s", stderr, err)
}
Expand All @@ -56,7 +56,8 @@ func getLastSeenHash(path, name string) (string, error) {
// HEAD@{upstream}
func gitHasDiff(path, name string) (bool, error) {
if gitHasLastSeenRef(path, name) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"))
stdout, stderr, err := config.Runtime.CmdRunner.Capture(
passToGit(filepath.Join(path, name), "rev-parse", gitDiffRefName, "HEAD@{upstream}"), 0)
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}
Expand All @@ -80,8 +81,7 @@ func gitDownloadABS(url, path, name string) (bool, error) {
if _, errExist := os.Stat(filepath.Join(path, name)); os.IsNotExist(errExist) {
cmd := passToGit(path, "clone", "--no-progress", "--single-branch",
"-b", "packages/"+name, url, name)
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, err := capture(cmd)
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
}
Expand All @@ -92,8 +92,7 @@ func gitDownloadABS(url, path, name string) (bool, error) {
}

cmd := passToGit(filepath.Join(path, name), "pull", "--ff-only")
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, err := capture(cmd)
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
}
Expand All @@ -105,8 +104,7 @@ func gitDownload(url, path, name string) (bool, error) {
_, err := os.Stat(filepath.Join(path, name, ".git"))
if os.IsNotExist(err) {
cmd := passToGit(path, "clone", "--no-progress", url, name)
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, errCapture := capture(cmd)
_, stderr, errCapture := config.Runtime.CmdRunner.Capture(cmd, 0)
if errCapture != nil {
return false, fmt.Errorf(gotext.Get("error cloning %s: %s", name, stderr))
}
Expand All @@ -117,8 +115,7 @@ func gitDownload(url, path, name string) (bool, error) {
}

cmd := passToGit(filepath.Join(path, name), "fetch")
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, err := capture(cmd)
_, stderr, err := config.Runtime.CmdRunner.Capture(cmd, 0)
if err != nil {
return false, fmt.Errorf(gotext.Get("error fetching %s: %s", name, stderr))
}
Expand All @@ -127,12 +124,12 @@ func gitDownload(url, path, name string) (bool, error) {
}

func gitMerge(path, name string) error {
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
_, stderr, err := config.Runtime.CmdRunner.Capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"), 0)
if err != nil {
return fmt.Errorf(gotext.Get("error resetting %s: %s", name, stderr))
}

_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
_, stderr, err = config.Runtime.CmdRunner.Capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"), 0)
if err != nil {
return fmt.Errorf(gotext.Get("error merging %s: %s", name, stderr))
}
Expand Down Expand Up @@ -281,7 +278,11 @@ func getPkgbuildsfromABS(pkgs []string, path string, dbExecutor db.Executor, for
return
}

_, stderr, err := capture(exec.Command("cp", "-r", filepath.Join(config.ABSDir, pkg, "trunk"), filepath.Join(path, pkg)))
_, stderr, err := config.Runtime.CmdRunner.Capture(
exec.Command(
"cp", "-r",
filepath.Join(config.ABSDir, pkg, "trunk"),
filepath.Join(path, pkg)), 0)
mux.Lock()
downloaded++
if err != nil {
Expand Down
38 changes: 4 additions & 34 deletions exec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"fmt"
"os"
"os/exec"
Expand All @@ -10,33 +9,11 @@ import (
"time"

"github.com/leonelquinteros/gotext"
"golang.org/x/crypto/ssh/terminal"

"github.com/Jguer/yay/v10/pkg/settings"
"github.com/Jguer/yay/v10/pkg/text"
)

func show(cmd *exec.Cmd) error {
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("")
}
return nil
}

func capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
var outbuf, errbuf bytes.Buffer

cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err = cmd.Run()
stdout = strings.TrimSpace(outbuf.String())
stderr = strings.TrimSpace(errbuf.String())

return stdout, stderr, err
}

func sudoLoopBackground() {
updateSudo()
go sudoLoop()
Expand All @@ -53,7 +30,7 @@ func updateSudo() {
for {
mSudoFlags := strings.Fields(config.SudoFlags)
mSudoFlags = append([]string{"-v"}, mSudoFlags...)
err := show(exec.Command(config.SudoBin, mSudoFlags...))
err := config.Runtime.CmdRunner.Show(exec.Command(config.SudoBin, mSudoFlags...))
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
Expand Down Expand Up @@ -82,13 +59,11 @@ func waitLock(dbPath string) {
}

func passToPacman(args *settings.Arguments) *exec.Cmd {
argArr := make([]string, 0)

mSudoFlags := strings.Fields(config.SudoFlags)
argArr := make([]string, 0, 32)

if args.NeedRoot(config.Runtime) {
argArr = append(argArr, config.SudoBin)
argArr = append(argArr, mSudoFlags...)
argArr = append(argArr, strings.Fields(config.SudoFlags)...)
}

argArr = append(argArr, config.PacmanBin)
Expand All @@ -108,8 +83,7 @@ func passToPacman(args *settings.Arguments) *exec.Cmd {
}

func passToMakepkg(dir string, args ...string) *exec.Cmd {
mflags := strings.Fields(config.MFlags)
args = append(args, mflags...)
args = append(args, strings.Fields(config.MFlags)...)

if config.MakepkgConf != "" {
args = append(args, "--config", config.MakepkgConf)
Expand All @@ -131,7 +105,3 @@ func passToGit(dir string, _args ...string) *exec.Cmd {
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
return cmd
}

func isTty() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
Loading

0 comments on commit c1171d4

Please sign in to comment.