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,8 +135,7 @@ 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 } => {
@ -158,13 +157,9 @@ fn program() -> Result<(), error::Error> {
List => { List => {
config.list_vaults()?; config.list_vaults()?;
}, },
Switch { name } => {
config.switch(&name)?;
println!("Switched to vault {}", colour::vault(&name));
},
} }
}, }
Config(command) => { else if let Config(command) = command {
use ConfigCommand::*; use ConfigCommand::*;
match command { match command {
Editor { editor } => { Editor { editor } => {
@ -179,8 +174,17 @@ fn program() -> Result<(), error::Error> {
} }
} }
} }
}, }
command => { 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 vault_folder = &config.current_vault()?.1;
let mut state = state::State::load(vault_folder)?; let mut state = state::State::load(vault_folder)?;
@ -227,18 +231,15 @@ fn program() -> Result<(), error::Error> {
task.save()?; task.save()?;
println!("Marked task {} as complete", colour::id(&id.to_string())); println!("Marked task {} as complete", colour::id(&id.to_string()));
}, },
Git { args } => {
git::run_command(args, vault_folder)?;
},
List {} => { List {} => {
tasks::list(vault_folder)?; tasks::list(vault_folder)?;
} },
Vault(_) | Config(_) => unreachable!(), // All commands which are dealt with in if let chain at start.
Vault(_) | Config(_) | Git { args : _ } | Switch { name : _ } => unreachable!(),
} }
state.save()?; state.save()?;
} }
}
config.save()?; config.save()?;