MCPcopy Create free account
hub / github.com/cogentcore/core / OutToFile

Method OutToFile

shell/exec.go:238–291  ·  view source on GitHub ↗

OutToFile processes the > arg that sends output to a file

(cl *sshclient.Client, cmdIO *exec.CmdIO, errOk bool, sargs []string, i int)

Source from the content-addressed store, hash-verified

236
237// OutToFile processes the > arg that sends output to a file
238func (sh *Shell) OutToFile(cl *sshclient.Client, cmdIO *exec.CmdIO, errOk bool, sargs []string, i int) []string {
239 n := len(sargs)
240 s := sargs[i]
241 sn := len(s)
242 fn := ""
243 narg := 1
244 if i < n-1 {
245 fn = sargs[i+1]
246 narg = 2
247 }
248 appn := false
249 errf := false
250 switch {
251 case sn > 1 && s[1] == '>':
252 appn = true
253 if sn > 2 && s[2] == '&' {
254 errf = true
255 }
256 case sn > 1 && s[1] == '&':
257 errf = true
258 case sn > 1:
259 fn = s[1:]
260 narg = 1
261 }
262 if fn == "" {
263 sh.HandleArgErr(errOk, fmt.Errorf("cosh: no output file specified"))
264 return sargs
265 }
266 if cl != nil {
267 if !strings.HasPrefix(fn, "@0:") {
268 return sargs
269 }
270 fn = fn[3:]
271 }
272 sargs = slices.Delete(sargs, i, i+narg)
273 // todo: process @n: expressions here -- if @0 then it is the same
274 // if @1, then need to launch an ssh "cat >[>] file" with pipe from command as stdin
275 var f *os.File
276 var err error
277 if appn {
278 f, err = os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
279 } else {
280 f, err = os.Create(fn)
281 }
282 if err == nil {
283 cmdIO.PushOut(f)
284 if errf {
285 cmdIO.PushErr(f)
286 }
287 } else {
288 sh.HandleArgErr(errOk, err)
289 }
290 return sargs
291}
292
293// OutToPipe processes the | arg that sends output to a pipe
294func (sh *Shell) OutToPipe(cl *sshclient.Client, cmdIO *exec.CmdIO, errOk bool, sargs []string, i int) []string {

Callers 1

ExecArgsMethod · 0.95

Calls 7

HandleArgErrMethod · 0.95
CreateMethod · 0.80
PushOutMethod · 0.80
PushErrMethod · 0.80
ErrorfMethod · 0.65
DeleteMethod · 0.65
OpenFileMethod · 0.45

Tested by

no test coverage detected