github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/goutils/exec/example_test.go (about) 1 /* 2 * Copyright (c) 2023-present unTill Software Development Group B. V. and Contributors 3 * @author Maxim Geraskin 4 * 5 * This source code is licensed under the MIT license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 package exec_test 10 11 import ( 12 "fmt" 13 "os" 14 "strings" 15 16 "github.com/voedger/voedger/pkg/goutils/exec" 17 ) 18 19 // Run a command and capture its output to strings 20 func ExamplePipedExec_RunToStrings() { 21 22 stdout, stderr, err := new(exec.PipedExec). 23 Command("echo", "RunToStrings"). 24 RunToStrings() 25 26 if err != nil { 27 fmt.Println("exec.PipedExec failed:", err, stderr) 28 } 29 30 fmt.Println(strings.TrimSpace(stdout)) 31 // Output: RunToStrings 32 } 33 34 // printf "1\n2\n3" | grep 2 35 func ExamplePipedExec_RunToStrings_pipe() { 36 37 stdout, stderr, err := new(exec.PipedExec). 38 Command("printf", `1\n2\n3`). 39 Command("grep", "2"). 40 RunToStrings() 41 42 if err != nil { 43 fmt.Println("exec.PipedExec failed:", err, stderr) 44 } 45 46 fmt.Println(strings.TrimSpace(stdout)) 47 // Output: 2 48 } 49 50 // Run a command and use os.Stdout, os.Stderr 51 func ExamplePipedExec_Run() { 52 53 err := new(exec.PipedExec). 54 Command("echo", "Run"). 55 Run(os.Stdout, os.Stderr) 56 57 if err != nil { 58 fmt.Println("exec.PipedExec failed:", err) 59 } 60 61 // Output: Run 62 }