Browse Source

Starting to deal with account commands

master
Julio Biason 3 years ago
parent
commit
1351050bea
  1. 27
      src/config/mod.rs
  2. 13
      src/main.rs

27
src/config/mod.rs

@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
@ -54,6 +55,21 @@ impl Favourite {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
accounts: HashMap<String, AccountConfig>,
}
impl Config {
pub fn add_account(name: &str, configuration: elefren::data::Data) {
let config = AccountConfig::from(configuration);
let mut accounts: HashMap<String, AccountConfig> = 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 favourite: Favourite,
pub mastodon: Data, pub mastodon: Data,
pub joplin: Option<JoplinConfig>, pub joplin: Option<JoplinConfig>,
@ -85,8 +101,8 @@ impl From<std::io::Error> for ConfigError {
} }
} }
impl Config { impl AccountConfig {
pub fn get() -> Result<Config, ConfigError> { pub fn get() -> Result<AccountConfig, ConfigError> {
let mut fp = File::open("downfav.toml")?; let mut fp = File::open("downfav.toml")?;
let mut contents = String::new(); let mut contents = String::new();
fp.read_to_string(&mut contents).unwrap(); fp.read_to_string(&mut contents).unwrap();
@ -105,11 +121,14 @@ impl Config {
} }
new_configuration new_configuration
} }
/// Add an account to the configuration
pub fn add_account() {}
} }
impl From<elefren::data::Data> for Config { impl From<elefren::data::Data> for AccountConfig {
fn from(data: elefren::data::Data) -> Self { fn from(data: elefren::data::Data) -> Self {
Config { Self {
favourite: Favourite::new(), favourite: Favourite::new(),
mastodon: data, mastodon: data,
joplin: None, joplin: None,

13
src/main.rs

@ -36,18 +36,27 @@ fn main() {
match args::parse() { match args::parse() {
args::Command::Fetch => fetch_favourites(), args::Command::Fetch => fetch_favourites(),
args::Command::AddAccount(account_name) => add_account(&account_name),
_ => println!("Unknown command"), _ => 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 /// Retrieve favourites
fn fetch_favourites() { fn fetch_favourites() {
let config = match config::Config::get() { let config = match config::AccountConfig::get() {
Ok(config) => config, Ok(config) => config,
Err(e) => { Err(e) => {
log::debug!("Configuration error: {:?}", e); log::debug!("Configuration error: {:?}", e);
let data = connect_to_mastodon(); let data = connect_to_mastodon();
config::Config::from(data) config::AccountConfig::from(data)
} }
}; };

Loading…
Cancel
Save