github.com/blend/go-sdk@v1.20220411.3/sh/put.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package sh 9 10 import ( 11 "context" 12 "io" 13 "os" 14 "os/exec" 15 ) 16 17 // PutContext runs a given command with a given reader as its stdin in a context. 18 func PutContext(ctx context.Context, stdin io.Reader, command string, args ...string) error { 19 absoluteCommand, err := exec.LookPath(command) 20 if err != nil { 21 return err 22 } 23 cmd := exec.CommandContext(ctx, absoluteCommand, args...) 24 cmd.Env = os.Environ() 25 cmd.Stdout = os.Stdout 26 cmd.Stderr = os.Stderr 27 cmd.Stdin = stdin 28 return cmd.Run() 29 } 30 31 // Put runs a given command with a given reader as its stdin. 32 func Put(stdin io.Reader, command string, args ...string) error { 33 absoluteCommand, err := exec.LookPath(command) 34 if err != nil { 35 return err 36 } 37 cmd := exec.Command(absoluteCommand, args...) 38 cmd.Env = os.Environ() 39 cmd.Stdout = os.Stdout 40 cmd.Stderr = os.Stderr 41 cmd.Stdin = stdin 42 return cmd.Run() 43 }