diff --git a/src/config/mod.rs b/src/config/mod.rs index bf52902..ba4f523 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -16,6 +16,7 @@ along with this program. If not, see . */ +use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; @@ -54,6 +55,21 @@ impl Favourite { #[derive(Serialize, Deserialize, Debug)] pub struct Config { + accounts: HashMap, +} + +impl Config { + pub fn add_account(name: &str, configuration: elefren::data::Data) { + let config = AccountConfig::from(configuration); + let mut accounts: HashMap = HashMap::new(); + accounts.insert(name.into(), config); + let content = toml::to_string(&accounts).unwrap(); + log::debug!("{}", content); + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct AccountConfig { pub favourite: Favourite, pub mastodon: Data, pub joplin: Option, @@ -85,8 +101,8 @@ impl From for ConfigError { } } -impl Config { - pub fn get() -> Result { +impl AccountConfig { + pub fn get() -> Result { let mut fp = File::open("downfav.toml")?; let mut contents = String::new(); fp.read_to_string(&mut contents).unwrap(); @@ -105,11 +121,14 @@ impl Config { } new_configuration } + + /// Add an account to the configuration + pub fn add_account() {} } -impl From for Config { +impl From for AccountConfig { fn from(data: elefren::data::Data) -> Self { - Config { + Self { favourite: Favourite::new(), mastodon: data, joplin: None, diff --git a/src/main.rs b/src/main.rs index 67c5a79..fb3f705 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,18 +36,27 @@ fn main() { match args::parse() { args::Command::Fetch => fetch_favourites(), + args::Command::AddAccount(account_name) => add_account(&account_name), _ => println!("Unknown command"), } } +/// Create a new account +fn add_account(name: &str) { + log::debug!("Creating account {}", name); + println!("Enter information for account \"{}\":", name); + let data = connect_to_mastodon(); + config::Config::add_account(name, data) +} + /// Retrieve favourites fn fetch_favourites() { - let config = match config::Config::get() { + let config = match config::AccountConfig::get() { Ok(config) => config, Err(e) => { log::debug!("Configuration error: {:?}", e); let data = connect_to_mastodon(); - config::Config::from(data) + config::AccountConfig::from(data) } };