diff --git a/src/args/mod.rs b/src/args/mod.rs
index 24a0fea..a2eefe7 100644
--- a/src/args/mod.rs
+++ b/src/args/mod.rs
@@ -32,6 +32,10 @@ pub enum Command {
CreateAccount(String),
/// Remove the account with the specified name
RemoveAccount(String),
+ /// Add a storage for the account
+ AddStorage(String, String),
+ /// Remove a storage in the account
+ RemoveStorage(String, String),
}
/// Parse the command line, returning the necessary command.
@@ -72,6 +76,21 @@ pub fn parse() -> Command {
("fetch", _) => Command::Fetch(account.into()),
("create", _) => Command::CreateAccount(account.into()),
("remove", _) => Command::RemoveAccount(account.into()),
+ ("storage", Some(arguments)) => {
+ if let Some(_type) = arguments.value_of("type") {
+ match arguments.subcommand() {
+ ("add", _) => {
+ Command::AddStorage(account.into(), _type.into())
+ }
+ ("remove", _) => {
+ Command::RemoveStorage(account.into(), _type.into())
+ }
+ _ => Command::Unknown,
+ }
+ } else {
+ Command::Unknown
+ }
+ }
_ => Command::Unknown,
}
} else {
diff --git a/src/config/config.rs b/src/config/config.rs
index 8214b1b..cbfb63b 100644
--- a/src/config/config.rs
+++ b/src/config/config.rs
@@ -74,7 +74,6 @@ impl Config {
}
/// Add a new account to the configuration file
- // TODO Result for account already exists?
pub fn add_account(&mut self, name: &str, configuration: Data) {
let account_data = AccountConfig {
favourite: None,
@@ -84,6 +83,11 @@ impl Config {
self.0.insert(name.into(), account_data);
}
+ /// Remove account
+ pub fn remove_account(&mut self, name: &str) {
+ self.0.remove(name);
+ }
+
/// Save the current configuration file.
pub fn save(&self) -> Result<(), ConfigError> {
let content = toml::to_string(&self.0)?;
diff --git a/src/config/mod.rs b/src/config/mod.rs
index b67f40c..b6ac00c 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -16,123 +16,5 @@
along with this program. If not, see .
*/
-use std::collections::HashMap;
-use std::fs::File;
-use std::io::prelude::*;
-
-use elefren::Data;
-use serde_derive::Deserialize;
-use serde_derive::Serialize;
-
pub mod config;
pub mod errors;
-
-#[derive(Serialize, Deserialize, Debug)]
-pub struct JoplinConfig {
- pub port: u32,
- pub folder: String,
- pub token: String,
-}
-
-#[derive(Serialize, Deserialize, Debug)]
-pub struct OrgConfig {
- pub location: String,
-}
-
-#[derive(Serialize, Deserialize, Debug)]
-pub struct Favourite {
- pub last: String,
-}
-
-impl Favourite {
- pub fn new() -> Self {
- Self { last: "".into() }
- }
-
- pub fn new_with_value(new_last: &str) -> Self {
- Self {
- last: new_last.into(),
- }
- }
-}
-
-#[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,
- pub org: Option,
-}
-
-/// Errors while loading the configuration file
-#[derive(Debug)]
-pub enum ConfigError {
- /// There is no configuration file
- NoConfig,
- /// The configuration file format is invalid
- InvalidConfig,
-}
-
-impl From for ConfigError {
- fn from(e: toml::de::Error) -> Self {
- // Since we only have one single error so far...
- log::debug!("Toml error: {:?}", e);
- ConfigError::InvalidConfig
- }
-}
-
-impl From for ConfigError {
- fn from(_: std::io::Error) -> Self {
- // This is not optimal: Some IO errors we can't recover (like
- // PermissionDenied)
- ConfigError::NoConfig
- }
-}
-
-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();
- Ok(toml::from_str(&contents)?)
- }
-
- pub fn save(self, most_recent_favourite: &str) -> Self {
- let new_configuration = Self {
- favourite: Favourite::new_with_value(most_recent_favourite),
- ..self
- };
- let content = toml::to_string(&new_configuration).unwrap();
-
- if let Ok(mut fp) = File::create("downfav.toml") {
- fp.write_all(content.as_bytes()).unwrap();
- }
- new_configuration
- }
-}
-
-impl From for AccountConfig {
- fn from(data: elefren::data::Data) -> Self {
- Self {
- favourite: Favourite::new(),
- mastodon: data,
- joplin: None,
- org: None,
- }
- }
-}
diff --git a/src/main.rs b/src/main.rs
index 3ff60bb..17724d2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -21,25 +21,26 @@ use std::io;
use elefren::helpers::cli;
use elefren::prelude::*;
-use crate::filesystem::storage::Filesystem;
-use crate::storage::data::Data;
-use crate::storage::joplin::Joplin;
-use crate::storage::org::Org;
-use crate::storage::storage::Storage;
-
mod args;
mod config;
-mod filesystem;
-mod storage;
+// mod filesystem;
+// mod storage;
fn main() {
env_logger::init();
match args::parse() {
- args::Command::FetchAll => fetch_favourites(),
+ args::Command::FetchAll => fetch_all_favourites(),
+ args::Command::Fetch(account_name) => fetch_favourites(&account_name),
args::Command::CreateAccount(account_name) => {
add_account(&account_name)
}
+ args::Command::RemoveAccount(account_name) => {
+ remove_account(&account_name)
+ }
+ args::Command::AddStorage(account_name, storage_type) => {
+ add_storage(&account_name, &storage_type)
+ }
_ => println!("Unknown command"),
}
}
@@ -52,55 +53,90 @@ fn add_account(name: &str) {
config.save().unwrap();
}
-/// Retrieve favourites
-fn fetch_favourites() {
- let config = match config::AccountConfig::get() {
- Ok(config) => config,
- Err(e) => {
- log::debug!("Configuration error: {:?}", e);
- let data = connect_to_mastodon();
- config::AccountConfig::from(data)
- }
- };
-
- let top = config.favourite.last.to_string();
- log::debug!("Last favourite seen: {}", top);
- let storage: Box = match (&config.joplin, &config.org) {
- (Some(joplin), _) => Box::new(Joplin::new_from_config(&joplin)),
- (None, Some(org)) => Box::new(Org::new_from_config(&org)),
- (None, None) => Box::new(Filesystem::new()),
- };
-
- let client = Mastodon::from(config.mastodon.clone());
- let most_recent_favourite = client
- .favourites()
- .unwrap()
- .items_iter()
- .take_while(|record| {
- println!("Current ID: {} (last favourite: {})", record.id, top);
- record.id != top
- })
- .map(|record| {
- log::debug!("Incoming record: {:?}", record);
- let conversion = Data::from(&record);
- log::debug!("Converted record: {:?}", conversion);
- storage.save(&conversion);
- record
- })
- .fold(None, {
- |first, current| -> Option {
- if first.is_some() {
- first
- } else {
- Some(current.id)
- }
- }
- });
-
- if let Some(new_favourite) = most_recent_favourite {
- config.save(&new_favourite);
+/// Remove account
+fn remove_account(name: &str) {
+ let mut config = config::config::Config::open().unwrap();
+ config.remove_account(name);
+ config.save().unwrap();
+}
+
+/// Add a storage for an account
+fn add_storage(account: &str, storage: &str) {
+ log::debug!("Adding storage \"{}\" for account \"{}\"", account, storage);
+ match storage {
+ "filesystem" => add_filesystem(account),
+ _ => println!("Storage unknown"),
}
}
+
+fn add_filesystem(account: &str) {
+ println!("Path for the files: ");
+ let mut path = String::new();
+ io::stdin()
+ .read_line(&mut path)
+ .expect("You need to enter yoru server URL");
+}
+
+/// Fetch from all accounts
+fn fetch_all_favourites() {
+ // let mut config = config::config::Config::open().unwrap();
+}
+
+/// Fetch the favourites from a single account
+fn fetch_favourites(_account: &str) {
+ //
+}
+
+// Retrieve favourites
+// fn fetch_favourites() {
+// let config = match config::AccountConfig::get() {
+// Ok(config) => config,
+// Err(e) => {
+// log::debug!("Configuration error: {:?}", e);
+// let data = connect_to_mastodon();
+// config::AccountConfig::from(data)
+// }
+// };
+
+// let top = config.favourite.last.to_string();
+// log::debug!("Last favourite seen: {}", top);
+// let storage: Box = match (&config.joplin, &config.org) {
+// (Some(joplin), _) => Box::new(Joplin::new_from_config(&joplin)),
+// (None, Some(org)) => Box::new(Org::new_from_config(&org)),
+// (None, None) => Box::new(Filesystem::new()),
+// };
+
+// let client = Mastodon::from(config.mastodon.clone());
+// let most_recent_favourite = client
+// .favourites()
+// .unwrap()
+// .items_iter()
+// .take_while(|record| {
+// println!("Current ID: {} (last favourite: {})", record.id, top);
+// record.id != top
+// })
+// .map(|record| {
+// log::debug!("Incoming record: {:?}", record);
+// let conversion = Data::from(&record);
+// log::debug!("Converted record: {:?}", conversion);
+// storage.save(&conversion);
+// record
+// })
+// .fold(None, {
+// |first, current| -> Option {
+// if first.is_some() {
+// first
+// } else {
+// Some(current.id)
+// }
+// }
+// });
+
+// if let Some(new_favourite) = most_recent_favourite {
+// config.save(&new_favourite);
+// }
+// }
+
/// Create a connection to a mastodon server.
fn connect_to_mastodon() -> elefren::data::Data {
println!("Your server URL: ");