diff --git a/src/args/mod.rs b/src/args/mod.rs index 5767a24..3b48699 100644 --- a/src/args/mod.rs +++ b/src/args/mod.rs @@ -37,7 +37,7 @@ pub fn parse() -> Result { .arg( Arg::with_name("account") .help("Account alias") - .required(true), + .required(false), ) .subcommand(SubCommand::with_name("create").about("Create the account")) .subcommand(SubCommand::with_name("remove").about("Remove the account")) @@ -85,23 +85,26 @@ pub fn parse() -> Result { ); let matches = parser.get_matches(); - let account_name = matches.value_of("account").unwrap(); // we can unwrap 'cause it is required - match matches.subcommand() { - ("create", _) => Ok(Command::add_account(account_name.into())), - ("remove", _) => Ok(Command::remove_account(account_name.into())), - ("storage", Some(args)) => match args.subcommand() { - ("add", Some(add_args)) => { - let storage = add_args - .subcommand_name() - .ok_or(ParsingError::UnknownCommand)?; - log::debug!("Storage: {:?}", storage); - Ok(Command::add_storage( - account_name.into(), - StorageType::try_from(storage)?, - )) - } - _ => unimplemented!(), - }, - _ => Err(ParsingError::UnknownCommand), + if let Some(account_name) = matches.value_of("account") { + match matches.subcommand() { + ("create", _) => Ok(Command::add_account(account_name.into())), + ("remove", _) => Ok(Command::remove_account(account_name.into())), + ("storage", Some(args)) => match args.subcommand() { + ("add", Some(add_args)) => { + let storage = add_args + .subcommand_name() + .ok_or(ParsingError::UnknownCommand)?; + log::debug!("Storage: {:?}", storage); + Ok(Command::add_storage( + account_name.into(), + StorageType::try_from(storage)?, + )) + } + _ => unimplemented!(), + }, + _ => Err(ParsingError::UnknownCommand), + } + } else { + Ok(Command::fetch_all()) } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3338cc4..2c0748e 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -67,6 +67,9 @@ pub enum Command { /// Add a storage in an account AddStorage(String, StorageType), + + /// Fetch favourites from all accounts + FetchAll, } impl Command { @@ -82,6 +85,10 @@ impl Command { Command::AddStorage(account.into(), storage) } + pub fn fetch_all() -> Self { + Command::FetchAll + } + /// Execute the command, based on its value pub fn execute(&self) -> CommandResult { match self { @@ -90,6 +97,7 @@ impl Command { Command::AddStorage(account, storage) => { add_storage(account, storage) } + Command::FetchAll => fetch_all(), } } } @@ -132,3 +140,17 @@ fn add_storage(account: &str, storage: &StorageType) -> CommandResult { config.save()?; Ok(()) } + +fn fetch_all() -> CommandResult { + let mut config = Config::open()?; + for (name, account_config) in config { + log::debug!("Fetching new items from {:?}", name); + if let Some(markdown_config) = account_config.markdown { + log::debug!( + "Markdown set to download to {:?}", + markdown_config.path + ); + } + } + Ok(()) +} diff --git a/src/config/config.rs b/src/config/config.rs index e36339e..1530032 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -37,10 +37,10 @@ struct Favourite { /// Account configuration #[derive(Serialize, Deserialize, Debug)] -struct AccountConfig { +pub struct AccountConfig { favourite: Option, mastodon: Data, - markdown: Option, + pub markdown: Option, // joplin: Option, // org: Option, } @@ -61,7 +61,7 @@ impl Config { /// Open the configuration file; if it doesn't exist, returns an empty set. pub fn open() -> Result { let filename = Config::filename()?; - log::debug!("Trying to open file \"{:?}\"", filename); + log::debug!("Trying to open file {:?}", filename); match File::open(filename) { Ok(mut fp) => { let mut contents = String::new(); @@ -104,9 +104,20 @@ impl Config { pub fn save(&self) -> Result<(), ConfigError> { let content = toml::to_string(&self.0)?; let filename = Config::filename()?; - log::debug!("Saving configuration to file \"{:?}\"", filename); + log::debug!("Saving configuration to file {:?}", filename); let mut fp = File::create(filename)?; fp.write_all(content.as_bytes())?; Ok(()) } } + +/// Produce an iterator for all the accounts in the configuration. +impl IntoIterator for Config { + type Item = (String, AccountConfig); + type IntoIter = + as IntoIterator>::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 30ba57a..787863a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -20,7 +20,6 @@ pub mod config; pub mod errors; use self::errors::ConfigError; - pub trait Configurable { fn config() -> Result where