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