From e07eef82db17a17853c980c185445cc808405f18 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 4 Jan 2025 18:08:46 +0100 Subject: [PATCH] Move session handling to seperate module --- src/lib.rs | 1 + src/main.rs | 93 ++----------------------------------------------- src/session.rs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 90 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/session.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f52f1c4 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod session; diff --git a/src/main.rs b/src/main.rs index eb4c5a1..1767761 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use anyhow::{ - bail, Context, Result, }; @@ -7,20 +6,10 @@ use clap::{ Parser, Subcommand, }; -use etebase::{ - Account, - Client, +use etecal::session::{ + store_etebase_session, + restore_etebase_session, }; -use std::fs::{ - File, -}; -use std::io::{ - Write, -}; -use std::path::{ - PathBuf, -}; - #[derive(Subcommand, Clone)] #[command(arg_required_else_help = true)] @@ -46,82 +35,6 @@ struct Cli { config: Option, } -fn get_data_dir() -> Result { - if let Ok(xdg_data_home) = std::env::var("XDG_DATA_HOME") { - let mut p = PathBuf::from(xdg_data_home); - p.push("etecal"); - return Ok(p); - } - - if let Ok(home) = std::env::var("HOME") { - let mut p = PathBuf::from(home); - p.push(".local/share/etecal"); - return Ok(p); - } - - bail!("Failed to determine data directory"); -} - -fn get_url_file_path() -> Result { - let data_dir = get_data_dir() - .context("Failed to find data dir")?; - - let mut url_file_path = PathBuf::from(data_dir.clone()); - url_file_path.push("url"); - - return Ok(url_file_path); -} - -fn get_url() -> Result { - let url = std::fs::read_to_string(get_url_file_path()?) - .context("Failed to restore API url")?; - - return Ok(url); -} - -fn get_stored_session_file_path() -> Result { - let data_dir = get_data_dir() - .context("Failed to find data dir")?; - - let mut stored_session_file_path = PathBuf::from(data_dir.clone()); - stored_session_file_path.push("session"); - - return Ok(stored_session_file_path); -} - -fn get_stored_session() -> Result { - let stored_session = std::fs::read_to_string(get_stored_session_file_path()?) - .context("Failed to read restored session")?; - - return Ok(stored_session); -} - -fn restore_etebase_session() -> Result { - let client = Client::new("etecal", &get_url()?) - .context("Failed to init API client")?; - - let etebase = Account::restore(client, &get_stored_session()?, None)?; - - return Ok(etebase); -} - -fn store_etebase_session(url: &str, user: &str, pass: &str) -> Result { - let client = Client::new("etecal", url) - .context("Failed to init API client")?; - - let etebase = Account::login(client, user, pass)?; - - std::fs::create_dir_all(get_data_dir()?)?; - - File::create(get_url_file_path()?)?.write_all(url.as_bytes())?; - - let stored_session = etebase.save(None)?; - - File::create(get_stored_session_file_path()?)?.write_all(stored_session.as_bytes())?; - - return Ok(etebase); -} - fn main() -> Result<()> { let cli = Cli::parse(); diff --git a/src/session.rs b/src/session.rs new file mode 100644 index 0000000..73e934a --- /dev/null +++ b/src/session.rs @@ -0,0 +1,94 @@ +use anyhow::{ + bail, + Context, + Result, +}; +use etebase::{ + Account, + Client, +}; +use std::fs::{ + File, +}; +use std::io::{ + Write, +}; +use std::path::{ + PathBuf, +}; + +fn get_data_dir() -> Result { + if let Ok(xdg_data_home) = std::env::var("XDG_DATA_HOME") { + let mut p = PathBuf::from(xdg_data_home); + p.push("etecal"); + return Ok(p); + } + + if let Ok(home) = std::env::var("HOME") { + let mut p = PathBuf::from(home); + p.push(".local/share/etecal"); + return Ok(p); + } + + bail!("Failed to determine data directory"); +} + +fn get_url_file_path() -> Result { + let data_dir = get_data_dir() + .context("Failed to find data dir")?; + + let mut url_file_path = PathBuf::from(data_dir.clone()); + url_file_path.push("url"); + + return Ok(url_file_path); +} + +fn get_url() -> Result { + let url = std::fs::read_to_string(get_url_file_path()?) + .context("Failed to restore API url")?; + + return Ok(url); +} + +fn get_stored_session_file_path() -> Result { + let data_dir = get_data_dir() + .context("Failed to find data dir")?; + + let mut stored_session_file_path = PathBuf::from(data_dir.clone()); + stored_session_file_path.push("session"); + + return Ok(stored_session_file_path); +} + +fn get_stored_session() -> Result { + let stored_session = std::fs::read_to_string(get_stored_session_file_path()?) + .context("Failed to read restored session")?; + + return Ok(stored_session); +} + +pub fn restore_etebase_session() -> Result { + let client = Client::new("etecal", &get_url()?) + .context("Failed to init API client")?; + + let etebase = Account::restore(client, &get_stored_session()?, None)?; + + return Ok(etebase); +} + +pub fn store_etebase_session(url: &str, user: &str, pass: &str) -> Result { + let client = Client::new("etecal", url) + .context("Failed to init API client")?; + + let etebase = Account::login(client, user, pass)?; + + std::fs::create_dir_all(get_data_dir()?)?; + + File::create(get_url_file_path()?)?.write_all(url.as_bytes())?; + + let stored_session = etebase.save(None)?; + + File::create(get_stored_session_file_path()?)?.write_all(stored_session.as_bytes())?; + + return Ok(etebase); +}