MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / RunPipe

Method RunPipe

agent/utils/cmd/cmdx.go:115–177  ·  view source on GitHub ↗
(commands ...PipeCommand)

Source from the content-addressed store, hash-verified

113}
114
115func (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))

Callers 6

runTarGzEncryptToFileFunction · 0.45
runTarGzDecryptToDirFunction · 0.45
restoreRuleFunction · 0.45
FindChainNumFunction · 0.45
loadDiskInfoFunction · 0.45
uninstallOpenclawPluginFunction · 0.45

Calls 10

preparePipeCommandsMethod · 0.95
FlushMethod · 0.95
StringMethod · 0.95
pipeResultErrMethod · 0.95
OpenFileMethod · 0.80
connectPipeCommandsFunction · 0.70
startPipeCommandsFunction · 0.70
handleErrStringFunction · 0.70
waitPipeCommandsFunction · 0.70
CloseMethod · 0.65

Tested by

no test coverage detected