Exec executes a command inside a container, returning the result containing stdout, stderr, and exit code. Note: - this is a synchronous operation; - cmd stdin is closed.
(ctx context.Context, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions))
| 47 | // - this is a synchronous operation; |
| 48 | // - cmd stdin is closed. |
| 49 | func Exec(ctx context.Context, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions)) (ExecResult, error) { |
| 50 | // prepare exec |
| 51 | execOptions := client.ExecCreateOptions{ |
| 52 | AttachStdout: true, |
| 53 | AttachStderr: true, |
| 54 | Cmd: cmd, |
| 55 | } |
| 56 | |
| 57 | for _, op := range ops { |
| 58 | op(&execOptions) |
| 59 | } |
| 60 | |
| 61 | res, err := apiClient.ExecCreate(ctx, id, execOptions) |
| 62 | if err != nil { |
| 63 | return ExecResult{}, err |
| 64 | } |
| 65 | execID := res.ID |
| 66 | |
| 67 | // run it, with stdout/stderr attached |
| 68 | aresp, err := apiClient.ExecAttach(ctx, execID, client.ExecAttachOptions{}) |
| 69 | if err != nil { |
| 70 | return ExecResult{}, err |
| 71 | } |
| 72 | |
| 73 | // read the output |
| 74 | s, err := demultiplexStreams(ctx, aresp.HijackedResponse) |
| 75 | if err != nil { |
| 76 | return ExecResult{}, err |
| 77 | } |
| 78 | |
| 79 | // get the exit code |
| 80 | inspect, err := apiClient.ExecInspect(ctx, execID, client.ExecInspectOptions{}) |
| 81 | if err != nil { |
| 82 | return ExecResult{}, err |
| 83 | } |
| 84 | |
| 85 | return ExecResult{ExitCode: inspect.ExitCode, outBuffer: &s.stdout, errBuffer: &s.stderr}, nil |
| 86 | } |
| 87 | |
| 88 | // ExecT calls Exec() and aborts the test if an error occurs. |
| 89 | func ExecT(ctx context.Context, t testing.TB, apiClient client.APIClient, id string, cmd []string, ops ...func(*client.ExecCreateOptions)) ExecResult { |
searching dependent graphs…