OutToFile processes the > arg that sends output to a file
(cl *sshclient.Client, cmdIO *exec.CmdIO, errOk bool, sargs []string, i int)
| 236 | |
| 237 | // OutToFile processes the > arg that sends output to a file |
| 238 | func (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 |
| 294 | func (sh *Shell) OutToPipe(cl *sshclient.Client, cmdIO *exec.CmdIO, errOk bool, sargs []string, i int) []string { |