(outputFile string, commands ...PipeCommand)
| 156 | } |
| 157 | |
| 158 | func (c *CommandHelper) RunPipeToFile(outputFile string, commands ...PipeCommand) (string, error) { |
| 159 | if len(commands) == 0 { |
| 160 | return "", nil |
| 161 | } |
| 162 | |
| 163 | ctx, cancel, cmds := c.preparePipeCommands(commands) |
| 164 | if cancel != nil { |
| 165 | defer cancel() |
| 166 | } |
| 167 | |
| 168 | file, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm) |
| 169 | if err != nil { |
| 170 | return "", err |
| 171 | } |
| 172 | defer func() { _ = file.Close() }() |
| 173 | |
| 174 | stderr := &lockedBuffer{limit: maxStreamOutputCapture} |
| 175 | if err := connectPipeCommands(cmds, file, stderr, stderr); err != nil { |
| 176 | return "", err |
| 177 | } |
| 178 | if err := startPipeCommands(cmds); err != nil { |
| 179 | return handleErrString("", stderr.String(), c.IgnoreExist1, err) |
| 180 | } |
| 181 | |
| 182 | runErr := c.pipeResultErr(ctx, waitPipeCommands(ctx, cmds)) |
| 183 | if runErr != nil { |
| 184 | return handleErrString("", stderr.String(), c.IgnoreExist1, runErr) |
| 185 | } |
| 186 | return "", nil |
| 187 | } |
| 188 | |
| 189 | func (c *CommandHelper) preparePipeCommands(commands []PipeCommand) (context.Context, context.CancelFunc, []*exec.Cmd) { |
| 190 | ctx, cancel := c.pipeContext() |
no test coverage detected