From 7ff0250a46dcf38d1fd2fdb7951e808846331980 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Thu, 6 Jun 2024 21:46:50 +0800 Subject: [PATCH] improve error handling --- check_diff/src/main.rs | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/check_diff/src/main.rs b/check_diff/src/main.rs index 4d0197f2a96..0f64b6216f2 100644 --- a/check_diff/src/main.rs +++ b/check_diff/src/main.rs @@ -1,5 +1,7 @@ use clap::Parser; use std::env; +use std::io::{self, Write}; +use std::path::Path; use std::process::Command; /// Inputs for the check_diff script @@ -18,22 +20,36 @@ struct CliInputs { rustfmt_config: Option>, } -fn clone_git_repo(url: String, dest: String) { +/// Clone a git repository and cd into it. +/// +/// Parameters: +/// url: git clone url +/// dest: directory where the repo should be cloned +fn clone_git_repo(url: &str, dest: &str) { env::set_var("GIT_TERMINAL_PROMPT", "0"); - let mut git_cmd = Command::new("git"); - git_cmd.args(["clone", "--quiet", url, "--depth", "1", "dest"]); - git_cmd.output().expect("failed to clone repository"); - let mut enter_dest_dir = Command::new("cd") - .arg(dest) + let git_cmd = Command::new("git") + .args(["clone", "--quiet", url, "--depth", "1", dest]) .output() - .expect("failed to enter directory."); -} + .expect("failed to clone repository"); + // if the git command does not return successfully, + // cd command will not work as well. So fail fast. + if !git_cmd.status.success() { + io::stdout().write_all(&git_cmd.stdout).unwrap(); + io::stderr().write_all(&git_cmd.stderr).unwrap(); + return; + } + println!("Successfully clone repository. Entering repository"); -fn main() { - let args = CliInputs::parse(); + let dest_path = Path::new(&dest); + let _ = env::set_current_dir(&dest_path).is_ok(); println!( - "remote_repo_url: {:?}, feature_branch: {:?}, - optional_commit_hash: {:?}, optional_rustfmt_config: {:?}", - args.remote_repo_url, args.feature_branch, args.commit_hash, args.rustfmt_config + "Current directory: {}", + env::current_dir().unwrap().display() ); } + +fn main() { + let _args = CliInputs::parse(); + let sample_repo = "https://github.com/rust-lang/rustfmt.git"; + clone_git_repo(sample_repo, "./tmp/"); +}