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

Add check_command_available functionality for basic-cli #223

Open
lukewilliamboswell opened this issue Jul 3, 2024 · 0 comments
Open

Add check_command_available functionality for basic-cli #223

lukewilliamboswell opened this issue Jul 3, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@lukewilliamboswell
Copy link
Collaborator

Here is an example rust implementation from roc-lang/roc/crates/utils/command/src/lib.rs.

This will be useful for scripts to check a command exists before trying to run it.

fn check_command_available(command_name: &str) -> bool {
    if cfg!(target_family = "unix") {
        let unparsed_path = match std::env::var("PATH") {
            Ok(var) => var,
            Err(VarError::NotPresent) => return false,
            Err(VarError::NotUnicode(_)) => {
                panic!("found PATH, but it included invalid unicode data!")
            }
        };

        std::env::split_paths(&unparsed_path).any(|dir| dir.join(command_name).exists())
    } else if cfg!(target = "windows") {
        let mut cmd = Command::new("Get-Command");

        cmd.args([command_name]);

        let cmd_str = format!("{cmd:?}");

        let cmd_out = cmd.output().unwrap_or_else(|err| {
            panic!(
                "Failed to execute `{cmd_str}` to check if {command_name} is available:\n    {err}"
            )
        });

        cmd_out.status.success()
    } else {
        // We're in uncharted waters, best not to panic if
        // things may end up working out down the line.
        true
    }
}
@lukewilliamboswell lukewilliamboswell added the good first issue Good for newcomers label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant