Move session handling to seperate module
This commit is contained in:
parent
e12108aa13
commit
e07eef82db
1
src/lib.rs
Normal file
1
src/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod session;
|
93
src/main.rs
93
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<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
94
src/session.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user