Move session handling to seperate module

This commit is contained in:
clerie 2025-01-04 18:08:46 +01:00
parent e12108aa13
commit e07eef82db
3 changed files with 98 additions and 90 deletions

1
src/lib.rs Normal file
View File

@ -0,0 +1 @@
pub mod session;

View File

@ -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<String>,
}
fn get_data_dir() -> Result<PathBuf> {
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<PathBuf> {
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<String> {
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<PathBuf> {
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<String> {
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<Account> {
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<Account> {
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();

94
src/session.rs Normal file
View File

@ -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<PathBuf> {
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<PathBuf> {
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<String> {
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<PathBuf> {
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<String> {
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<Account> {
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<Account> {
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);
}