(outputFile string, commands ...PipeCommand)
| 177 | } |
| 178 | |
| 179 | func (c *CommandHelper) RunPipeToFile(outputFile string, commands ...PipeCommand) (string, error) { |
| 180 | if len(commands) == 0 { |
| 181 | return "", nil |
| 182 | } |
| 183 | |
| 184 | ctx, cancel, cmds := c.preparePipeCommands(commands) |
| 185 | if cancel != nil { |
| 186 | defer cancel() |
| 187 | } |
| 188 | |
| 189 | file, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm) |
| 190 | if err != nil { |
| 191 | return "", err |
| 192 | } |
| 193 | defer func() { _ = file.Close() }() |
| 194 | |
| 195 | stderr := &lockedBuffer{limit: maxStreamOutputCapture} |
| 196 | if err := connectPipeCommands(cmds, file, stderr, stderr); err != nil { |
| 197 | return "", err |
| 198 | } |
| 199 | if err := startPipeCommands(cmds); err != nil { |
| 200 | return handleErrString("", stderr.String(), c.IgnoreExist1, err) |
| 201 | } |
| 202 | |
| 203 | runErr := c.pipeResultErr(ctx, waitPipeCommands(ctx, cmds)) |
| 204 | if runErr != nil { |
| 205 | return handleErrString("", stderr.String(), c.IgnoreExist1, runErr) |
| 206 | } |
| 207 | return "", nil |
| 208 | } |
| 209 | |
| 210 | func (c *CommandHelper) preparePipeCommands(commands []PipeCommand) (context.Context, context.CancelFunc, []*exec.Cmd) { |
| 211 | ctx, cancel := c.pipeContext() |
no test coverage detected