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

Method ExecArgs

shell/exec.go:159–235  ·  view source on GitHub ↗

ExecArgs processes the args to given exec command, handling all of the input / output redirection and file globbing, homedir expansion, etc.

(cmdIO *exec.CmdIO, errOk bool, cmd any, args ...any)

Source from the content-addressed store, hash-verified

157// handling all of the input / output redirection and
158// file globbing, homedir expansion, etc.
159func (sh *Shell) ExecArgs(cmdIO *exec.CmdIO, errOk bool, cmd any, args ...any) (*sshclient.Client, string, []string) {
160 if len(sh.Jobs) > 0 {
161 jb := sh.Jobs.Peek()
162 if jb.OutIsPipe() {
163 cmdIO.PushIn(jb.PipeIn.Peek())
164 }
165 }
166 scmd := reflectx.ToString(cmd)
167 cl := sh.ActiveSSH()
168 isCmd := sh.isCommand.Peek()
169 sargs := make([]string, 0, len(args))
170 var err error
171 for _, a := range args {
172 s := reflectx.ToString(a)
173 if s == "" {
174 continue
175 }
176 if cl == nil {
177 s, err = homedir.Expand(s)
178 sh.HandleArgErr(errOk, err)
179 // note: handling globbing in a later pass, to not clutter..
180 } else {
181 if s[0] == '~' {
182 s = "$HOME/" + s[1:]
183 }
184 }
185 sargs = append(sargs, s)
186 }
187 if scmd[0] == '@' {
188 newHost := ""
189 if scmd == "@0" { // local
190 cl = nil
191 } else {
192 hnm := scmd[1:]
193 if scl, ok := sh.SSHClients[hnm]; ok {
194 newHost = hnm
195 cl = scl
196 } else {
197 sh.HandleArgErr(errOk, fmt.Errorf("cosh: ssh connection named: %q not found", hnm))
198 }
199 }
200 if len(sargs) > 0 {
201 scmd = sargs[0]
202 sargs = sargs[1:]
203 } else { // just a ssh switch
204 sh.SSHActive = newHost
205 return nil, "", nil
206 }
207 }
208 for i := 0; i < len(sargs); i++ { // we modify so no range
209 s := sargs[i]
210 switch {
211 case s[0] == '>':
212 sargs = sh.OutToFile(cl, cmdIO, errOk, sargs, i)
213 case s[0] == '|':
214 sargs = sh.OutToPipe(cl, cmdIO, errOk, sargs, i)
215 case cl == nil && isCmd && strings.HasPrefix(s, "args"):
216 sargs = sh.CmdArgs(errOk, sargs, i)

Callers 1

ExecMethod · 0.95

Calls 10

ActiveSSHMethod · 0.95
HandleArgErrMethod · 0.95
OutToFileMethod · 0.95
OutToPipeMethod · 0.95
CmdArgsMethod · 0.95
ToStringFunction · 0.92
PeekMethod · 0.80
OutIsPipeMethod · 0.80
PushInMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected