-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
exec.go
133 lines (109 loc) · 2.77 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"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 := strings.TrimSpace(outbuf.String())
stderr := strings.TrimSpace(errbuf.String())
return stdout, stderr, err
}
func sudoLoopBackground() {
updateSudo()
go sudoLoop()
}
func sudoLoop() {
for {
updateSudo()
time.Sleep(298 * time.Second)
}
}
func updateSudo() {
for {
mSudoFlags := strings.Fields(config.SudoFlags)
mSudoFlags = append([]string{"-v"}, mSudoFlags...)
err := show(exec.Command(config.SudoBin, mSudoFlags...))
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
break
}
}
}
// waitLock will lock yay checking the status of db.lck until it does not exist
func waitLock() {
if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
return
}
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...")
for {
time.Sleep(3 * time.Second)
if _, err := os.Stat(filepath.Join(pacmanConf.DBPath, "db.lck")); err != nil {
fmt.Println()
return
}
}
}
func passToPacman(args *arguments) *exec.Cmd {
argArr := make([]string, 0)
mSudoFlags := strings.Fields(config.SudoFlags)
if args.needRoot() {
argArr = append(argArr, config.SudoBin)
argArr = append(argArr, mSudoFlags...)
}
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, "--config", config.PacmanConf)
argArr = append(argArr, "--")
argArr = append(argArr, args.targets...)
if args.needRoot() {
waitLock()
}
return exec.Command(argArr[0], argArr[1:]...)
}
func passToMakepkg(dir string, args ...string) *exec.Cmd {
mflags := strings.Fields(config.MFlags)
args = append(args, mflags...)
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 {
cmd := exec.Command("test", "-t", "1")
cmd.Stdout = os.Stdout
err := cmd.Run()
return err == nil
}