github.com/eun/go@v0.0.0-20170811110501-92cfd07a6cfd/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_Run() {
    61  	cmd := exec.Command("sleep", "1")
    62  	log.Printf("Running command and waiting for it to finish...")
    63  	err := cmd.Run()
    64  	log.Printf("Command finished with error: %v", err)
    65  }
    66  
    67  func ExampleCmd_Start() {
    68  	cmd := exec.Command("sleep", "5")
    69  	err := cmd.Start()
    70  	if err != nil {
    71  		log.Fatal(err)
    72  	}
    73  	log.Printf("Waiting for command to finish...")
    74  	err = cmd.Wait()
    75  	log.Printf("Command finished with error: %v", err)
    76  }
    77  
    78  func ExampleCmd_StdoutPipe() {
    79  	cmd := exec.Command("echo", "-n", `{"Name": "Bob", "Age": 32}`)
    80  	stdout, err := cmd.StdoutPipe()
    81  	if err != nil {
    82  		log.Fatal(err)
    83  	}
    84  	if err := cmd.Start(); err != nil {
    85  		log.Fatal(err)
    86  	}
    87  	var person struct {
    88  		Name string
    89  		Age  int
    90  	}
    91  	if err := json.NewDecoder(stdout).Decode(&person); err != nil {
    92  		log.Fatal(err)
    93  	}
    94  	if err := cmd.Wait(); err != nil {
    95  		log.Fatal(err)
    96  	}
    97  	fmt.Printf("%s is %d years old\n", person.Name, person.Age)
    98  }
    99  
   100  func ExampleCmd_StdinPipe() {
   101  	cmd := exec.Command("cat")
   102  	stdin, err := cmd.StdinPipe()
   103  	if err != nil {
   104  		log.Fatal(err)
   105  	}
   106  
   107  	go func() {
   108  		defer stdin.Close()
   109  		io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
   110  	}()
   111  
   112  	out, err := cmd.CombinedOutput()
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  
   117  	fmt.Printf("%s\n", out)
   118  }
   119  
   120  func ExampleCmd_StderrPipe() {
   121  	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
   122  	stderr, err := cmd.StderrPipe()
   123  	if err != nil {
   124  		log.Fatal(err)
   125  	}
   126  
   127  	if err := cmd.Start(); err != nil {
   128  		log.Fatal(err)
   129  	}
   130  
   131  	slurp, _ := ioutil.ReadAll(stderr)
   132  	fmt.Printf("%s\n", slurp)
   133  
   134  	if err := cmd.Wait(); err != nil {
   135  		log.Fatal(err)
   136  	}
   137  }
   138  
   139  func ExampleCmd_CombinedOutput() {
   140  	cmd := exec.Command("sh", "-c", "echo stdout; echo 1>&2 stderr")
   141  	stdoutStderr, err := cmd.CombinedOutput()
   142  	if err != nil {
   143  		log.Fatal(err)
   144  	}
   145  	fmt.Printf("%s\n", stdoutStderr)
   146  }
   147  
   148  func ExampleCommandContext() {
   149  	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
   150  	defer cancel()
   151  
   152  	if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil {
   153  		// This will fail after 100 milliseconds. The 5 second sleep
   154  		// will be interrupted.
   155  	}
   156  }