gclone/src/gclone.rs

58 lines
1.1 KiB
Rust
Raw Normal View History

2019-02-27 10:57:01 +01:00
#[macro_use]
extern crate log;
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
2020-02-18 14:57:02 +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-27 10:57:01 +01:00
// Init beautylog
beautylog::init(log::LevelFilter::Info).ok();
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") {
2020-02-18 14:57:02 +01:00
&url[0..url.len() - 4]
2019-02-25 10:21:35 +01:00
} 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-27 10:57:01 +01:00
info!("{} {}{}", "cloning", url, "...");
clone(&url, &path)?;
info!("{}", "done!");
2019-02-11 17:33:37 +01:00
2019-02-11 17:56:43 +01:00
// Append to cache
2020-10-26 17:58:20 +01:00
let mut cache = Cache::read_or_generate();
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() {
2019-02-27 10:57:01 +01:00
error!("{}", e);
2019-02-12 14:51:45 +01:00
exit(1);
}
}