2018-07-19 19:48:46 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-07-21 17:18:19 +02:00
|
|
|
"path/filepath"
|
2018-07-19 19:48:46 +02:00
|
|
|
"strings"
|
2018-07-19 19:56:40 +02:00
|
|
|
"time"
|
2018-07-19 19:48:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2018-08-18 07:38:32 +02:00
|
|
|
stdout := strings.TrimSpace(outbuf.String())
|
|
|
|
stderr := strings.TrimSpace(errbuf.String())
|
2018-07-19 19:48:46 +02:00
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:56:40 +02:00
|
|
|
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 {
|
2018-11-14 13:14:41 +01:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-07-19 19:56:40 +02:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 17:18:19 +02:00
|
|
|
// waitLock will lock yay checking the status of db.lck until it does not exist
|
|
|
|
func waitLock() {
|
2018-09-04 16:47:22 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
|
2018-07-21 17:18:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-13 19:44:10 +02:00
|
|
|
fmt.Println(bold(yellow(smallArrow)), filepath.Join(pacmanConf.DBPath, "db.lck"), "is present.")
|
|
|
|
|
|
|
|
fmt.Print(bold(yellow(smallArrow)), " There may be another Pacman instance running. Waiting...")
|
2018-07-21 17:18:19 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(3 * time.Second)
|
2018-09-04 16:47:22 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
|
2018-07-21 18:36:16 +02:00
|
|
|
fmt.Println()
|
2018-07-21 17:18:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:48:46 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-07-28 20:51:19 +02:00
|
|
|
argArr = append(argArr, "--config", config.PacmanConf)
|
2018-07-19 19:48:46 +02:00
|
|
|
argArr = append(argArr, "--")
|
|
|
|
argArr = append(argArr, args.targets...)
|
|
|
|
|
2018-07-21 18:36:16 +02:00
|
|
|
if args.needRoot() {
|
2018-07-21 17:18:19 +02:00
|
|
|
waitLock()
|
|
|
|
}
|
2018-07-19 19:48:46 +02:00
|
|
|
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...)
|
|
|
|
|
2018-07-18 01:53:53 +02:00
|
|
|
if config.MakepkgConf != "" {
|
|
|
|
args = append(args, "--config", config.MakepkgConf)
|
|
|
|
}
|
|
|
|
|
2018-07-19 19:48:46 +02:00
|
|
|
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...)
|
|
|
|
return cmd
|
|
|
|
}
|
2018-09-27 17:50:59 +02:00
|
|
|
|
|
|
|
func isTty() bool {
|
|
|
|
cmd := exec.Command("test", "-t", "1")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
return err == nil
|
|
|
|
}
|