StartProxy returns a proxyExec representing a running instance of the proxy.
(ctx context.Context, args ...string)
| 55 | |
| 56 | // StartProxy returns a proxyExec representing a running instance of the proxy. |
| 57 | func StartProxy(ctx context.Context, args ...string) (*ProxyExec, error) { |
| 58 | ctx, cancel := context.WithCancel(ctx) |
| 59 | // Open a pipe for tracking the output from the cmd |
| 60 | pr, pw, err := os.Pipe() |
| 61 | if err != nil { |
| 62 | cancel() |
| 63 | return nil, fmt.Errorf("unable to open stdout pipe: %w", err) |
| 64 | } |
| 65 | |
| 66 | c := cmd.NewCommand(cmd.WithLogger(log.NewStdLogger(pw, pw))) |
| 67 | c.SetArgs(args) |
| 68 | c.SetOut(pw) |
| 69 | c.SetErr(pw) |
| 70 | |
| 71 | p := &ProxyExec{ |
| 72 | Out: pr, |
| 73 | cmd: c, |
| 74 | cancel: cancel, |
| 75 | closers: []io.Closer{pr, pw}, |
| 76 | done: make(chan bool), |
| 77 | } |
| 78 | // Start the command in the background |
| 79 | go func() { |
| 80 | defer close(p.done) |
| 81 | defer cancel() |
| 82 | p.err = c.ExecuteContext(ctx) |
| 83 | }() |
| 84 | return p, nil |
| 85 | } |
| 86 | |
| 87 | // Stop sends the TERM signal to the proxy and returns. |
| 88 | func (p *ProxyExec) Stop() { |
no test coverage detected