github.com/blend/go-sdk@v1.20220411.3/sh/put_output.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  	"bytes"
    12  	"io"
    13  	"os"
    14  	"os/exec"
    15  )
    16  
    17  // PutOutput runs a given command with a given reader as its stdin and captures the output.
    18  func PutOutput(stdin io.Reader, command string, args ...string) ([]byte, error) {
    19  	absoluteCommand, err := exec.LookPath(command)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	cmd := exec.Command(absoluteCommand, args...)
    24  	cmd.Env = os.Environ()
    25  	buffer := new(bytes.Buffer)
    26  	cmd.Stdout = buffer
    27  	cmd.Stderr = buffer
    28  	cmd.Stdin = stdin
    29  	return buffer.Bytes(), cmd.Run()
    30  }