gclone/src/gclone.rs

54 lines
1.1 KiB
Rust
Raw Normal View History

2019-02-11 17:33:37 +01:00
use std::path::PathBuf;
2019-02-21 15:21:27 +01:00
use std::process::exit;
2019-02-11 17:33:37 +01:00
use colored::*;
2019-02-21 15:21:27 +01:00
use gclone::git::{clone, parse_url, GCLONE_PATH};
2019-02-12 14:51:45 +01:00
use gclone::{first_arg, Cache, Result};
2019-02-11 17:33:37 +01:00
2019-02-25 10:21:35 +01:00
fn help() {
print!(include_str!("../assets/help.txt"));
}
2019-02-11 17:33:37 +01:00
2019-02-12 14:51:45 +01:00
fn main_result() -> Result<()> {
2019-02-11 17:33:37 +01:00
// Parse args
2019-02-12 14:51:45 +01:00
let url = first_arg()?;
2019-02-11 17:33:37 +01:00
if url == "-h" || url == "--help" {
return Ok(help());
2019-02-11 17:33:37 +01:00
}
2019-02-25 10:21:35 +01:00
let url = if url.ends_with(".git") {
&url[0..url.len() - 4]
} else {
&url
};
2019-02-12 14:51:45 +01:00
let (server, owner, repo) = parse_url(&url)?;
2019-02-11 17:33:37 +01:00
// Build path
2019-02-12 11:11:32 +01:00
let mut path = PathBuf::from(&*GCLONE_PATH);
2019-02-11 17:33:37 +01:00
path.push(&server);
path.push(&owner);
path.push(&repo);
2019-02-12 14:51:45 +01:00
// Clone repository
2019-02-11 17:33:37 +01:00
eprintln!("{} {} {}{}", "info:".bold(), "cloning", url, "...");
2019-02-12 14:51:45 +01:00
clone(&url, &path.display().to_string())?;
2019-02-11 17:33:37 +01:00
eprintln!("{} {}", "info:".bold(), "done!");
2019-02-11 17:56:43 +01:00
// Append to cache
let mut cache = Cache::read()?;
2019-02-11 17:56:43 +01:00
cache.append(path.display().to_string());
2019-02-12 14:51:45 +01:00
cache.write()?;
2019-02-11 17:56:43 +01:00
Ok(())
2019-02-11 17:33:37 +01:00
}
2019-02-12 14:51:45 +01:00
fn main() {
if let Err(e) = main_result() {
eprintln!("{}", e);
exit(1);
}
}