kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

Request for an API that do not exit when executable to run is not found #90

Open Frizlab opened 4 years ago

Frizlab commented 4 years ago

Specifically I’m launching a process in a server (yes, I know, I shouldn’t!), and it’s a pity having a server being killed like that, even in the case of a misconfiguration.

I’ve seen the launchThrowably() method on the Process extension; I was thinking of something along these lines but for runAsync (e.g. runAsyncThrowably?)

kareman commented 4 years ago

Yes this is something that should be better supported in SwiftShell. It might be better to have all the run*-commands throw errors if they can't be executed.

For now you can verify the command is available first:

guard SwiftShell.run("which","wrongcommand").succeeded else {
    // handle error
}
let c  = runAsync("wrongcommand")

Or you can let bash handle it:

let c  = runAsync(bash: "wrongcommand")
try c.finish()

Then the error will be thrown when you call finish().

Frizlab commented 4 years ago

Another workaround, I think, would be to call runAsync("/usr/bin/env", "wrongcommand"). The wrongcommand can even be a full path, it seems to work. There are few OSes where /usr/bin/env does not exist AFAICT.