|
22 | 22 | // GlobalCommandArgs global command args for external package setting
|
23 | 23 | GlobalCommandArgs []string
|
24 | 24 |
|
25 |
| - // DefaultCommandExecutionTimeout default command execution timeout duration |
| 25 | + // defaultCommandExecutionTimeout default command execution timeout duration |
26 | 26 | defaultCommandExecutionTimeout = 360 * time.Second
|
27 | 27 | )
|
28 | 28 |
|
@@ -109,71 +109,48 @@ 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 |
| - 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 |
| 112 | + if timeout == -1 { |
| 113 | + timeout = defaultCommandExecutionTimeout |
137 | 114 | }
|
138 | 115 |
|
139 |
| - if len(rc.Dir) == 0 { |
| 116 | + if len(dir) == 0 { |
140 | 117 | log(c.String())
|
141 | 118 | } else {
|
142 |
| - log("%s: %v", rc.Dir, c) |
| 119 | + log("%s: %v", dir, c) |
143 | 120 | }
|
144 | 121 |
|
145 |
| - ctx, cancel := context.WithTimeout(c.parentContext, rc.Timeout) |
| 122 | + ctx, cancel := context.WithTimeout(c.parentContext, timeout) |
146 | 123 | defer cancel()
|
147 | 124 |
|
148 | 125 | cmd := exec.CommandContext(ctx, c.name, c.args...)
|
149 |
| - if rc.Env == nil { |
| 126 | + if env == nil { |
150 | 127 | cmd.Env = append(os.Environ(), fmt.Sprintf("LC_ALL=%s", DefaultLocale))
|
151 | 128 | } else {
|
152 |
| - cmd.Env = rc.Env |
| 129 | + cmd.Env = env |
153 | 130 | cmd.Env = append(cmd.Env, fmt.Sprintf("LC_ALL=%s", DefaultLocale))
|
154 | 131 | }
|
155 | 132 |
|
156 | 133 | // TODO: verify if this is still needed in golang 1.15
|
157 | 134 | if goVersionLessThan115 {
|
158 | 135 | cmd.Env = append(cmd.Env, "GODEBUG=asyncpreemptoff=1")
|
159 | 136 | }
|
160 |
| - cmd.Dir = rc.Dir |
161 |
| - cmd.Stdout = rc.Stdout |
162 |
| - cmd.Stderr = rc.Stderr |
163 |
| - cmd.Stdin = rc.Stdin |
| 137 | + cmd.Dir = dir |
| 138 | + cmd.Stdout = stdout |
| 139 | + cmd.Stderr = stderr |
| 140 | + cmd.Stdin = stdin |
164 | 141 | if err := cmd.Start(); err != nil {
|
165 | 142 | return err
|
166 | 143 | }
|
167 | 144 |
|
168 | 145 | desc := c.desc
|
169 | 146 | if desc == "" {
|
170 |
| - desc = fmt.Sprintf("%s %s %s [repo_path: %s]", GitExecutable, c.name, strings.Join(c.args, " "), rc.Dir) |
| 147 | + desc = fmt.Sprintf("%s %s %s [repo_path: %s]", GitExecutable, c.name, strings.Join(c.args, " "), dir) |
171 | 148 | }
|
172 | 149 | pid := process.GetManager().Add(desc, cancel)
|
173 | 150 | defer process.GetManager().Remove(pid)
|
174 | 151 |
|
175 |
| - if rc.CancelFunc != nil { |
176 |
| - err := rc.CancelFunc(ctx, cancel) |
| 152 | + if fn != nil { |
| 153 | + err := fn(ctx, cancel) |
177 | 154 | if err != nil {
|
178 | 155 | cancel()
|
179 | 156 | _ = cmd.Wait()
|
|
0 commit comments