| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package exec_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io/ioutil" |
| 22 |
|
| 23 |
"k8s.io/utils/exec" |
| 24 |
) |
| 25 |
|
| 26 |
func ExampleNew_stderrPipe() { |
| 27 |
cmd := exec.New().Command("/bin/sh", "-c", "echo 'We can read from stderr via pipe!' >&2") |
| 28 |
|
| 29 |
stderrPipe, err := cmd.StderrPipe() |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
|
| 34 |
stderr := make(chan []byte) |
| 35 |
go func() { |
| 36 |
b, err := ioutil.ReadAll(stderrPipe) |
| 37 |
if err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
stderr <- b |
| 41 |
}() |
| 42 |
|
| 43 |
if err := cmd.Start(); err != nil { |
| 44 |
panic(err) |
| 45 |
} |
| 46 |
|
| 47 |
received := <-stderr |
| 48 |
|
| 49 |
if err := cmd.Wait(); err != nil { |
| 50 |
panic(err) |
| 51 |
} |
| 52 |
|
| 53 |
fmt.Println(string(received)) |
| 54 |
// Output: We can read from stderr via pipe! |
| 55 |
} |