From fa934ed4a5cf1f985a0ba6b8c296566690b9de56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= <26653921+dj8yfo@users.noreply.github.com> Date: Fri, 8 Nov 2024 04:13:18 +0200 Subject: [PATCH] chore: Gracefully handle missing `git` on `cargo near new` (#246) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit while a fuller solution might be done in scope of #238 this pr is a quicker clarification of error ![image](https://github.com/user-attachments/assets/fa31cf55-86a7-4bc4-8e3e-25926d144e31) ![image](https://github.com/user-attachments/assets/5f179aa6-e626-49de-b117-cc2dd0aca4ca) Co-authored-by: dj8yf0μl --- cargo-near/src/commands/new/mod.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/cargo-near/src/commands/new/mod.rs b/cargo-near/src/commands/new/mod.rs index 8ed794d7..1bc53f56 100644 --- a/cargo-near/src/commands/new/mod.rs +++ b/cargo-near/src/commands/new/mod.rs @@ -1,8 +1,10 @@ use std::process::Stdio; -use color_eyre::eyre::{ContextCompat, WrapErr}; - use crate::posthog_tracking; +use color_eyre::{ + eyre::{ContextCompat, WrapErr}, + Section, +}; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(input_context = near_cli_rs::GlobalContext)] @@ -61,13 +63,26 @@ impl NewContext { .spawn(posthog_tracking::track_usage) .unwrap(); - let status = std::process::Command::new("git") + let child_result = std::process::Command::new("git") .arg("init") .current_dir(project_dir) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .status()?; - if !status.success() { + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn(); + let child = match child_result { + Ok(child) => child, + Err(io_err) if io_err.kind() == std::io::ErrorKind::NotFound => { + return Err(io_err) + .wrap_err("`git` executable isn't available") + .note("Git from https://git-scm.com/ is required to be available in PATH")?; + } + Err(io_err) => { + return Err(io_err)?; + } + }; + let output = child.wait_with_output()?; + if !output.status.success() { + println!("{}", String::from_utf8_lossy(&output.stderr)); return Err(color_eyre::eyre::eyre!( "Failed to execute process: `git init`" ));