openssh-rust / openssh

Scriptable SSH through OpenSSH in Rust
Apache License 2.0
232 stars 35 forks source link

Add `.with_args` feature to rust library #148

Open ActuallyHappening opened 2 months ago

ActuallyHappening commented 2 months ago

This features exists in bossy::Command here: https://docs.rs/bossy/latest/bossy/struct.Command.html#method.with_args

It is quite useful, allowing you to write this:

let kill_cmd = session.command("/root/.cargo/bin/nu").withargs([
  "-c",
  r##"ps | filter {|ps| $ps.name == "surreal"} | get pid | each {|pid| kill $pid }"##,
]);

... instead of this:

let mut kill_cmd = session.command("/root/.cargo/bin/nu");
kill_cmd.args([
  "-c",
  r##"ps | filter {|ps| $ps.name == "surreal"} | get pid | each {|pid| kill $pid }"##,
]);

This saves using the mut keyword and a new line, and can even be implemented trivially using an extension trait

NobodyXu commented 2 months ago

That does sound nice to have!

We can also have a more general

fn with_command(self, f: impl FnOnce(&mut Self)) -> Self;

I would accept a PR for this.

mingmamma commented 1 month ago

@NobodyXu I am checking out the codebase and have some basic understanding. The suggestion seems like a interesting addition of API. If were to implement the function signature you suggested, based on my understanding that would be an addition of something like

// in src/session.rs, I presumed?
// presuming Self == openssh::Session?
pub fn with_command(self, f: impl FnOnce(&mut Self)) -> Self { todo!() }

Could you give a concrete example of what would be an instance of function/closure as the argument of the type: f: impl FnOnce(&mut Self) to call the new API with?

I just started playing with the codebase and don't see what such instance would be like (presumably a block of code involving use of some of the existing APIs to do some business, but I just don't visualise how that might be the case)

NobodyXu commented 1 month ago

@mingmamma the function will be added to openssh::OwningCommand, which has many functions taking a mutable reference, such as arg, stdin, stdout.