toru/src/main.rs

174 lines
6.4 KiB
Rust
Raw Normal View History

2022-08-24 10:38:47 +00:00
mod vcs;
2022-08-21 00:59:09 +00:00
mod edit;
mod args;
2022-08-20 04:01:43 +00:00
mod vault;
2022-08-25 00:44:22 +00:00
mod index;
2022-08-20 04:01:43 +00:00
mod error;
mod tasks;
mod state;
mod graph;
mod stats;
2022-08-20 04:01:43 +00:00
mod config;
mod format;
2022-08-20 04:01:43 +00:00
use args::*;
2022-08-20 04:01:43 +00:00
fn main() {
let result = program();
match result {
Ok(()) => {
std::process::exit(0);
},
Err(err) => {
println!("{}", err);
std::process::exit(1);
2022-08-20 04:01:43 +00:00
}
}
}
fn program() -> Result<(), error::Error> {
let command = Args::accept_command();
2022-08-20 04:01:43 +00:00
let mut config = config::Config::load()?;
if let Command::Vault(command) = command {
match command {
VaultCommand::New { name, path } => {
vault::new(name.clone(), path, &mut config)?;
println!("Created vault {}", format::vault(&name));
},
VaultCommand::Disconnect { name } => {
vault::disconnect(&name, &mut config)?;
println!("Disconnected vault {}", format::vault(&name));
},
VaultCommand::Connect { name , path } => {
vault::connect(name.clone(), path, &mut config)?;
println!("Connected vault {}", format::vault(&name));
},
VaultCommand::Delete { name } => {
vault::delete(&name, &mut config)?;
println!("Deleted vault {}", format::vault(&name));
},
VaultCommand::List => {
config.list_vaults()?;
},
VaultCommand::Rename { old_name, new_name } => {
2022-08-23 11:05:28 +00:00
config.rename_vault(&old_name, new_name.clone())?;
println!("Renamed vault {} to {}", format::vault(&old_name), format::vault(&new_name));
2022-08-23 11:05:28 +00:00
}
}
}
else if let Command::Config(command) = command {
use ConfigCommand::*;
match command {
Editor { editor } => {
match editor {
Some(editor) => {
config.editor = editor;
println!("Updated editor command to: {}", config.editor);
},
None => {
println!("Current editor command: {}", config.editor);
2022-08-21 07:34:44 +00:00
}
}
}
}
}
else if let Command::Switch { name } = command {
config.switch(&name)?;
println!("Switched to vault {}", format::vault(&name));
}
else if let Command::Git { args } = command {
let vault_folder = &config.current_vault()?.1;
2022-08-24 10:38:47 +00:00
vcs::command(args, vcs::Vcs::Git, vault_folder)?;
}
2022-08-30 22:45:37 +00:00
else if let Command::Svn { args } = command {
let vault_folder = &config.current_vault()?.1;
vcs::command(args, vcs::Vcs::Svn, vault_folder)?;
}
else if command == Command::GitIgnore {
2022-08-22 08:51:54 +00:00
let vault_folder = &config.current_vault()?.1;
2022-08-24 10:38:47 +00:00
vcs::create_gitignore(vault_folder)?;
println!("Default {} file created", format::file(".gitignore"));
2022-08-22 08:51:54 +00:00
}
2022-08-30 22:45:37 +00:00
else if command == Command::SvnIgnore {
2022-08-24 10:38:47 +00:00
let vault_folder = &config.current_vault()?.1;
2022-08-30 22:45:37 +00:00
vcs::set_svn_ignore(vault_folder)?;
println!("Default svn:ignore property set");
2022-08-24 10:38:47 +00:00
}
// Commands that require loading in the state.
else {
let vault_folder = &config.current_vault()?.1;
let mut state = state::State::load(vault_folder)?;
match command {
Command::New { name, info, tag, dependency, priority, due } => {
let task = tasks::Task::new(name, info, tag, dependency, priority, due, vault_folder, &mut state)?;
println!("Created task {} (ID: {})", format::task(&task.data.name), format::id(task.data.id));
},
Command::Delete { id_or_name } => {
2022-08-25 00:44:22 +00:00
let id = state.data.index.lookup(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, false)?;
let name = task.data.name.clone();
2022-08-25 00:44:22 +00:00
state.data.index.remove(task.data.name.clone(), task.data.id);
state.data.deps.remove_node(task.data.id);
task.delete()?;
println!("Deleted task {} (ID: {})", format::task(&name), format::id(id));
},
Command::View { id_or_name } => {
2022-08-25 00:44:22 +00:00
let id = state.data.index.lookup(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, true)?;
task.display(vault_folder, &state)?;
},
Command::Edit { id_or_name, info } => {
2022-08-25 00:44:22 +00:00
let id = state.data.index.lookup(&id_or_name)?;
if info {
edit::edit_info(id, vault_folder.clone(), &config.editor)?;
}
else {
edit::edit_raw(id, vault_folder.clone(), &config.editor, &mut state)?;
}
println!("Updated task {}", format::id(id));
},
Command::Track { id_or_name, hours, minutes, date, message } => {
2022-08-25 00:44:22 +00:00
let id = state.data.index.lookup(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
let entry = tasks::TimeEntry::new(hours, minutes, date, message);
task.data.time_entries.push(entry);
task.save()?;
},
Command::Stats(command) => {
use StatsCommand::*;
match command {
Tracked { days } => {
stats::time_per_tag(days, vault_folder)?;
2022-08-24 23:15:15 +00:00
},
Completed { days } => {
stats::completed_tasks(days, vault_folder)?;
}
}
},
Command::Complete { id_or_name } => {
2022-08-25 00:44:22 +00:00
let id = state.data.index.lookup(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
task.data.completed = Some(chrono::Local::now().naive_local());
task.save()?;
println!("Marked task {} as complete", format::id(id));
},
Command::List { options } => {
2022-08-29 07:34:22 +00:00
tasks::list(options, vault_folder, &state)?;
},
// All commands which are dealt with in if let chain at start.
2022-08-30 22:45:37 +00:00
Command::Vault(_) | Command::Config(_) | Command::Git { args : _ } | Command::Svn { args : _ } | Command::Switch { name : _ } | Command::GitIgnore | Command::SvnIgnore => unreachable!(),
2022-08-20 04:01:43 +00:00
}
state.save()?;
2022-08-20 04:01:43 +00:00
}
config.save()?;
Ok(())
}