github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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/exec"
    16  	"strings"
    17  	"time"
    18  )
    19  
    20  func ExampleLookPath() {
    21  	path, err := exec.LookPath("fortune")
    22  	if err != nil {
    23  		log.Fatal("installing fortune is in your future")
    24  	}
    25  	fmt.Printf("fortune is available at %s\n", path)
    26  }
    27  
    28  func ExampleCommand() {
    29  	cmd := exec.Command("tr", "a-z", "A-Z")
    30  	cmd.Stdin = strings.NewReader("some input")
    31  	var out bytes.Buffer
    32  	cmd.Stdout = &out
    33  	err := cmd.Run()
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  	fmt.Printf("in all caps: %q\n", out.String())
    38  }
    39  
    40  func ExampleCmd_Output() {
    41  	out, err := exec.Command("date").Output()
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  	fmt.Printf("The date is %s\n", out)
    46  }
    47  
    48  func ExampleCmd_Start() {
    49  	cmd := exec.Command("sleep", "5")
    50  	err := cmd.Start()
    51  	if err != nil {
    52  		log.Fatal(err)
    53  	}
    54  	log.Printf("Waiting for command to finish...")
    55  	err = cmd.Wait()
    56  	log.Printf("Command finished with error: %v", err)
    57  }
    58  
    59  func ExampleCmd_StdoutPipe() {
    60  	cmd := exec.Command("echo", "-n", `{"Name": "Bob", "Age": 32}`)
    61  	stdout, err := cmd.StdoutPipe()
    62  	if err != nil {
    63  		log.Fatal(err)
    64  	}
    65  	if err := cmd.Start(); err != nil {
    66  		log.Fatal(err)
    67  	}
    68  	var person struct {
    69  		Name string
    70  		Age  int
    71  	}
    72  	if err := json.NewDecoder(stdout).Decode(&person); err != nil {
    73  		log.Fatal(err)
    74  	}
    75  	if err := cmd.Wait(); err != nil {
    76  		log.Fatal(err)
    77  	}
    78  	fmt.Printf("%s is %d years old\n", person.Name, person.Age)
    79  }
    80  
    81  func ExampleCmd_StdinPipe() {
    82  	cmd := exec.Command("cat")
    83  	stdin, err := cmd.StdinPipe()
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  
    88  	go func() {
    89  		defer stdin.Close()
    90  		io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
    91  	}()
    92  
    93  	out, err := cmd.CombinedOutput()
    94  	if err != nil {
    95  		log.Fatal(err)
    96  	}
    97  
    98  	fmt.Printf("%s\n", out)
    99  }
   100  
   101  func ExampleCmd_StderrPipe() {
   102  	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
   103  	stderr, err := cmd.StderrPipe()
   104  	if err != nil {
   105  		log.Fatal(err)
   106  	}
   107  
   108  	if err := cmd.Start(); err != nil {
   109  		log.Fatal(err)
   110  	}
   111  
   112  	slurp, _ := ioutil.ReadAll(stderr)
   113  	fmt.Printf("%s\n", slurp)
   114  
   115  	if err := cmd.Wait(); err != nil {
   116  		log.Fatal(err)
   117  	}
   118  }
   119  
   120  func ExampleCmd_CombinedOutput() {
   121  	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
   122  	stdoutStderr, err := cmd.CombinedOutput()
   123  	if err != nil {
   124  		log.Fatal(err)
   125  	}
   126  	fmt.Printf("%s\n", stdoutStderr)
   127  }
   128  
   129  func ExampleCommandContext() {
   130  	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
   131  	defer cancel()
   132  
   133  	if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil {
   134  		// This will fail after 100 milliseconds. The 5 second sleep
   135  		// will be interrupted.
   136  	}
   137  }