-
Notifications
You must be signed in to change notification settings - Fork 47
/
fork.go
80 lines (72 loc) · 2.09 KB
/
fork.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
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"
"os"
"os/exec"
)
// ForkParsed parses a command into binary and arguments.
// It resolves the command name in your $PATH list for you.
// It shows output and allows input.
// It is intended to be used in scripting.
func ForkParsed(command string) error {
cmd, err := CmdParsed(command)
if err != nil {
return err
}
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
// ForkParsedContext parses a command into binary and arguments.
// It resolves the command name in your $PATH list for you.
// It shows output and allows input.
// It is intended to be used in scripting.
func ForkParsedContext(ctx context.Context, command string) error {
cmd, err := CmdParsedContext(ctx, command)
if err != nil {
return err
}
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
}
// Fork runs a command with a given list of arguments.
// It resolves the command name in your $PATH list for you.
// It shows output and allows input.
// It is intended to be used in scripting.
func Fork(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 = os.Stdin
return cmd.Run()
}
// ForkContext runs a command with a given list of arguments and context.
// It resolves the command name in your $PATH list for you.
// It shows output and allows input.
// It is intended to be used in scripting.
func ForkContext(ctx context.Context, 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 = os.Stdin
return cmd.Run()
}