Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
    println!("Installing project tools...");

    let install_args: Vec<Vec<&str>> = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.44"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.1"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.9.3"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.5"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        // These packages are located in this repository
        vec!["--path", "mdbook-exerciser", "--locked"],
        vec!["--path", "mdbook-course", "--locked"],
    ];

    for args in &install_args {
        let status = Command::new(env!("CARGO"))
            .arg("install")
            .args(args)
            .status()
            .expect("Failed to execute cargo install");

        if !status.success() {
            let error_message = format!(
                "cargo install {} {} exited with status code: {}",
                args.get(0).unwrap(),
                args.get(1).unwrap(),
                status.code().unwrap()
            );
            return Err(Box::from(error_message));
        }
    }

    Ok(())
}

fn get_help_string(task: Option<&str>) -> String {
    if let Some(t) = task {
        format!(
            "Unrecognized task '{t}'. Available tasks:

install-tools            Installs the tools the project depends on.
"
        )
    } else {
        "Missing task. To execute a task run `cargo xtask [task]`.".to_string()
    }
}


Review comment (suggestion):
for better error handling this could print the whole command with e.g. `args.join(" ")` instead of printing the first two args. This avoids hiding the rest in which an error could be lurking.

Code after:
    println!("Installing project tools...");

    let install_args: Vec<Vec<&str>> = vec![
        // The --locked flag is important for reproducible builds. It also
        // avoids breakage due to skews between mdbook and mdbook-svgbob.
        vec!["mdbook", "--locked", "--version", "0.4.44"],
        vec!["mdbook-svgbob", "--locked", "--version", "0.2.1"],
        vec!["mdbook-pandoc", "--locked", "--version", "0.9.3"],
        vec!["mdbook-i18n-helpers", "--locked", "--version", "0.3.5"],
        vec!["i18n-report", "--locked", "--version", "0.2.0"],
        // These packages are located in this repository
        vec!["--path", "mdbook-exerciser", "--locked"],
        vec!["--path", "mdbook-course", "--locked"],
    ];

    for args in &install_args {
        let status = Command::new(env!("CARGO"))
            .arg("install")
            .args(args)
            .status()
            .expect("Failed to execute cargo install");

        if !status.success() {
            let error_message = format!(
                "Command 'cargo install {}' exited with status code: {}",
                args.join(" "),
                status.code().unwrap()
            );
            return Err(Box::from(error_message));
        }
    }

    Ok(())
}

fn get_help_string(task: Option<&str>) -> String {
    if let Some(t) = task {
        format!(
            "Unrecognized task '{t}'. Available tasks:

install-tools            Installs the tools the project depends on."
        )
    } else {
        "Missing task. To execute a task run `cargo xtask [task]`.".to_string()
    }
}