2018-07-19 17:48:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-07-21 15:18:19 +00:00
|
|
|
"path/filepath"
|
2018-07-19 17:48:46 +00:00
|
|
|
"strings"
|
2018-07-19 17:56:40 +00:00
|
|
|
"time"
|
2020-05-02 14:17:20 +00:00
|
|
|
|
2020-05-04 07:24:32 +00:00
|
|
|
"github.com/leonelquinteros/gotext"
|
2020-05-02 14:17:20 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2020-05-04 07:24:32 +00:00
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/settings"
|
2020-06-13 17:29:50 +00:00
|
|
|
"github.com/Jguer/yay/v10/pkg/text"
|
2018-07-19 17:48:46 +00: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
|
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
func capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
|
2018-07-19 17:48:46 +00:00
|
|
|
var outbuf, errbuf bytes.Buffer
|
|
|
|
|
|
|
|
cmd.Stdout = &outbuf
|
|
|
|
cmd.Stderr = &errbuf
|
2020-05-02 14:17:20 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
stdout = strings.TrimSpace(outbuf.String())
|
|
|
|
stderr = strings.TrimSpace(errbuf.String())
|
2018-07-19 17:48:46 +00:00
|
|
|
|
|
|
|
return stdout, stderr, err
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:56:40 +00:00
|
|
|
func sudoLoopBackground() {
|
|
|
|
updateSudo()
|
|
|
|
go sudoLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sudoLoop() {
|
|
|
|
for {
|
|
|
|
updateSudo()
|
|
|
|
time.Sleep(298 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSudo() {
|
|
|
|
for {
|
2019-03-19 21:10:58 +00:00
|
|
|
mSudoFlags := strings.Fields(config.SudoFlags)
|
|
|
|
mSudoFlags = append([]string{"-v"}, mSudoFlags...)
|
|
|
|
err := show(exec.Command(config.SudoBin, mSudoFlags...))
|
2018-07-19 17:56:40 +00:00
|
|
|
if err != nil {
|
2018-11-14 12:14:41 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
2018-07-19 17:56:40 +00:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 15:18:19 +00:00
|
|
|
// waitLock will lock yay checking the status of db.lck until it does not exist
|
2020-07-05 13:34:27 +00:00
|
|
|
func waitLock(dbPath string) {
|
|
|
|
lockDBPath := filepath.Join(dbPath, "db.lck")
|
|
|
|
if _, err := os.Stat(lockDBPath); err != nil {
|
2018-07-21 15:18:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-05 13:34:27 +00:00
|
|
|
text.Warnln(gotext.Get("%s is present.", lockDBPath))
|
2020-05-04 07:24:32 +00:00
|
|
|
text.Warn(gotext.Get("There may be another Pacman instance running. Waiting..."))
|
2018-07-21 15:18:19 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(3 * time.Second)
|
2020-07-05 13:34:27 +00:00
|
|
|
if _, err := os.Stat(lockDBPath); err != nil {
|
2018-07-21 16:36:16 +00:00
|
|
|
fmt.Println()
|
2018-07-21 15:18:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:45:23 +00:00
|
|
|
func passToPacman(args *settings.Arguments) *exec.Cmd {
|
2018-07-19 17:48:46 +00:00
|
|
|
argArr := make([]string, 0)
|
|
|
|
|
2019-03-19 21:10:58 +00:00
|
|
|
mSudoFlags := strings.Fields(config.SudoFlags)
|
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
if args.NeedRoot(config.Runtime) {
|
2019-03-19 21:10:58 +00:00
|
|
|
argArr = append(argArr, config.SudoBin)
|
|
|
|
argArr = append(argArr, mSudoFlags...)
|
2018-07-19 17:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argArr = append(argArr, config.PacmanBin)
|
2020-07-08 01:22:01 +00:00
|
|
|
argArr = append(argArr, args.FormatGlobals()...)
|
2020-07-05 00:45:23 +00:00
|
|
|
argArr = append(argArr, args.FormatArgs()...)
|
2018-07-19 17:48:46 +00:00
|
|
|
if config.NoConfirm {
|
|
|
|
argArr = append(argArr, "--noconfirm")
|
|
|
|
}
|
|
|
|
|
2020-05-02 14:17:20 +00:00
|
|
|
argArr = append(argArr, "--config", config.PacmanConf, "--")
|
2020-07-05 00:45:23 +00:00
|
|
|
argArr = append(argArr, args.Targets...)
|
2018-07-19 17:48:46 +00:00
|
|
|
|
2020-07-05 01:17:35 +00:00
|
|
|
if args.NeedRoot(config.Runtime) {
|
2020-07-05 13:34:27 +00:00
|
|
|
waitLock(config.Runtime.PacmanConf.DBPath)
|
2018-07-21 15:18:19 +00:00
|
|
|
}
|
2018-07-19 17:48:46 +00:00
|
|
|
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-17 23:53:53 +00:00
|
|
|
if config.MakepkgConf != "" {
|
|
|
|
args = append(args, "--config", config.MakepkgConf)
|
|
|
|
}
|
|
|
|
|
2018-07-19 17:48:46 +00: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 15:50:59 +00:00
|
|
|
|
|
|
|
func isTty() bool {
|
2020-03-19 17:43:12 +00:00
|
|
|
return terminal.IsTerminal(int(os.Stdout.Fd()))
|
2018-09-27 15:50:59 +00:00
|
|
|
}
|