yay/exec.go

136 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
2018-07-19 19:56:40 +02:00
"time"
"github.com/leonelquinteros/gotext"
"golang.org/x/crypto/ssh/terminal"
2020-07-05 02:45:23 +02:00
"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
}
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 {
2019-03-19 22:10:58 +01:00
mSudoFlags := strings.Fields(config.SudoFlags)
mSudoFlags = append([]string{"-v"}, mSudoFlags...)
err := show(exec.Command(config.SudoBin, mSudoFlags...))
2018-07-19 19:56:40 +02:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
2018-07-19 19:56:40 +02:00
} else {
break
}
}
}
// waitLock will lock yay checking the status of db.lck until it does not exist
func waitLock(dbPath string) {
lockDBPath := filepath.Join(dbPath, "db.lck")
if _, err := os.Stat(lockDBPath); err != nil {
return
}
text.Warnln(gotext.Get("%s is present.", lockDBPath))
text.Warn(gotext.Get("There may be another Pacman instance running. Waiting..."))
for {
time.Sleep(3 * time.Second)
if _, err := os.Stat(lockDBPath); err != nil {
fmt.Println()
return
}
}
}
2020-07-05 02:45:23 +02:00
func passToPacman(args *settings.Arguments) *exec.Cmd {
argArr := make([]string, 0)
2019-03-19 22:10:58 +01:00
mSudoFlags := strings.Fields(config.SudoFlags)
if args.NeedRoot(config.Runtime) {
2019-03-19 22:10:58 +01:00
argArr = append(argArr, config.SudoBin)
argArr = append(argArr, mSudoFlags...)
}
argArr = append(argArr, config.PacmanBin)
2020-07-08 03:22:01 +02:00
argArr = append(argArr, args.FormatGlobals()...)
2020-07-05 02:45:23 +02:00
argArr = append(argArr, args.FormatArgs()...)
if config.NoConfirm {
argArr = append(argArr, "--noconfirm")
}
argArr = append(argArr, "--config", config.PacmanConf, "--")
2020-07-05 02:45:23 +02:00
argArr = append(argArr, args.Targets...)
if args.NeedRoot(config.Runtime) {
waitLock(config.Runtime.PacmanConf.DBPath)
}
return exec.Command(argArr[0], argArr[1:]...)
}
func passToMakepkg(dir string, args ...string) *exec.Cmd {
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)
}
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
}
func isTty() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}