Skip to content

Commit 7ff0250

Browse files
committed
improve error handling
1 parent 5953546 commit 7ff0250

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

check_diff/src/main.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use clap::Parser;
22
use std::env;
3+
use std::io::{self, Write};
4+
use std::path::Path;
35
use std::process::Command;
46

57
/// Inputs for the check_diff script
@@ -18,22 +20,36 @@ struct CliInputs {
1820
rustfmt_config: Option<Vec<String>>,
1921
}
2022

21-
fn clone_git_repo(url: String, dest: String) {
23+
/// Clone a git repository and cd into it.
24+
///
25+
/// Parameters:
26+
/// url: git clone url
27+
/// dest: directory where the repo should be cloned
28+
fn clone_git_repo(url: &str, dest: &str) {
2229
env::set_var("GIT_TERMINAL_PROMPT", "0");
23-
let mut git_cmd = Command::new("git");
24-
git_cmd.args(["clone", "--quiet", url, "--depth", "1", "dest"]);
25-
git_cmd.output().expect("failed to clone repository");
26-
let mut enter_dest_dir = Command::new("cd")
27-
.arg(dest)
30+
let git_cmd = Command::new("git")
31+
.args(["clone", "--quiet", url, "--depth", "1", dest])
2832
.output()
29-
.expect("failed to enter directory.");
30-
}
33+
.expect("failed to clone repository");
34+
// if the git command does not return successfully,
35+
// cd command will not work as well. So fail fast.
36+
if !git_cmd.status.success() {
37+
io::stdout().write_all(&git_cmd.stdout).unwrap();
38+
io::stderr().write_all(&git_cmd.stderr).unwrap();
39+
return;
40+
}
41+
println!("Successfully clone repository. Entering repository");
3142

32-
fn main() {
33-
let args = CliInputs::parse();
43+
let dest_path = Path::new(&dest);
44+
let _ = env::set_current_dir(&dest_path).is_ok();
3445
println!(
35-
"remote_repo_url: {:?}, feature_branch: {:?},
36-
optional_commit_hash: {:?}, optional_rustfmt_config: {:?}",
37-
args.remote_repo_url, args.feature_branch, args.commit_hash, args.rustfmt_config
46+
"Current directory: {}",
47+
env::current_dir().unwrap().display()
3848
);
3949
}
50+
51+
fn main() {
52+
let _args = CliInputs::parse();
53+
let sample_repo = "https://github.com/rust-lang/rustfmt.git";
54+
clone_git_repo(sample_repo, "./tmp/");
55+
}

0 commit comments

Comments
 (0)