matklad / xshell

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to make `cmd` prompt the user

azzamsa opened this issue · comments

Hi.

I would like to make podman system prune prompt [y/N] to user as it is executed in shell.

Shell:

~
❯ podman system prune
WARNING! This command removes:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache

Are you sure you want to continue? [y/N]

Xshell:

❯ clean
🧽 Cleaning dagling images
$ podman system prune
WARNING! This command removes:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache

Are you sure you want to continue? [y/N] Error: EOF
Error: command exited with non-zero code `podman system prune`: 125
    println!("🧽 Cleaning dagling images");
    cmd!(sh, "podman system prune").run()?;

One option is to use yes-or-no function such https://github.com/matklad/config/blob/0b4e7f1f31678a5f7aee27d212f0832baed722b2/xtool/src/amend.rs#L14, but it doesn't solve the original problem. It will not work with the command that has no -y or --assumeyes flag.

Thanks for xshell ❤️

podman system prune -f

podman system prune -f

I know about --force flag. I use it just to demonstrate. This doesn't answer my original question.

If you want to inherit stdin, that doesn't seem possible at the moment:

xshell/src/lib.rs

Lines 1011 to 1014 in ca9a6d6

command.stdin(match &self.data.stdin_contents {
Some(_) => Stdio::piped(),
None => Stdio::null(),
});
.

You have read the answer yourself, then pass it using https://docs.rs/xshell/latest/xshell/struct.Cmd.html#method.stdin.

Yeah. We can use stdin() such as this https://github.com/matklad/config/blob/0b4e7f1f31678a5f7aee27d212f0832baed722b2/xtool/src/autowatch.rs#L29. If the answer is hard coded / static / certain.

What I want is that I want to react based on the situation, it could be yes, or it could be no.

This is my issue:

    cmd!(sh, "flatpak update").run()?;

Because my internet is metered, I want to pass y or n based on how big the update candidate is.

One solution is to list the update candidate and then use yes-or-no function to prompt the user. However, flatpak doesn't have such command. All it has is --assumeyes https://docs.flatpak.org/en/latest/flatpak-command-reference.html#flatpak-update.

What's wrong with

if yes_or_no("Update?") {
    cmd.stdin("y");
} else {
    cmd.stdin("n");
}

?

EDIT: okay, you want to see the output of the command before responding. Yeah, that's not possible at this moment, and is probably out of scope, similar to #43.

is probably out of scope, similar to #43.

Oh, I see.

  • xshell
#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! duct = "0.13.6"
//! xshell = "0.2"
//! anyhow = "1.0"
//! ```
use xshell::{cmd, Shell};
// use duct::cmd;

fn run() -> anyhow::Result<()> {
    let sh = Shell::new()?;
    cmd!(sh, "podman system prune").run()?;
    // cmd!("podman", "system", "prune").run()?;

    Ok(())
}

fn main() -> anyhow::Result<()> {
    run()?;
    println!("✨ You have a new shiny machine!");

    Ok(())
}
~
❯ cclean
$ podman system prune
WARNING! This command removes:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache

Are you sure you want to continue? [y/N] Error: EOF
Error: command exited with non-zero code `podman system prune`: 125
  • duct
#!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! duct = "0.13.6"
//! xshell = "0.2"
//! anyhow = "1.0"
//! ```
// use xshell::{cmd, Shell};
use duct::cmd;

fn run() -> anyhow::Result<()> {
    // let sh = Shell::new()?;
    // cmd!(sh, "podman system prune").run()?;
    cmd!("podman", "system", "prune").run()?;

    Ok(())
}

fn main() -> anyhow::Result<()> {
    run()?;
    println!("✨ You have a new shiny machine!");

    Ok(())
}
❯ cclean
WARNING! This command removes:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all dangling build cache

Are you sure you want to continue? [y/N] n
✨ You have a new shiny machine!

Oh, I see. duct will pipe correctly and act no different than normal shell.

I will probably just use duct.
Thanks a lot for help ❤️ @lnicola