github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/os/exec/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package exec_test 6 7 import ( 8 "bytes" 9 "context" 10 "encoding/json" 11 "fmt" 12 "io" 13 "io/ioutil" 14 "log" 15 "os" 16 "os/exec" 17 "strings" 18 "time" 19 ) 20 21 func ExampleLookPath() { 22 path, err := exec.LookPath("fortune") 23 if err != nil { 24 log.Fatal("installing fortune is in your future") 25 } 26 fmt.Printf("fortune is available at %s\n", path) 27 } 28 29 func ExampleCommand() { 30 cmd := exec.Command("tr", "a-z", "A-Z") 31 cmd.Stdin = strings.NewReader("some input") 32 var out bytes.Buffer 33 cmd.Stdout = &out 34 err := cmd.Run() 35 if err != nil { 36 log.Fatal(err) 37 } 38 fmt.Printf("in all caps: %q\n", out.String()) 39 } 40 41 func ExampleCommand_environment() { 42 cmd := exec.Command("prog") 43 cmd.Env = append(os.Environ(), 44 "FOO=duplicate_value", // ignored 45 "FOO=actual_value", // this value is used 46 ) 47 if err := cmd.Run(); err != nil { 48 log.Fatal(err) 49 } 50 } 51 52 func ExampleCmd_Output() { 53 out, err := exec.Command("date").Output() 54 if err != nil { 55 log.Fatal(err) 56 } 57 fmt.Printf("The date is %s\n", out) 58 } 59 60 func ExampleCmd_Start() { 61 cmd := exec.Command("sleep", "5") 62 err := cmd.Start() 63 if err != nil { 64 log.Fatal(err) 65 } 66 log.Printf("Waiting for command to finish...") 67 err = cmd.Wait() 68 log.Printf("Command finished with error: %v", err) 69 } 70 71 func ExampleCmd_StdoutPipe() { 72 cmd := exec.Command("echo", "-n", `{"Name": "Bob", "Age": 32}`) 73 stdout, err := cmd.StdoutPipe() 74 if err != nil { 75 log.Fatal(err) 76 } 77 if err := cmd.Start(); err != nil { 78 log.Fatal(err) 79 } 80 var person struct { 81 Name string 82 Age int 83 } 84 if err := json.NewDecoder(stdout).Decode(&person); err != nil { 85 log.Fatal(err) 86 } 87 if err := cmd.Wait(); err != nil { 88 log.Fatal(err) 89 } 90 fmt.Printf("%s is %d years old\n", person.Name, person.Age) 91 } 92 93 func ExampleCmd_StdinPipe() { 94 cmd := exec.Command("cat") 95 stdin, err := cmd.StdinPipe() 96 if err != nil { 97 log.Fatal(err) 98 } 99 100 go func() { 101 defer stdin.Close() 102 io.WriteString(stdin, "values written to stdin are passed to cmd's standard input") 103 }() 104 105 out, err := cmd.CombinedOutput() 106 if err != nil { 107 log.Fatal(err) 108 } 109 110 fmt.Printf("%s\n", out) 111 } 112 113 func ExampleCmd_StderrPipe() { 114 cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr") 115 stderr, err := cmd.StderrPipe() 116 if err != nil { 117 log.Fatal(err) 118 } 119 120 if err := cmd.Start(); err != nil { 121 log.Fatal(err) 122 } 123 124 slurp, _ := ioutil.ReadAll(stderr) 125 fmt.Printf("%s\n", slurp) 126 127 if err := cmd.Wait(); err != nil { 128 log.Fatal(err) 129 } 130 } 131 132 func ExampleCmd_CombinedOutput() { 133 cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr") 134 stdoutStderr, err := cmd.CombinedOutput() 135 if err != nil { 136 log.Fatal(err) 137 } 138 fmt.Printf("%s\n", stdoutStderr) 139 } 140 141 func ExampleCommandContext() { 142 ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) 143 defer cancel() 144 145 if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil { 146 // This will fail after 100 milliseconds. The 5 second sleep 147 // will be interrupted. 148 } 149 }