shortcircut loading state with git command; switch as top level command

This commit is contained in:
aaron-jack-manning 2022-08-22 18:17:37 +10:00
parent a0fd9b01d7
commit 4f44f6d894
2 changed files with 110 additions and 106 deletions

View File

@ -44,3 +44,6 @@ Run `--help` alongside any command to get details on what it does.
- Due dates - Due dates
- Taken as input when creating notes - Taken as input when creating notes
- Displayed in list view by default (with number of days remaining) - Displayed in list view by default (with number of days remaining)
- Git integration
- Command to add default gitignore file
- `clean` command to delete discarded tasks

View File

@ -75,6 +75,10 @@ enum Command {
/// Commands for interacting with vaults. /// Commands for interacting with vaults.
#[clap(subcommand)] #[clap(subcommand)]
Vault(VaultCommand), Vault(VaultCommand),
/// Switches to the specified vault.
Switch {
name : String,
},
} }
#[derive(clap::Subcommand, Debug)] #[derive(clap::Subcommand, Debug)]
@ -108,10 +112,6 @@ enum VaultCommand {
}, },
/// Lists all configured vaults. /// Lists all configured vaults.
List, List,
/// Switches to the specified vault.
Switch {
name : String,
},
} }
fn main() { fn main() {
@ -135,110 +135,111 @@ fn program() -> Result<(), error::Error> {
let mut config = config::Config::load()?; let mut config = config::Config::load()?;
use Command::*; use Command::*;
match command { if let Vault(command) = command {
Vault(command) => { use VaultCommand::*;
use VaultCommand::*; match command {
match command { New { name, path } => {
New { name, path } => { vault::new(name.clone(), path, &mut config)?;
vault::new(name.clone(), path, &mut config)?; println!("Created vault {}", colour::vault(&name));
println!("Created vault {}", colour::vault(&name)); },
}, Disconnect { name } => {
Disconnect { name } => { vault::disconnect(&name, &mut config)?;
vault::disconnect(&name, &mut config)?; println!("Disconnected vault {}", colour::vault(&name));
println!("Disconnected vault {}", colour::vault(&name)); },
}, Connect { name , path } => {
Connect { name , path } => { vault::connect(name.clone(), path, &mut config)?;
vault::connect(name.clone(), path, &mut config)?; println!("Connected vault {}", colour::vault(&name));
println!("Connected vault {}", colour::vault(&name)); },
}, Delete { name } => {
Delete { name } => { vault::delete(&name, &mut config)?;
vault::delete(&name, &mut config)?; println!("Deleted vault {}", colour::vault(&name));
println!("Deleted vault {}", colour::vault(&name)); },
}, List => {
List => { config.list_vaults()?;
config.list_vaults()?; },
},
Switch { name } => {
config.switch(&name)?;
println!("Switched to vault {}", colour::vault(&name));
},
}
},
Config(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);
}
}
}
}
},
command => {
let vault_folder = &config.current_vault()?.1;
let mut state = state::State::load(vault_folder)?;
match command {
New { name, info, tags, dependencies, priority } => {
let task = tasks::Task::new(name, info, tags, dependencies, priority, vault_folder, &mut state)?;
println!("Created task {} (ID: {})", colour::task_name(&task.data.name), colour::id(&task.data.id.to_string()));
},
Delete { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, false)?;
let name = task.data.name.clone();
state.index_remove(task.data.name.clone(), task.data.id);
task.delete()?;
println!("Deleted task {} (ID: {})", colour::task_name(&name), colour::id(&id.to_string()));
},
View { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, true)?;
task.display()?;
},
Edit { id_or_name, info } => {
let id = state.name_or_id_to_id(&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 {}", colour::id(&id.to_string()));
},
Discard { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
task.data.discarded = true;
task.save()?;
println!("Discarded task {}", colour::id(&id.to_string()));
},
Complete { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
task.data.complete = true;
task.save()?;
println!("Marked task {} as complete", colour::id(&id.to_string()));
},
Git { args } => {
git::run_command(args, vault_folder)?;
},
List {} => {
tasks::list(vault_folder)?;
}
Vault(_) | Config(_) => unreachable!(),
}
state.save()?;
} }
} }
else if let 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);
}
}
}
}
}
else if let Switch { name } = command {
config.switch(&name)?;
println!("Switched to vault {}", colour::vault(&name));
}
else if let Git { args } = command {
let vault_folder = &config.current_vault()?.1;
git::run_command(args, vault_folder)?;
}
// 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 {
New { name, info, tags, dependencies, priority } => {
let task = tasks::Task::new(name, info, tags, dependencies, priority, vault_folder, &mut state)?;
println!("Created task {} (ID: {})", colour::task_name(&task.data.name), colour::id(&task.data.id.to_string()));
},
Delete { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, false)?;
let name = task.data.name.clone();
state.index_remove(task.data.name.clone(), task.data.id);
task.delete()?;
println!("Deleted task {} (ID: {})", colour::task_name(&name), colour::id(&id.to_string()));
},
View { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let task = tasks::Task::load(id, vault_folder, true)?;
task.display()?;
},
Edit { id_or_name, info } => {
let id = state.name_or_id_to_id(&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 {}", colour::id(&id.to_string()));
},
Discard { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
task.data.discarded = true;
task.save()?;
println!("Discarded task {}", colour::id(&id.to_string()));
},
Complete { id_or_name } => {
let id = state.name_or_id_to_id(&id_or_name)?;
let mut task = tasks::Task::load(id, vault_folder, false)?;
task.data.complete = true;
task.save()?;
println!("Marked task {} as complete", colour::id(&id.to_string()));
},
List {} => {
tasks::list(vault_folder)?;
},
// All commands which are dealt with in if let chain at start.
Vault(_) | Config(_) | Git { args : _ } | Switch { name : _ } => unreachable!(),
}
state.save()?;
}
config.save()?; config.save()?;