github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/sh/run.go (about) 1 // Copyright 2018 the u-root 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 sh 6 7 import ( 8 "io" 9 "log" 10 "os" 11 "os/exec" 12 ) 13 14 // Run runs a command with stdin, stdout and stderr. 15 func Run(arg0 string, args ...string) error { 16 return RunWithIO(os.Stdin, os.Stdout, os.Stderr, arg0, args...) 17 } 18 19 // RunWithLogs runs a command with stdin, stdout and stderr. This function is 20 // more verbose than log.Run. 21 func RunWithLogs(arg0 string, args ...string) error { 22 log.Printf("executing command %q with args %q", arg0, args) 23 err := RunWithIO(os.Stdin, os.Stdout, os.Stderr, arg0, args...) 24 if err != nil { 25 log.Printf("command %q with args %q failed: %v", arg0, args, err) 26 } 27 return err 28 } 29 30 // RunWithIO runs a command with the given input, output and error. 31 func RunWithIO(in io.Reader, out, err io.Writer, arg0 string, args ...string) error { 32 cmd := exec.Command(arg0, args...) 33 cmd.Stdin = in 34 cmd.Stdout = out 35 cmd.Stderr = err 36 return cmd.Run() 37 } 38 39 // RunOrDie runs a commands with stdin, stdout and stderr. If there is a an 40 // error, it is fatally logged. 41 func RunOrDie(arg0 string, args ...string) { 42 if err := Run(arg0, args...); err != nil { 43 log.Fatal(err) 44 } 45 }