Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to passthrough stdout/stdin for External Tasks #106

Open
joshuawright11 opened this issue Sep 21, 2021 · 1 comment
Open

Comments

@joshuawright11
Copy link

I'd like to run an external task from my command that may ask the user for input.

What's the best way of passing through requests for input (I assume from the external task's stdout) to the user, and also passing the user's input to the external tasks stdin?

A very simple example:

I'd like to run sudo echo hello as an external task and have the user be notified when sudo asks for their password. How could I accomplish that with the Task API?

@joshuawright11
Copy link
Author

joshuawright11 commented Sep 21, 2021

So far what I've got is

@main struct Run {
    static func main() {
        let cli = CLI(singleCommand: MyCommand())
        cli.go()
    }
}

final class MyCommand: Command {
    var name: String = "something"

    func execute() throws {
        Task(executable: "sudo", arguments: ["-S", "echo", "hello"], stdout: stdout, stderr: stderr).runSync()
    }
}

Which results in prompting the user for their password, but I can't figure out how to pass input to the command through to the Task.

I also see there is a Task.execvp which causes input to go to the subcommand, but it seems to mean that the rest of the current command won't be executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant