Merge branch 'master' of gitea.tforgione.fr:tforgione/gclone

This commit is contained in:
2019-02-12 10:45:39 +01:00
6 changed files with 212 additions and 16 deletions

View File

@@ -1,5 +0,0 @@
use gclone::Cache;
fn main() {
let cache = Cache::read();
}

View File

@@ -6,7 +6,7 @@ use std::path::PathBuf;
use colored::*;
use gclone::{git_dir, Cache};
use gclone::{git_dir, Cache, Result};
macro_rules! unwrap {
($e: expr) => {
@@ -55,9 +55,9 @@ fn help() {
}
fn main() {
fn main() -> Result<()> {
let git_dir = git_dir();
let git_dir = git_dir()?;
// Parse args
let url = match env::args().nth(1) {
@@ -72,7 +72,7 @@ fn main() {
};
if url == "-h" || url == "--help" {
return help();
return Ok(help());
}
let (server, owner, repo) = match parse_url(&url) {
@@ -137,7 +137,7 @@ fn main() {
eprintln!("{} {}", "info:".bold(), "done!");
// Append to cache
let mut cache = Cache::read();
let mut cache = Cache::read()?;
cache.append(path.display().to_string());
match cache.write() {
@@ -150,4 +150,6 @@ fn main() {
},
}
Ok(())
}

View File

@@ -6,6 +6,7 @@ use std::fs::File;
use std::io::{self, Write, BufRead, BufReader};
use colored::*;
use walkdir::WalkDir;
macro_rules! impl_from_error {
($type: ty, $variant: path, $from: ty) => {
@@ -43,11 +44,9 @@ impl fmt::Display for MError {
}
}
impl Error for MError {
impl Error for MError {}
}
type Result<T> = result::Result<T, MError>;
pub type Result<T> = result::Result<T, MError>;
pub fn git_dir() -> Result<String> {
Ok(env::var("GCLONE_PATH")?)
@@ -56,6 +55,16 @@ pub fn git_dir() -> Result<String> {
pub struct Cache(Vec<String>);
impl Cache {
pub fn new() -> Result<Cache> {
Ok(Cache(WalkDir::new(git_dir()?)
.max_depth(3)
.into_iter()
.filter_map(|x| x.ok())
.map(|x| x.path().display().to_string())
.collect()))
}
pub fn read() -> Result<Cache> {
let mut path = PathBuf::from(git_dir()?);
@@ -91,6 +100,18 @@ impl Cache {
Ok(())
}
pub fn find(&self, dirname: &str) -> Vec<String> {
let dirname = &format!("/{}", dirname);
let mut matches = vec![];
for line in &self.0 {
if line.ends_with(dirname) {
matches.push(line.clone());
}
}
matches
}
pub fn append(&mut self, value: String) {
self.0.push(value);
}

125
src/pgd.rs Normal file
View File

@@ -0,0 +1,125 @@
use std::env;
use std::error::Error;
use std::process::exit;
use std::io::{stdout, stdin, Write};
use std::num::Wrapping;
use colored::*;
use gclone::Cache;
fn flush_stdout() {
stdout().flush().ok();
}
fn main() {
let request = match env::args().nth(1) {
Some(arg) => arg,
None => {
eprintln!("{} {}",
"error:".bold().red(),
"cdg expects an argument".bold());
exit(1);
},
};
main_with_cache(&request, true);
}
fn main_with_cache(request: &str, regen: bool) {
let cache = Cache::read();
let matches = cache.find(&request);
match matches.len() {
0 => {
if regen {
eprintln!("{} {}",
"warning:".bold().yellow(),
"directory not found, regenerating cache...".bold());
let cache = Cache::new();
match cache.write() {
Ok(_) => (),
Err(e) => {
eprintln!("{} {} {}",
"warning:".bold().yellow(),
"couldn't write cache:".bold(),
e.description());
},
}
return main_with_cache(request, false);
} else {
eprintln!("{} {}",
"error:".bold().red(),
"no such directory".bold());
exit(1);
}
},
1 => {
println!("{}", matches[0]);
},
len => {
eprintln!("{}", "info: multiple entries found, please select one".bold());
for (i, j) in matches.iter().enumerate() {
eprintln!("{} {}",
&format!("[{}]", i + 1).bold().blue(),
&format!("{}", j).yellow());
}
eprint!("{}", "Enter your choice: ".bold());
flush_stdout();
let mut string = String::new();
match stdin().read_line(&mut string) {
Ok(_) => (),
Err(e) => {
eprintln!("{} {} {}",
"error:".bold().red(),
"couldn't read line:".bold(),
e.description());
exit(1);
},
};
// Pop the trailing \n of the string
string.pop();
// Use wrapping to move 0 to std::usize::MAX which is ok.
let value = (Wrapping(match string.parse::<usize>() {
Ok(v) => v,
Err(e) => {
eprintln!("{} {} {}",
"error:".bold().red(),
"couldn't parse integer:".bold(),
e.description());
exit(1);
}
}) - Wrapping(1)).0;
if value >= len {
eprintln!("{} {}",
"error:".bold().red(),
"integer wasn't in the right range".bold());
exit(1);
}
println!("{}", matches[value]);
},
}
}