Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ flate2 = "1.0.13"
handlebars = "3.0.1"
attohttpc = "0.13"
regex = "0.1.41"
rustc-serialize = "0.3.16"
rustc-serialize = "0.3.24"
time = "0.1.33"
toml = "0.1.23"
walkdir = "=0.1.3"
term = "=0.2.12"
18 changes: 11 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn fetch_rng(from: Option<String>, to: Option<String>) -> () {
}

for date in date_strings {
fetch(date);
fetch(date)
}
}

Expand All @@ -121,7 +121,13 @@ pub fn fetch(s: String) -> () {
Some(a) => a,
None => panic!("Could not fetch archive"),
};
archive.fetch();

print!("fetch {} ... ", archive);

match archive.fetch() {
Ok(status) => println!("ok: {}", status),
Err(status) => println!("error: {}", status),
}
}

pub fn ls_data() -> () {
Expand Down Expand Up @@ -154,11 +160,9 @@ pub fn ls_data() -> () {

/// Print the standard paths that the app uses.
pub fn show_paths() -> () {
let v = vec![config_path(), data_path(), config_file_path()];

v.into_iter()
.map(|e| ::print_green(format!(" {}\n", e.to_str().unwrap()).as_ref()))
.for_each(drop);
for e in vec![config_path(), data_path(), config_file_path()].iter() {
println!("{}", e.to_string_lossy());
}
}

/// TODO: I'm not sure if this is supported anymore?
Expand Down
20 changes: 9 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ const CONFIG: &'static str = "gar.toml";
pub fn config_path() -> PathBuf {
let home = match env::var("HOME") {
Ok(v) => v,
Err(e) => {
println!("Could not get the HOME environment variable");
panic!(e);
},
Err(e) => panic!("could not get the HOME environment variable: {}", e),
};

let mut path = PathBuf::new();
path.push(home);
path.push(PREFIX);
Expand All @@ -44,7 +42,7 @@ pub fn config_file_path() -> PathBuf {
fn read_configuration_file() -> Table {
let mut f: File = match File::open(config_file_path()) {
Ok(v) => v,
Err(e) => panic!(e),
Err(e) => panic!("{}", e),
};
let mut s: String = String::new();
f.read_to_string(&mut s).unwrap();
Expand Down Expand Up @@ -84,16 +82,18 @@ pub fn data_exists(filename: &String) -> bool {
/// Default things to run each time we go through the main entry point.
pub fn init() -> () {
let cpath: PathBuf = config_path();
let cpath_str = cpath.to_string_lossy();
let dpath: PathBuf = data_path();
let dpath_str = dpath.to_string_lossy();
let config_file: PathBuf = config_file_path();

if !cpath.exists() {
println!("Config file path created for the first time");
println!("config file path created for the first time: {}", cpath_str);
fs::create_dir_all(cpath.to_str().unwrap()).unwrap();
}

if !dpath.exists() {
println!("Data path created for the first time");
println!("data path created for the first time: {}", dpath_str);
fs::create_dir_all(dpath.to_str().unwrap()).unwrap();
}

Expand All @@ -108,12 +108,10 @@ pub fn init() -> () {

let mut f: File = match File::create(config_file) {
Ok(v) => v,
Err(e) => panic!("Could not open config file for writing {}", e),
Err(e) => panic!("could not open config file for writing {}", e),
};

println!("Writing configuration for the first time");
println!("writing configuration for the first time");
f.write_all(s.as_bytes()).unwrap();
}
}


87 changes: 18 additions & 69 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! This is a small tool that helps you interface with githubarchive.org
//! It provides utilities to fetch specific archives, or fetch a range of archives via date.
//! You can then run some semi-complex queries on the downloaded archives, which are still in gz
//! format. Try running `help` to see information of each command, and subcommands.
//! This is a small tool that helps you interface with
//! githubarchive.org It provides utilities to fetch specific
//! archives, or fetch a range of archives via date. You can then run
//! some semi-complex queries on the downloaded archives, which are
//! still in gz format. Try running `help` to see information of each
//! command, and subcommands.
//!
//! ````nocode
//! gar 0.2.0
Expand Down Expand Up @@ -82,24 +84,32 @@
//!
//! Here is an example of a query:
//!
//! gar query --where language:Rust,type:create
//! ```nocode
//! gar query --where language:Rust,type:create
//! ```
//!
//! This will search for all events, and select only the events where the repository is of the Rust
//! language, and the type of event is a CreateEvent. You can also specify time constraints with to
//! and from:
//!
//! gar query --where language:Rust,type:create --from 2013-1-1-1 --to 2013-1-5-1
//! ```nocode
//! gar query --where language:Rust,type:create --from 2013-1-1-1 --to 2013-1-5-1
//! ```
//!
//! And as you noticed you can also provide a type of event, and language using the `--where` clause.
//! The way you do this, is by providing a label, delimited with a colon `:` and provide the value.
//! For example:
//!
//! language:Rust
//! ```nocode
//! language:Rust
//! ```
//!
//! Satisfies this query. You can add more constraints by delimiting them with a comma ','. The
//! relevance of a comma in this case is as if it's a logical `AND`. As you previously saw:
//!
//! language:Rust,type:create
//! ```nocode
//! language:Rust,type:create
//! ```
//!
//! Here's the list of things you can add as constraints:
//!
Expand Down Expand Up @@ -208,71 +218,10 @@ extern crate toml;
extern crate chrono;
extern crate attohttpc;
extern crate regex;
extern crate term;
extern crate flate2;
extern crate walkdir;
extern crate handlebars;

pub mod models;
pub mod config;
pub mod cli;

#[inline]
fn print_yellow(s: &str) -> () {
generic_print(s, term::color::YELLOW);
}

#[inline]
fn print_green(s: &str) -> () {
generic_print(s, term::color::GREEN);
}

#[inline]
fn print_red(s: &str) -> () {
let mut t = term::stderr().unwrap();
t.fg(term::color::RED).unwrap();
write!(t, "{}", s).unwrap();
t.reset().unwrap();
}

#[inline]
fn print_magenta(s: &str) -> () {
generic_print(s, term::color::MAGENTA);
}

#[inline]
fn generic_print(s: &str, col: term::color::Color) -> () {
let mut t = term::stdout().unwrap();
t.fg(col).unwrap();
write!(t, "{}", s).unwrap();
t.reset().unwrap();
}

fn vec_contains<T: PartialEq>(v: &Vec<T>, t: &T) -> bool {
for e in v {
if e == t { return true }
}
false
}


#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_vec_contains() -> () {
let vs: Vec<String> = vec!["potato".into(), "yotato".into(), "motato".into()];
let vu32: Vec<u32> = vec![1,2,3,4,5,6];
let vi32: Vec<i32> = vec![1,2,3,4,5];
let jon: String = "jon".into();

assert!(::vec_contains::<String>(&vs, &"potato".into()));
assert!(::vec_contains::<u32>(&vu32, &2));
assert!(::vec_contains::<i32>(&vi32, &5));

assert!(!::vec_contains::<i32>(&vi32, &122));
assert!(!::vec_contains::<u32>(&vu32, &123123));
assert!(!::vec_contains::<String>(&vs, &jon));
}
}
Loading