Load tasks from positional arguments
This commit is contained in:
parent
01b832f614
commit
d02aca6864
19
src/db.rs
19
src/db.rs
|
@ -4,7 +4,7 @@ use std::os::unix::fs::MetadataExt;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum DbError {
|
pub enum Error {
|
||||||
EntryNotFound(String)
|
EntryNotFound(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +31,23 @@ impl Entry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Load single entry from folder
|
||||||
|
pub fn entry(basedir: &str, filter: impl Fn(&Path) -> bool, name: &str) -> Option<Entry> {
|
||||||
|
let basepath = PathBuf::from(&basedir);
|
||||||
|
let path = basepath.clone().join(name);
|
||||||
|
|
||||||
|
if !path.exists() || !filter(&path) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Entry::new(
|
||||||
|
path.clone(),
|
||||||
|
path.file_name().expect("Failed to read file name").to_os_string(),
|
||||||
|
basepath
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads entire database from folder
|
||||||
pub fn from(path_name: &str, filter: impl Fn(&Path) -> bool) -> Result<Vec<Entry>, std::io::Error> {
|
pub fn from(path_name: &str, filter: impl Fn(&Path) -> bool) -> Result<Vec<Entry>, std::io::Error> {
|
||||||
let path = PathBuf::from(&path_name);
|
let path = PathBuf::from(&path_name);
|
||||||
|
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -14,18 +14,14 @@ fn main() -> Result<(), std::io::Error> {
|
||||||
let home_dir = env::var("HOME").expect("$HOME not defined. WTF?");
|
let home_dir = env::var("HOME").expect("$HOME not defined. WTF?");
|
||||||
let base_dir = format!("{}/.git-build", home_dir);
|
let base_dir = format!("{}/.git-build", home_dir);
|
||||||
|
|
||||||
//let entries = db::Database::from(&base_dir, is_executable)?;
|
|
||||||
let tasks = task::from_dir(&base_dir).expect("Could not load DB");
|
|
||||||
|
|
||||||
let cmd = cli::Cli::from_args();
|
let cmd = cli::Cli::from_args();
|
||||||
|
|
||||||
/*
|
|
||||||
let tasks = if cmd.tasks.is_empty() {
|
let tasks = if cmd.tasks.is_empty() {
|
||||||
db.tasks()
|
task::from_dir(&base_dir).expect("Could not load DB")
|
||||||
} else {
|
} else {
|
||||||
db.tasks_from(cmd.tasks).expect("Could not process the lsit of tasks given")
|
task::from_dir_and_list(&base_dir, cmd.tasks).expect("Could not load given tasks")
|
||||||
};
|
};
|
||||||
*/
|
|
||||||
for (task_name, task) in tasks.iter() {
|
for (task_name, task) in tasks.iter() {
|
||||||
let mut context = HashMap::new();
|
let mut context = HashMap::new();
|
||||||
//context.insert("$i18n_task", task_name.to_str().expect("WTF"));
|
//context.insert("$i18n_task", task_name.to_str().expect("WTF"));
|
||||||
|
|
20
src/task.rs
20
src/task.rs
|
@ -55,8 +55,28 @@ pub fn from_entries(db: Vec<Entry>) -> HashMap<String, Task> {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a hashmap of tasks, or std::io::Error
|
||||||
|
/// Reads all entries in a directory
|
||||||
pub fn from_dir(base_dir: &str) -> Result<HashMap<String, Task>, std::io::Error> {
|
pub fn from_dir(base_dir: &str) -> Result<HashMap<String, Task>, std::io::Error> {
|
||||||
Ok(from_entries(
|
Ok(from_entries(
|
||||||
db::from(base_dir, is_executable)?
|
db::from(base_dir, is_executable)?
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a hashmap of tasks, or std::io::Error
|
||||||
|
/// Reads entries in a given list from a directory, fails if a requested entry doesn't exist
|
||||||
|
/// (does not load the whole folder)
|
||||||
|
pub fn from_dir_and_list(basedir: &str, list: Vec<String>) -> Result<HashMap<String, Task>, db::Error> {
|
||||||
|
let mut entries: HashMap<String, Task> = HashMap::new();
|
||||||
|
for item in list {
|
||||||
|
if let Some(entry) = db::entry(&basedir, is_executable, &item) {
|
||||||
|
entries.insert(item.clone(), Task::from_entry(&entry));
|
||||||
|
} else {
|
||||||
|
return Err(db::Error::EntryNotFound(item.clone()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(
|
||||||
|
entries
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue