-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bash.go
102 lines (81 loc) · 2.1 KB
/
Bash.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package asciichgolangpublic
type BashService struct {
CommandExecutorBase
}
func Bash() (b *BashService) {
return NewBashService()
}
func NewBashService() (b *BashService) {
b = new(BashService)
b.SetParentCommandExecutorForBaseClass(b)
return b
}
func (b *BashService) MustRunCommand(options *RunCommandOptions) (commandOutput *CommandOutput) {
commandOutput, err := b.RunCommand(options)
if err != nil {
LogGoErrorFatal(err)
}
return commandOutput
}
func (b *BashService) MustRunOneLiner(oneLiner string, verbose bool) (output *CommandOutput) {
output, err := b.RunOneLiner(oneLiner, verbose)
if err != nil {
LogGoErrorFatal(err)
}
return output
}
func (b *BashService) MustRunOneLinerAndGetStdoutAsString(oneLiner string, verbose bool) (stdout string) {
stdout, err := b.RunOneLinerAndGetStdoutAsString(oneLiner, verbose)
if err != nil {
LogGoErrorFatal(err)
}
return stdout
}
func (b *BashService) RunCommand(options *RunCommandOptions) (commandOutput *CommandOutput, err error) {
if options == nil {
return nil, TracedErrorNil("options")
}
optionsToUse := options.GetDeepCopy()
joinedCommand, err := optionsToUse.GetJoinedCommand()
if err != nil {
return nil, err
}
bashCommand := []string{
"bash",
"-c",
joinedCommand,
}
optionsToUse.Command = bashCommand
commandOutput, err = Exec().RunCommand(optionsToUse)
if err != nil {
return nil, err
}
return commandOutput, nil
}
func (b *BashService) RunOneLiner(oneLiner string, verbose bool) (output *CommandOutput, err error) {
if oneLiner == "" {
return nil, TracedErrorEmptyString("oneLiner")
}
output, err = b.RunCommand(
&RunCommandOptions{
Command: []string{oneLiner},
Verbose: verbose,
LiveOutputOnStdout: verbose,
},
)
if err != nil {
return nil, err
}
return output, nil
}
func (b *BashService) RunOneLinerAndGetStdoutAsString(oneLiner string, verbose bool) (stdout string, err error) {
output, err := b.RunOneLiner(oneLiner, verbose)
if err != nil {
return "", err
}
stdout, err = output.GetStdoutAsString()
if err != nil {
return "", err
}
return stdout, nil
}