Skip to content

Commit

Permalink
Refactor pssToFoo() functions
Browse files Browse the repository at this point in the history
Previously each call to an external command had two functions.
PassToFoo() and PassToFooCapture(). These functions are always similar
and end up with duplicated code.

So instead have the passToFoo() functions return the cmd itself and
create small helper functions show() and capture() which will run the
command and either forward it to std{out,err,in} or capture the output

Also the saveVCSInfo() function which was called after every makepkg
call is now only called after the pacman -U succeeds.
  • Loading branch information
Morganamilo committed Jul 19, 2018
1 parent 6e41ef8 commit 473a2de
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 133 deletions.
6 changes: 3 additions & 3 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func cleanRemove(pkgNames []string) (err error) {
arguments := makeArguments()
arguments.addArg("R")
arguments.addTarget(pkgNames...)
err = passToPacman(arguments)
err = show(passToPacman(arguments))
return err
}

Expand All @@ -67,7 +67,7 @@ func syncClean(parser *arguments) error {
}
}

err := passToPacman(parser)
err := show(passToPacman(parser))
if err != nil {
return err
}
Expand Down Expand Up @@ -185,7 +185,7 @@ func cleanUntracked() error {
dir := filepath.Join(config.BuildDir, file.Name())

if shouldUseGit(dir) {
err = passToGit(dir, "clean", "-fx")
err = show(passToGit(dir, "clean", "-fx"))
if err != nil {
return err
}
Expand Down
130 changes: 29 additions & 101 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,19 @@ func handleCmd() (err error) {
case "V", "version":
handleVersion()
case "D", "database":
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
case "F", "files":
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
case "Q", "query":
err = handleQuery()
case "R", "remove":
err = handleRemove()
case "S", "sync":
err = handleSync()
case "T", "deptest":
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
case "U", "upgrade":
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
case "G", "getpkgbuild":
err = handleGetpkgbuild()
case "P", "print":
Expand All @@ -209,7 +209,7 @@ func handleQuery() error {
if cmdArgs.existsArg("u", "upgrades") {
err = printUpdateList(cmdArgs)
} else {
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
}

return err
Expand All @@ -221,7 +221,7 @@ func handleHelp() error {
return nil
}

return passToPacman(cmdArgs)
return show(passToPacman(cmdArgs))
}

//this function should only set config options
Expand Down Expand Up @@ -436,7 +436,7 @@ func handleSync() (err error) {
arguments.delArg("i", "info")
arguments.delArg("l", "list")
arguments.clearTargets()
err = passToPacman(arguments)
err = show(passToPacman(arguments))
if err != nil {
return
}
Expand All @@ -453,9 +453,9 @@ func handleSync() (err error) {
} else if cmdArgs.existsArg("c", "clean") {
err = syncClean(cmdArgs)
} else if cmdArgs.existsArg("l", "list") {
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
} else if cmdArgs.existsArg("c", "clean") {
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
} else if cmdArgs.existsArg("i", "info") {
err = syncInfo(targets)
} else if cmdArgs.existsArg("u", "sysupgrade") {
Expand All @@ -469,7 +469,7 @@ func handleSync() (err error) {

func handleRemove() (err error) {
removeVCSPackage(cmdArgs.targets)
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
return
}

Expand Down Expand Up @@ -579,41 +579,29 @@ func numberMenu(pkgS []string, flags []string) (err error) {
return err
}

// passToPacman outsources execution to pacman binary without modifications.
func passToPacman(args *arguments) error {
var cmd *exec.Cmd
argArr := make([]string, 0)

if args.needRoot() {
argArr = append(argArr, "sudo")
}

argArr = append(argArr, config.PacmanBin)
argArr = append(argArr, cmdArgs.formatGlobals()...)
argArr = append(argArr, args.formatArgs()...)
if config.NoConfirm {
argArr = append(argArr, "--noconfirm")
}

argArr = append(argArr, "--")

argArr = append(argArr, args.targets...)

cmd = exec.Command(argArr[0], argArr[1:]...)

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
}

//passToPacman but return the output instead of showing the user
func passToPacmanCapture(args *arguments) (string, string, error) {
func capture(cmd *exec.Cmd) (string, string, error) {
var outbuf, errbuf bytes.Buffer
var cmd *exec.Cmd

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

return stdout, stderr, err
}

// passToPacman outsources execution to pacman binary without modifications.
func passToPacman(args *arguments) *exec.Cmd {
argArr := make([]string, 0)

if args.needRoot() {
Expand All @@ -631,20 +619,11 @@ func passToPacmanCapture(args *arguments) (string, string, error) {

argArr = append(argArr, args.targets...)

cmd = exec.Command(argArr[0], argArr[1:]...)
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

err := cmd.Run()
stdout := outbuf.String()
stderr := errbuf.String()

return stdout, stderr, err
return exec.Command(argArr[0], argArr[1:]...)
}

// passToMakepkg outsources execution to makepkg binary without modifications.
func passToMakepkg(dir string, args ...string) (err error) {

func passToMakepkg(dir string, args ...string) *exec.Cmd {
if config.NoConfirm {
args = append(args)
}
Expand All @@ -653,68 +632,17 @@ func passToMakepkg(dir string, args ...string) (err error) {
args = append(args, mflags...)

cmd := exec.Command(config.MakepkgBin, args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
cmd.Dir = dir
err = cmd.Run()
if err == nil {
_ = saveVCSInfo()
}
return
return cmd
}

func passToMakepkgCapture(dir string, args ...string) (string, string, error) {
var outbuf, errbuf bytes.Buffer

if config.NoConfirm {
args = append(args)
}

mflags := strings.Fields(config.MFlags)
args = append(args, mflags...)

cmd := exec.Command(config.MakepkgBin, args...)
cmd.Dir = dir
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

err := cmd.Run()
stdout := outbuf.String()
stderr := errbuf.String()

if err == nil {
_ = saveVCSInfo()
}

return stdout, stderr, err
}

func passToGit(dir string, _args ...string) (err error) {
func passToGit(dir string, _args ...string) *exec.Cmd {
gitflags := strings.Fields(config.GitFlags)
args := []string{"-C", dir}
args = append(args, gitflags...)
args = append(args, _args...)

cmd := exec.Command(config.GitBin, args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err = cmd.Run()
return
}

func passToGitCapture(dir string, _args ...string) (string, string, error) {
var outbuf, errbuf bytes.Buffer
gitflags := strings.Fields(config.GitFlags)
args := []string{"-C", dir}
args = append(args, gitflags...)
args = append(args, _args...)

cmd := exec.Command(config.GitBin, args...)
cmd.Dir = dir
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf

err := cmd.Run()
stdout := outbuf.String()
stderr := errbuf.String()

return stdout, stderr, err
return cmd
}
23 changes: 9 additions & 14 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,22 @@ func downloadFile(path string, url string) (err error) {
}

func gitHasDiff(path string, name string) (bool, error) {
stdout, stderr, err := passToGitCapture(filepath.Join(path, name), "rev-parse", "HEAD")
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}

head := strings.TrimSpace(stdout)

stdout, stderr, err = passToGitCapture(filepath.Join(path, name), "rev-parse", "HEAD@{upstream}")
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}

upstream := strings.TrimSpace(stdout)
lines := strings.Split(stdout, "\n")
head := lines[0]
upstream := lines[1]

return head != upstream, nil
}

func gitDownload(url string, path string, name string) (bool, error) {
_, err := os.Stat(filepath.Join(path, name, ".git"))
if os.IsNotExist(err) {
err = passToGit(path, "clone", url, name)
err = show(passToGit(path, "clone", url, name))
if err != nil {
return false, fmt.Errorf("error cloning %s", name)
}
Expand All @@ -75,7 +70,7 @@ func gitDownload(url string, path string, name string) (bool, error) {
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
}

err = passToGit(filepath.Join(path, name), "fetch")
err = show(passToGit(filepath.Join(path, name), "fetch"))
if err != nil {
return false, fmt.Errorf("error fetching %s", name)
}
Expand All @@ -84,12 +79,12 @@ func gitDownload(url string, path string, name string) (bool, error) {
}

func gitMerge(url string, path string, name string) error {
err := passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD")
err := show(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
if err != nil {
return fmt.Errorf("error resetting %s", name)
}

err = passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff")
err = show(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
if err != nil {
return fmt.Errorf("error merging %s", name)
}
Expand All @@ -98,7 +93,7 @@ func gitMerge(url string, path string, name string) error {
}

func gitDiff(path string, name string) error {
err := passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}")
err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))

return err
}
Expand Down
Loading

0 comments on commit 473a2de

Please sign in to comment.