Merge pull request #567 from Morganamilo/refactor-install

Refactor install
This commit is contained in:
Anna 2018-07-19 21:32:29 +01:00 committed by GitHub
commit 298afac0e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 234 additions and 312 deletions

View File

@ -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
}
@ -67,7 +67,7 @@ func syncClean(parser *arguments) error {
}
}
err := passToPacman(parser)
err := show(passToPacman(parser))
if err != nil {
return err
}
@ -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
}

189
cmd.go
View File

@ -2,13 +2,9 @@ package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var cmdArgs = makeArguments()
@ -120,31 +116,6 @@ If no arguments are provided 'yay -Syu' will be performed
If no operation is provided -Y will be assumed`)
}
func sudoLoopBackground() {
updateSudo()
go sudoLoop()
}
func sudoLoop() {
for {
updateSudo()
time.Sleep(298 * time.Second)
}
}
func updateSudo() {
for {
cmd := exec.Command("sudo", "-v")
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
func handleCmd() (err error) {
for option, value := range cmdArgs.options {
if handleConfig(option, value) {
@ -175,9 +146,9 @@ 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":
@ -185,9 +156,9 @@ func handleCmd() (err error) {
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":
@ -209,7 +180,7 @@ func handleQuery() error {
if cmdArgs.existsArg("u", "upgrades") {
err = printUpdateList(cmdArgs)
} else {
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
}
return err
@ -221,7 +192,7 @@ func handleHelp() error {
return nil
}
return passToPacman(cmdArgs)
return show(passToPacman(cmdArgs))
}
//this function should only set config options
@ -436,7 +407,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
}
@ -453,9 +424,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") {
@ -469,7 +440,7 @@ func handleSync() (err error) {
func handleRemove() (err error) {
removeVCSPackage(cmdArgs.targets)
err = passToPacman(cmdArgs)
err = show(passToPacman(cmdArgs))
return
}
@ -578,143 +549,3 @@ 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:]...)
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) {
var outbuf, errbuf bytes.Buffer
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:]...)
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Run()
stdout := outbuf.String()
stderr := errbuf.String()
return stdout, stderr, err
}
// passToMakepkg outsources execution to makepkg binary without modifications.
func passToMakepkg(dir string, args ...string) (err error) {
if config.NoConfirm {
args = append(args)
}
mflags := strings.Fields(config.MFlags)
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
}
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) {
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
}

View File

@ -45,19 +45,14 @@ 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
}
@ -65,7 +60,7 @@ func gitHasDiff(path string, name string) (bool, error) {
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)
}
@ -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)
}
@ -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)
}
@ -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
}

99
exec.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
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) (string, string, error) {
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Run()
stdout := outbuf.String()
stderr := errbuf.String()
return stdout, stderr, err
}
func sudoLoopBackground() {
updateSudo()
go sudoLoop()
}
func sudoLoop() {
for {
updateSudo()
time.Sleep(298 * time.Second)
}
}
func updateSudo() {
for {
err := show(exec.Command("sudo", "-v"))
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
func passToPacman(args *arguments) *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...)
return exec.Command(argArr[0], argArr[1:]...)
}
func passToMakepkg(dir string, args ...string) *exec.Cmd {
if config.NoConfirm {
args = append(args)
}
mflags := strings.Fields(config.MFlags)
args = append(args, mflags...)
cmd := exec.Command(config.MakepkgBin, args...)
cmd.Dir = dir
return cmd
}
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
return cmd
}

View File

@ -27,107 +27,37 @@ func install(parser *arguments) error {
removeMake := false
srcinfosStale := make(map[string]*gosrc.Srcinfo)
//remotenames: names of all non repo packages on the system
if mode == ModeAny || mode == ModeRepo {
if config.CombinedUpgrade {
if parser.existsArg("y", "refresh") {
err = earlyRefresh(parser)
if err != nil {
return fmt.Errorf("Error refreshing databases")
}
}
} else if parser.existsArg("y", "refresh") || parser.existsArg("u", "sysupgrade") || len(parser.targets) > 0 {
err = earlyPacmanCall(parser)
if err != nil {
return err
}
}
}
//we may have done -Sy, our handle now has an old
//database.
err = initAlpmHandle()
if err != nil {
return err
}
_, _, localNames, remoteNames, err := filterPackages()
if err != nil {
return err
}
//cache as a stringset. maybe make it return a string set in the first
//place
remoteNamesCache := sliceToStringSet(remoteNames)
localNamesCache := sliceToStringSet(localNames)
if mode == ModeAny || mode == ModeRepo {
if config.CombinedUpgrade {
if parser.existsArg("y", "refresh") {
arguments := parser.copy()
parser.delArg("y", "refresh")
arguments.delArg("u", "sysupgrade")
arguments.delArg("s", "search")
arguments.delArg("i", "info")
arguments.delArg("l", "list")
arguments.clearTargets()
err = passToPacman(arguments)
if err != nil {
return fmt.Errorf("Error installing repo packages")
}
}
} else if parser.existsArg("y", "refresh") || parser.existsArg("u", "sysupgrade") || len(parser.targets) > 0 {
arguments := parser.copy()
arguments.op = "S"
targets := parser.targets
parser.clearTargets()
arguments.clearTargets()
syncDb, err := alpmHandle.SyncDbs()
if err != nil {
return err
}
if mode == ModeRepo {
arguments.targets = targets
} else {
alpmHandle.SetQuestionCallback(func(alpm.QuestionAny) {})
//seperate aur and repo targets
for _, _target := range targets {
target := toTarget(_target)
if target.Db == "aur" {
parser.addTarget(_target)
continue
}
var singleDb *alpm.Db
if target.Db != "" {
singleDb, err = alpmHandle.SyncDbByName(target.Db)
if err != nil {
return err
}
_, err = singleDb.PkgCache().FindSatisfier(target.DepString())
} else {
_, err = syncDb.FindSatisfier(target.DepString())
}
if err == nil {
arguments.addTarget(_target)
} else {
_, err := syncDb.PkgCachebyGroup(target.Name)
if err == nil {
arguments.addTarget(_target)
continue
}
parser.addTarget(_target)
}
}
}
if parser.existsArg("y", "refresh") || parser.existsArg("u", "sysupgrade") || len(arguments.targets) > 0 {
err = passToPacman(arguments)
if err != nil {
return fmt.Errorf("Error installing repo packages")
}
}
//we may have done -Sy, our handle now has an old
//database.
err = initAlpmHandle()
if err != nil {
return err
}
_, _, localNames, remoteNames, err = filterPackages()
if err != nil {
return err
}
remoteNamesCache = sliceToStringSet(remoteNames)
localNamesCache = sliceToStringSet(localNames)
}
}
requestTargets := parser.copy().targets
//create the arguments to pass for the repo install
@ -204,7 +134,7 @@ func install(parser *arguments) error {
parser.op = "S"
parser.delArg("y", "refresh")
parser.options["ignore"] = arguments.options["ignore"]
return passToPacman(parser)
return show(passToPacman(parser))
}
if len(dp.Aur) > 0 && 0 == os.Geteuid() {
@ -340,7 +270,7 @@ func install(parser *arguments) error {
}
if len(arguments.targets) > 0 || arguments.existsArg("u") {
err := passToPacman(arguments)
err := show(passToPacman(arguments))
if err != nil {
return fmt.Errorf("Error installing repo packages")
}
@ -364,14 +294,14 @@ func install(parser *arguments) error {
}
if len(depArguments.targets) > 0 {
_, stderr, err := passToPacmanCapture(depArguments)
_, stderr, err := capture(passToPacman(depArguments))
if err != nil {
return fmt.Errorf("%s%s", stderr, err)
}
}
if len(expArguments.targets) > 0 {
_, stderr, err := passToPacmanCapture(expArguments)
_, stderr, err := capture(passToPacman(expArguments))
if err != nil {
return fmt.Errorf("%s%s", stderr, err)
}
@ -398,7 +328,7 @@ func install(parser *arguments) error {
oldValue := config.NoConfirm
config.NoConfirm = true
err = passToPacman(removeArguments)
err = show(passToPacman(removeArguments))
config.NoConfirm = oldValue
if err != nil {
@ -413,6 +343,75 @@ func install(parser *arguments) error {
return nil
}
func inRepos(syncDb alpm.DbList, pkg string) bool {
target := toTarget(pkg)
if target.Db == "aur" {
return false
} else if target.Db != "" {
return true
}
_, err := syncDb.FindSatisfier(target.DepString())
if err == nil {
return true
}
_, err = syncDb.PkgCachebyGroup(target.Name)
if err == nil {
return true
}
return false
}
func earlyPacmanCall(parser *arguments) error {
arguments := parser.copy()
arguments.op = "S"
targets := parser.targets
parser.clearTargets()
arguments.clearTargets()
syncDb, err := alpmHandle.SyncDbs()
if err != nil {
return err
}
if mode == ModeRepo {
arguments.targets = targets
} else {
alpmHandle.SetQuestionCallback(func(alpm.QuestionAny) {})
//seperate aur and repo targets
for _, target := range targets {
if inRepos(syncDb, target) {
arguments.addTarget(target)
} else {
parser.addTarget(target)
}
}
}
if parser.existsArg("y", "refresh") || parser.existsArg("u", "sysupgrade") || len(arguments.targets) > 0 {
err = show(passToPacman(arguments))
if err != nil {
return fmt.Errorf("Error installing repo packages")
}
}
return nil
}
func earlyRefresh(parser *arguments) error {
arguments := parser.copy()
parser.delArg("y", "refresh")
arguments.delArg("u", "sysupgrade")
arguments.delArg("s", "search")
arguments.delArg("i", "info")
arguments.delArg("l", "list")
arguments.clearTargets()
return show(passToPacman(arguments))
}
func getIncompatible(pkgs []*rpc.Pkg, srcinfos map[string]*gosrc.Srcinfo, bases map[string][]*rpc.Pkg) (stringSet, error) {
incompatible := make(stringSet)
alpmArch, err := alpmHandle.Arch()
@ -449,7 +448,7 @@ nextpkg:
}
func parsePackageList(dir string) (map[string]string, string, error) {
stdout, stderr, err := passToMakepkgCapture(dir, "--packagelist")
stdout, stderr, err := capture(passToMakepkg(dir, "--packagelist"))
if err != nil {
return nil, "", fmt.Errorf("%s%s", stderr, err)
@ -677,7 +676,7 @@ func showPkgBuildDiffs(pkgs []*rpc.Pkg, srcinfos map[string]*gosrc.Srcinfo, base
} else {
args = append(args, "--color=never")
}
err := passToGit(dir, args...)
err := show(passToGit(dir, args...))
if err != nil {
return err
}
@ -838,7 +837,7 @@ func downloadPkgBuildsSources(pkgs []*rpc.Pkg, bases map[string][]*rpc.Pkg, inco
args = append(args, "--ignorearch")
}
err = passToMakepkg(dir, args...)
err = show(passToMakepkg(dir, args...))
if err != nil {
return fmt.Errorf("Error downloading sources: %s", cyan(formatPkgbase(pkg, bases)))
}
@ -861,7 +860,7 @@ func buildInstallPkgBuilds(dp *depPool, do *depOrder, srcinfos map[string]*gosrc
}
//pkgver bump
err := passToMakepkg(dir, args...)
err := show(passToMakepkg(dir, args...))
if err != nil {
return fmt.Errorf("Error making: %s", pkg.Name)
}
@ -899,7 +898,7 @@ func buildInstallPkgBuilds(dp *depPool, do *depOrder, srcinfos map[string]*gosrc
args = append(args, "--ignorearch")
}
err := passToMakepkg(dir, args...)
err := show(passToMakepkg(dir, args...))
if err != nil {
return fmt.Errorf("Error making: %s", pkg.Name)
}
@ -973,7 +972,7 @@ func buildInstallPkgBuilds(dp *depPool, do *depOrder, srcinfos map[string]*gosrc
}
}
err = passToPacman(arguments)
err = show(passToPacman(arguments))
if err != nil {
return err
}
@ -982,8 +981,13 @@ func buildInstallPkgBuilds(dp *depPool, do *depOrder, srcinfos map[string]*gosrc
updateVCSData(pkg.Name, srcinfo.Source)
}
err = saveVCSInfo()
if err != nil {
fmt.Println(err)
}
if len(depArguments.targets) > 0 {
_, stderr, err := passToPacmanCapture(depArguments)
_, stderr, err := capture(passToPacman(depArguments))
if err != nil {
return fmt.Errorf("%s%s", stderr, err)
}

View File

@ -229,7 +229,7 @@ func syncInfo(pkgS []string) (err error) {
arguments := cmdArgs.copy()
arguments.clearTargets()
arguments.addTarget(repoS...)
err = passToPacman(arguments)
err = show(passToPacman(arguments))
if err != nil {
return

13
vcs.go
View File

@ -1,11 +1,9 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
@ -136,13 +134,9 @@ func updateVCSData(pkgName string, sources []gosrc.ArchString) {
func getCommit(url string, branch string, protocols []string) string {
for _, protocol := range protocols {
var outbuf bytes.Buffer
cmd := exec.Command(config.GitBin, "ls-remote", protocol+"://"+url, branch)
cmd.Stdout = &outbuf
cmd.Env = append(cmd.Env, "GIT_TERMINAL_PROMPT=0")
err := cmd.Start()
cmd := passToGit("ls-remote", protocol+"://"+url, branch)
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
stdout, _, err := capture(cmd)
if err != nil {
continue
}
@ -161,7 +155,6 @@ func getCommit(url string, branch string, protocols []string) string {
continue
}
stdout := outbuf.String()
split := strings.Fields(stdout)
if len(split) < 2 {