put client init code in utils

This commit is contained in:
Mahesh Bansod 2021-11-07 15:27:17 +05:30
parent 1e293c67aa
commit bb5763deb0
3 changed files with 32 additions and 31 deletions

View File

@ -1,6 +1,4 @@
use socialvoid as sv_client;
use socialvoid::session::RegisterRequest;
use socialvoid::session::SessionHolder;
use structopt::StructOpt;
mod error;
@ -15,34 +13,7 @@ async fn main() {
let args = Cli::from_args();
let config = load_config();
// initialize sv client -
// If session_file has a valid session holder, then use that session otherwise try to create a new one.
// let sv = match Socialvoid::load_session_or_default(config.session_file).await {
// };
let sv = match std::fs::read(&config.session_file) {
Ok(bytes) => match sv_client::new(SessionHolder::deserialize(bytes)).await {
Ok(client) => client,
Err(_) => panic!(
"The session file may be corrupt. try deleting it to have a new session created."
),
},
Err(err) => {
println!(
"There was a problem while reading the session file.\n{}",
err
);
// TODO: give the user the option to either quit or to change the path of the session file
// also look into taking the path of the config and session from the command line
println!("Creating new session.");
match sv_client::new_with_defaults().await {
Ok(client) => client,
Err(err) => panic!(
"There was an error while trying to establish a new session.\n{}",
MyFriendlyError::from(err)
),
}
}
};
let sv = init_client(&config).await;
if let Some(cmd) = args.commands {
match cmd {

View File

@ -2,6 +2,9 @@ use rpassword::read_password;
use serde::{Deserialize, Serialize};
use std::io::{stdin, stdout, Write};
use crate::error::MyFriendlyError;
use socialvoid::session::SessionHolder;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Config {
pub session_file: String,
@ -61,3 +64,30 @@ pub fn prompt_password(prompt: &str) -> String {
std::io::stdout().flush().unwrap();
read_password().expect("Couldn't read the password")
}
pub async fn init_client(config: &Config) -> socialvoid::Client {
match std::fs::read(&config.session_file) {
Ok(bytes) => match socialvoid::new(SessionHolder::deserialize(bytes)).await {
Ok(client) => client,
Err(_) => panic!(
"The session file may be corrupt. try deleting it to have a new session created."
),
},
Err(err) => {
println!(
"There was a problem while reading the session file.\n{}",
err
);
// TODO: give the user the option to either quit or to change the path of the session file
// also look into taking the path of the config and session from the command line
println!("Creating new session.");
match socialvoid::new_with_defaults().await {
Ok(client) => client,
Err(err) => panic!(
"There was an error while trying to establish a new session.\n{}",
MyFriendlyError::from(err)
),
}
}
}
}

View File

@ -64,7 +64,7 @@ async fn make_cdn_client_from(
}
/// Create a client with user defined session
/// And CDN as gven in the server information
/// And CDN as given in the server information
/// TODO: maybe verify the session and return an error if session is invalid
pub async fn new(session: SessionHolder) -> Result<Client, SocialvoidError> {
let rpc_client = Arc::new(socialvoid_rawclient::new());