(commands ...PipeCommand)
| 113 | } |
| 114 | |
| 115 | func (c *CommandHelper) RunPipe(commands ...PipeCommand) (string, error) { |
| 116 | if len(commands) == 0 { |
| 117 | return "", nil |
| 118 | } |
| 119 | |
| 120 | ctx, cancel, cmds := c.preparePipeCommands(commands) |
| 121 | if cancel != nil { |
| 122 | defer cancel() |
| 123 | } |
| 124 | |
| 125 | customWriter := &CustomWriter{taskItem: c.taskItem} |
| 126 | var outputFile *os.File |
| 127 | stdout, stderr := &lockedBuffer{}, &lockedBuffer{} |
| 128 | limitOutputCapture := c.taskItem != nil || c.logger != nil || len(c.outputFile) != 0 |
| 129 | if limitOutputCapture { |
| 130 | stdout.limit = maxStreamOutputCapture |
| 131 | stderr.limit = maxStreamOutputCapture |
| 132 | } |
| 133 | var pipeStderr io.Writer = stderr |
| 134 | var lastStdout io.Writer = stdout |
| 135 | var lastStderr io.Writer = stderr |
| 136 | var streamWriter io.Writer |
| 137 | if c.taskItem != nil { |
| 138 | streamWriter = customWriter |
| 139 | } else if c.logger != nil { |
| 140 | streamWriter = c.logger.Writer() |
| 141 | } else if len(c.outputFile) != 0 { |
| 142 | file, err := os.OpenFile(c.outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, constant.FilePerm) |
| 143 | if err != nil { |
| 144 | return "", err |
| 145 | } |
| 146 | outputFile = file |
| 147 | lastStdout = outputFile |
| 148 | } |
| 149 | if streamWriter != nil { |
| 150 | pipeStderr = io.MultiWriter(stderr, streamWriter) |
| 151 | lastStdout = io.MultiWriter(stdout, streamWriter) |
| 152 | lastStderr = io.MultiWriter(stderr, streamWriter) |
| 153 | } |
| 154 | defer func() { |
| 155 | if c.taskItem != nil { |
| 156 | customWriter.Flush() |
| 157 | } |
| 158 | if closer, ok := streamWriter.(io.Closer); ok { |
| 159 | _ = closer.Close() |
| 160 | } |
| 161 | if outputFile != nil { |
| 162 | _ = outputFile.Close() |
| 163 | } |
| 164 | }() |
| 165 | if err := connectPipeCommands(cmds, lastStdout, lastStderr, pipeStderr); err != nil { |
| 166 | return "", err |
| 167 | } |
| 168 | if err := startPipeCommands(cmds); err != nil { |
| 169 | return handleErrString(stdout.String(), stderr.String(), c.IgnoreExist1, err) |
| 170 | } |
| 171 | |
| 172 | runErr := c.pipeResultErr(ctx, waitPipeCommands(ctx, cmds)) |
no test coverage detected