Browse Source

Exposing the markdown configuration to download to it

master
Julio Biason 3 years ago
parent
commit
39267a461c
  1. 41
      src/args/mod.rs
  2. 22
      src/commands/mod.rs
  3. 19
      src/config/config.rs
  4. 1
      src/config/mod.rs

41
src/args/mod.rs

@ -37,7 +37,7 @@ pub fn parse() -> Result<Command, ParsingError> {
.arg( .arg(
Arg::with_name("account") Arg::with_name("account")
.help("Account alias") .help("Account alias")
.required(true), .required(false),
) )
.subcommand(SubCommand::with_name("create").about("Create the account")) .subcommand(SubCommand::with_name("create").about("Create the account"))
.subcommand(SubCommand::with_name("remove").about("Remove the account")) .subcommand(SubCommand::with_name("remove").about("Remove the account"))
@ -85,23 +85,26 @@ pub fn parse() -> Result<Command, ParsingError> {
); );
let matches = parser.get_matches(); let matches = parser.get_matches();
let account_name = matches.value_of("account").unwrap(); // we can unwrap 'cause it is required if let Some(account_name) = matches.value_of("account") {
match matches.subcommand() { match matches.subcommand() {
("create", _) => Ok(Command::add_account(account_name.into())), ("create", _) => Ok(Command::add_account(account_name.into())),
("remove", _) => Ok(Command::remove_account(account_name.into())), ("remove", _) => Ok(Command::remove_account(account_name.into())),
("storage", Some(args)) => match args.subcommand() { ("storage", Some(args)) => match args.subcommand() {
("add", Some(add_args)) => { ("add", Some(add_args)) => {
let storage = add_args let storage = add_args
.subcommand_name() .subcommand_name()
.ok_or(ParsingError::UnknownCommand)?; .ok_or(ParsingError::UnknownCommand)?;
log::debug!("Storage: {:?}", storage); log::debug!("Storage: {:?}", storage);
Ok(Command::add_storage( Ok(Command::add_storage(
account_name.into(), account_name.into(),
StorageType::try_from(storage)?, StorageType::try_from(storage)?,
)) ))
} }
_ => unimplemented!(), _ => unimplemented!(),
}, },
_ => Err(ParsingError::UnknownCommand), _ => Err(ParsingError::UnknownCommand),
}
} else {
Ok(Command::fetch_all())
} }
} }

22
src/commands/mod.rs

@ -67,6 +67,9 @@ pub enum Command {
/// Add a storage in an account /// Add a storage in an account
AddStorage(String, StorageType), AddStorage(String, StorageType),
/// Fetch favourites from all accounts
FetchAll,
} }
impl Command { impl Command {
@ -82,6 +85,10 @@ impl Command {
Command::AddStorage(account.into(), storage) Command::AddStorage(account.into(), storage)
} }
pub fn fetch_all() -> Self {
Command::FetchAll
}
/// Execute the command, based on its value /// Execute the command, based on its value
pub fn execute(&self) -> CommandResult { pub fn execute(&self) -> CommandResult {
match self { match self {
@ -90,6 +97,7 @@ impl Command {
Command::AddStorage(account, storage) => { Command::AddStorage(account, storage) => {
add_storage(account, storage) add_storage(account, storage)
} }
Command::FetchAll => fetch_all(),
} }
} }
} }
@ -132,3 +140,17 @@ fn add_storage(account: &str, storage: &StorageType) -> CommandResult {
config.save()?; config.save()?;
Ok(()) 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(())
}

19
src/config/config.rs

@ -37,10 +37,10 @@ struct Favourite {
/// Account configuration /// Account configuration
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct AccountConfig { pub struct AccountConfig {
favourite: Option<Favourite>, favourite: Option<Favourite>,
mastodon: Data, mastodon: Data,
markdown: Option<MarkdownConfig>, pub markdown: Option<MarkdownConfig>,
// joplin: Option<JoplinConfig>, // joplin: Option<JoplinConfig>,
// org: Option<OrgConfig>, // org: Option<OrgConfig>,
} }
@ -61,7 +61,7 @@ impl Config {
/// Open the configuration file; if it doesn't exist, returns an empty set. /// Open the configuration file; if it doesn't exist, returns an empty set.
pub fn open() -> Result<Self, ConfigError> { pub fn open() -> Result<Self, ConfigError> {
let filename = Config::filename()?; let filename = Config::filename()?;
log::debug!("Trying to open file \"{:?}\"", filename); log::debug!("Trying to open file {:?}", filename);
match File::open(filename) { match File::open(filename) {
Ok(mut fp) => { Ok(mut fp) => {
let mut contents = String::new(); let mut contents = String::new();
@ -104,9 +104,20 @@ impl Config {
pub fn save(&self) -> Result<(), ConfigError> { pub fn save(&self) -> Result<(), ConfigError> {
let content = toml::to_string(&self.0)?; let content = toml::to_string(&self.0)?;
let filename = Config::filename()?; let filename = Config::filename()?;
log::debug!("Saving configuration to file \"{:?}\"", filename); log::debug!("Saving configuration to file {:?}", filename);
let mut fp = File::create(filename)?; let mut fp = File::create(filename)?;
fp.write_all(content.as_bytes())?; fp.write_all(content.as_bytes())?;
Ok(()) Ok(())
} }
} }
/// Produce an iterator for all the accounts in the configuration.
impl IntoIterator for Config {
type Item = (String, AccountConfig);
type IntoIter =
<HashMap<std::string::String, AccountConfig> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

1
src/config/mod.rs

@ -20,7 +20,6 @@ pub mod config;
pub mod errors; pub mod errors;
use self::errors::ConfigError; use self::errors::ConfigError;
pub trait Configurable { pub trait Configurable {
fn config() -> Result<Self, ConfigError> fn config() -> Result<Self, ConfigError>
where where

Loading…
Cancel
Save