92 lines
2.5 KiB
Rust
92 lines
2.5 KiB
Rust
use lazy_static::lazy_static;
|
|
use std::sync::Mutex;
|
|
|
|
use crate::services::login::Login;
|
|
use crate::commands::config;
|
|
use crate::store::gconfig;
|
|
use crate::commands::clone::get_url_props;
|
|
use crate::services::api_call::ApiCall;
|
|
|
|
lazy_static! {
|
|
static ref REQUEST_MANAGER: Mutex<Option<RequestManager>> = Mutex::new(None);
|
|
}
|
|
|
|
pub fn get_request_manager() -> &'static Mutex<Option<RequestManager>> {
|
|
if REQUEST_MANAGER.lock().unwrap().is_none() {
|
|
*REQUEST_MANAGER.lock().unwrap() = Some(RequestManager::new());
|
|
}
|
|
&REQUEST_MANAGER
|
|
}
|
|
|
|
pub struct RequestManager {
|
|
token: Option<String>,
|
|
host: Option<String>,
|
|
}
|
|
|
|
impl RequestManager {
|
|
pub fn new() -> Self {
|
|
RequestManager {
|
|
token: None,
|
|
host: None,
|
|
}
|
|
}
|
|
|
|
pub fn set_host(&mut self, host: String) {
|
|
self.host = Some(host);
|
|
}
|
|
|
|
pub fn get_host(&mut self) -> String
|
|
{
|
|
if self.host.is_none()
|
|
{
|
|
let remote = match config::get_remote("origin") {
|
|
Some(r) => r,
|
|
None => {
|
|
// todo ask user instead
|
|
eprintln!("fatal: unable to find a remote");
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
let (host, _, _) = get_url_props(&remote);
|
|
self.host = Some(host.clone());
|
|
// todo ask user
|
|
}
|
|
self.host.clone().unwrap()
|
|
}
|
|
|
|
pub fn get_token(&mut self) -> String {
|
|
if self.token.is_none() {
|
|
// look in global config
|
|
if let Some(token) = gconfig::read_token() {
|
|
if !token.is_empty() {
|
|
self.token = Some(token);
|
|
return self.token.clone().unwrap();
|
|
}
|
|
}
|
|
|
|
// look in local config
|
|
if let Some(token) = config::find_option_in_cat("core", "token")
|
|
{
|
|
if !token.is_empty() {
|
|
self.token = Some(token);
|
|
return self.token.clone().unwrap();
|
|
}
|
|
}
|
|
|
|
// ask for a token
|
|
let get_token = Login::new()
|
|
.ask_auth()
|
|
.set_host(Some(self.get_host()))
|
|
.send_login();
|
|
|
|
// todo deal with error cases
|
|
self.token = Some(get_token.unwrap());
|
|
if let Err(err) = gconfig::write_token(&self.token.clone().unwrap()) {
|
|
eprintln!("err: failed to write token ({})", err);
|
|
}
|
|
}
|
|
|
|
self.token.clone().unwrap()
|
|
}
|
|
}
|