-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
65 lines (55 loc) · 1.43 KB
/
proxy.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
package cli
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/suzuki-shunsuke/go-error-with-exit-code/ecerror"
)
type Runner struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
var errAquaCantBeExecuted = errors.New(`the command "aqua" can't be executed via aqua-proxy to prevent the infinite loop`)
func (runner *Runner) Run(ctx context.Context, args ...string) error {
cmdName := filepath.Base(args[0])
if cmdName == "aqua" {
fmt.Fprintln(os.Stderr, "[ERROR] "+errAquaCantBeExecuted.Error())
return errAquaCantBeExecuted
}
cmd := exec.CommandContext(ctx, "aqua", append([]string{"exec", "--", cmdName}, args[1:]...)...) //nolint:gosec
cmd.Stdin = runner.Stdin
cmd.Stdout = runner.Stdout
cmd.Stderr = runner.Stderr
setCancel(cmd)
if err := cmd.Run(); err != nil {
return ecerror.Wrap(err, cmd.ProcessState.ExitCode())
}
return nil
}
const waitDelay = 1000 * time.Hour
func setCancel(cmd *exec.Cmd) {
cmd.Cancel = func() error {
return cmd.Process.Signal(os.Interrupt)
}
cmd.WaitDelay = waitDelay
}
func absoluteAquaPath() (string, error) {
aquaPath, err := exec.LookPath("aqua")
if err != nil {
return "", fmt.Errorf("aqua isn't found: %w", err)
}
if filepath.IsAbs(aquaPath) {
return aquaPath, nil
}
a, err := filepath.Abs(aquaPath)
if err != nil {
return "", fmt.Errorf(`convert relative path "%s" to absolute path: %w`, aquaPath, err)
}
return a, nil
}