gclone/src/gclone.rs

54 lines
1.1 KiB
Rust

use std::path::PathBuf;
use std::process::exit;
use colored::*;
use gclone::git::{clone, parse_url, GCLONE_PATH};
use gclone::{first_arg, Cache, Result};
fn help() {
print!(include_str!("../assets/help.txt"));
}
fn main_result() -> Result<()> {
// Parse args
let url = first_arg()?;
if url == "-h" || url == "--help" {
return Ok(help());
}
let url = if url.ends_with(".git") {
&url[0..url.len() - 4]
} else {
&url
};
let (server, owner, repo) = parse_url(&url)?;
// Build path
let mut path = PathBuf::from(&*GCLONE_PATH);
path.push(&server);
path.push(&owner);
path.push(&repo);
// Clone repository
eprintln!("{} {} {}{}", "info:".bold(), "cloning", url, "...");
clone(&url, &path.display().to_string())?;
eprintln!("{} {}", "info:".bold(), "done!");
// Append to cache
let mut cache = Cache::read()?;
cache.append(path.display().to_string());
cache.write()?;
Ok(())
}
fn main() {
if let Err(e) = main_result() {
eprintln!("{}", e);
exit(1);
}
}