@@ -109,48 +109,71 @@ func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Dura
109
109
// RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
110
110
// it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin. Between cmd.Start and cmd.Wait the passed in function is run.
111
111
func (c * Command ) RunInDirTimeoutEnvFullPipelineFunc (env []string , timeout time.Duration , dir string , stdout , stderr io.Writer , stdin io.Reader , fn func (context.Context , context.CancelFunc ) error ) error {
112
- if timeout == - 1 {
113
- timeout = DefaultCommandExecutionTimeout
112
+ return c .RunWithContext (& RunContext {
113
+ Env : env ,
114
+ Timeout : timeout ,
115
+ Dir : dir ,
116
+ Stdout : stdout ,
117
+ Stderr : stderr ,
118
+ Stdin : stdin ,
119
+ CancelFunc : fn ,
120
+ })
121
+ }
122
+
123
+ // RunContext represents parameters to run the command
124
+ type RunContext struct {
125
+ Env []string
126
+ Timeout time.Duration
127
+ Dir string
128
+ Stdout , Stderr io.Writer
129
+ Stdin io.Reader
130
+ CancelFunc func (context.Context , context.CancelFunc ) error
131
+ }
132
+
133
+ // RunWithContext run the command with context
134
+ func (c * Command ) RunWithContext (rc * RunContext ) error {
135
+ if rc .Timeout == - 1 {
136
+ rc .Timeout = DefaultCommandExecutionTimeout
114
137
}
115
138
116
- if len (dir ) == 0 {
139
+ if len (rc . Dir ) == 0 {
117
140
log (c .String ())
118
141
} else {
119
- log ("%s: %v" , dir , c )
142
+ log ("%s: %v" , rc . Dir , c )
120
143
}
121
144
122
- ctx , cancel := context .WithTimeout (c .parentContext , timeout )
145
+ ctx , cancel := context .WithTimeout (c .parentContext , rc . Timeout )
123
146
defer cancel ()
124
147
125
148
cmd := exec .CommandContext (ctx , c .name , c .args ... )
126
- if env == nil {
149
+ if rc . Env == nil {
127
150
cmd .Env = append (os .Environ (), fmt .Sprintf ("LC_ALL=%s" , DefaultLocale ))
128
151
} else {
129
- cmd .Env = env
152
+ cmd .Env = rc . Env
130
153
cmd .Env = append (cmd .Env , fmt .Sprintf ("LC_ALL=%s" , DefaultLocale ))
131
154
}
132
155
133
156
// TODO: verify if this is still needed in golang 1.15
134
157
if goVersionLessThan115 {
135
158
cmd .Env = append (cmd .Env , "GODEBUG=asyncpreemptoff=1" )
136
159
}
137
- cmd .Dir = dir
138
- cmd .Stdout = stdout
139
- cmd .Stderr = stderr
140
- cmd .Stdin = stdin
160
+ cmd .Dir = rc . Dir
161
+ cmd .Stdout = rc . Stdout
162
+ cmd .Stderr = rc . Stderr
163
+ cmd .Stdin = rc . Stdin
141
164
if err := cmd .Start (); err != nil {
142
165
return err
143
166
}
144
167
145
168
desc := c .desc
146
169
if desc == "" {
147
- desc = fmt .Sprintf ("%s %s %s [repo_path: %s]" , GitExecutable , c .name , strings .Join (c .args , " " ), dir )
170
+ desc = fmt .Sprintf ("%s %s %s [repo_path: %s]" , GitExecutable , c .name , strings .Join (c .args , " " ), rc . Dir )
148
171
}
149
172
pid := process .GetManager ().Add (desc , cancel )
150
173
defer process .GetManager ().Remove (pid )
151
174
152
- if fn != nil {
153
- err := fn (ctx , cancel )
175
+ if rc . CancelFunc != nil {
176
+ err := rc . CancelFunc (ctx , cancel )
154
177
if err != nil {
155
178
cancel ()
156
179
_ = cmd .Wait ()
0 commit comments