This example shows how to run a command and capture it's output.
()
| 58 | |
| 59 | // This example shows how to run a command and capture it's output. |
| 60 | func ExampleCapture() { |
| 61 | ctx := newCtx() |
| 62 | stdout := &bytes.Buffer{} |
| 63 | stderr := &bytes.Buffer{} |
| 64 | echo := shell.Command("echo").Verbose().Capture(stdout, stderr) |
| 65 | echo.With("First echo").Run(ctx) |
| 66 | echo.With("Second echo").Run(ctx) |
| 67 | log.I(ctx, "<{<%s}>", stdout.String()) |
| 68 | log.W(ctx, "<!{<%s}!>", stderr.String()) |
| 69 | // Output: |
| 70 | //I: [Example] Exec: echo "First echo" |
| 71 | //I: [Example] <echo> First echo |
| 72 | //I: [Example] Exec: echo "Second echo" |
| 73 | //I: [Example] <echo> Second echo |
| 74 | //I: [Example] <{<First echo |
| 75 | //Second echo |
| 76 | //}> |
| 77 | //W: [Example] <!{<}!> |
| 78 | } |