@@ -109,48 +109,71 @@ func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Dura
109109// RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
110110// 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.
111111func (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
114137 }
115138
116- if len (dir ) == 0 {
139+ if len (rc . Dir ) == 0 {
117140 log (c .String ())
118141 } else {
119- log ("%s: %v" , dir , c )
142+ log ("%s: %v" , rc . Dir , c )
120143 }
121144
122- ctx , cancel := context .WithTimeout (c .parentContext , timeout )
145+ ctx , cancel := context .WithTimeout (c .parentContext , rc . Timeout )
123146 defer cancel ()
124147
125148 cmd := exec .CommandContext (ctx , c .name , c .args ... )
126- if env == nil {
149+ if rc . Env == nil {
127150 cmd .Env = append (os .Environ (), fmt .Sprintf ("LC_ALL=%s" , DefaultLocale ))
128151 } else {
129- cmd .Env = env
152+ cmd .Env = rc . Env
130153 cmd .Env = append (cmd .Env , fmt .Sprintf ("LC_ALL=%s" , DefaultLocale ))
131154 }
132155
133156 // TODO: verify if this is still needed in golang 1.15
134157 if goVersionLessThan115 {
135158 cmd .Env = append (cmd .Env , "GODEBUG=asyncpreemptoff=1" )
136159 }
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
141164 if err := cmd .Start (); err != nil {
142165 return err
143166 }
144167
145168 desc := c .desc
146169 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 )
148171 }
149172 pid := process .GetManager ().Add (desc , cancel )
150173 defer process .GetManager ().Remove (pid )
151174
152- if fn != nil {
153- err := fn (ctx , cancel )
175+ if rc . CancelFunc != nil {
176+ err := rc . CancelFunc (ctx , cancel )
154177 if err != nil {
155178 cancel ()
156179 _ = cmd .Wait ()
0 commit comments