Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 15, 2025 12:10
Show Gist options
  • Save rust-play/0415e1b62e679d23a3faa20ba0480aee to your computer and use it in GitHub Desktop.
Save rust-play/0415e1b62e679d23a3faa20ba0480aee to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::process::Command;
fn main() {
if is_git_installed() {
println!("Git is installed. Initializing Git repository...");
if let Err(e) = initialize_git_repo() {
eprintln!("Failed to initialize Git repository: {}", e);
} else {
println!("Git repository initialized successfully.");
}
} else {
println!("Git is not installed. Skipping Git initialization.");
}
}
fn is_git_installed() -> bool {
Command::new("git")
.arg("--version")
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
fn initialize_git_repo() -> std::io::Result<()> {
let status = Command::new("git").arg("init").status()?;
if status.success() {
Ok(())
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Git init failed with status: {}", status),
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment