Invoking editor from Swift in subshell

Hi, I'm working on a personal project where I need to edit some files as part of a workflow. In ruby, I was able to use the Commander library, which allows you to spawn an editor like vim, modify a file, and then continue with the modified file.

Does anyone know of anything like this for Swift? I've found SwiftShell and SwiftCLI, but they don't seem to provide this functionality.

Alternatively, does anyone have any pointers on implementing something like this in Swift using Process or posix_spawn? I'm not super familiar with the requirements around piping stdin/stdout to make this work.

So what I was actually looking for was system(). For some reason it's not available in Swift, but it's easy enough to get a C wrapper to invoke it.

For some reason it's not available in Swift

FYI, system is not available in Swift because it’s been deprecated for a long time. It’s deprecated because it’s use is strongly discouraged. Running a command via system is expensive (it’s starts up a shell) and error prone (quoting arguments, argh!). Worse yet, the sort of errors it induces are a common source of security vulnerabilities.

Now, for your use case it’s probably fine to just invoke system (it’s a standard API, so it’s not going away any time soon) but my advice is that you structure your code so that there’s no access to this feature when you deploy it to production.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple