fix(token): allow to get and store token in local config

This commit is contained in:
lol 2024-02-21 16:50:54 +01:00
parent 9859f26604
commit 639f18426f
4 changed files with 32 additions and 16 deletions

View File

@ -12,8 +12,18 @@ pub struct CredentialArgs<'a> {
}
pub fn credential_add(args: CredentialArgs) {
let username = args.username.unwrap().next().unwrap();
// get remote if exists
let remote = match config::get_remote("origin") {
None => {
eprintln!("fatal: No remote origin, impossible to send request to get token");
std::process::exit(1);
},
Some(remote) => remote
};
let (host, _, _) = get_url_props(&remote);
// get username and password
let username = args.username.unwrap().next().unwrap();
let password = match args.password {
Some(mut pwd) => pwd.next().unwrap().to_owned(),
None => {
@ -22,22 +32,13 @@ pub fn credential_add(args: CredentialArgs) {
}
};
let remote = match config::get_remote("origin") {
None => {
eprintln!("fatal: No remote origin, impossible to send request to get token");
std::process::exit(1);
},
Some(remote) => remote
};
let (host, _, _) = get_url_props(&remote);
// get token
let get_token = Login::new()
.set_auth(username, &password)
.set_host(Some(host))
.send_login();
// deal with error
if let Err(err) = get_token {
if let RequestError(err) = err {
eprintln!("fatal: Failed to get token for these credential. ({})", err);
@ -48,6 +49,7 @@ pub fn credential_add(args: CredentialArgs) {
std::process::exit(1);
}
let _ = config::add_core(username, get_token.unwrap().as_str());
// save token
let _ = config::write_option_in_cat("core", "token", get_token.unwrap().as_str());
}

View File

@ -32,11 +32,11 @@ impl PushChange for New {
match res {
Err(ApiError::IncorrectRequest(err)) => {
eprintln!("fatal: error pushing file {}: {}", obj.name, err.status());
eprintln!("fatal: error pushing file '{}': {}", obj.name, err.status());
std::process::exit(1);
},
Err(ApiError::RequestError(_)) => {
eprintln!("fatal: request error pushing file {}", obj.name);
eprintln!("fatal: request error pushing file '{}'", obj.name);
std::process::exit(1);
}
_ => (),

View File

@ -56,6 +56,7 @@ impl RequestManager {
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);
@ -63,10 +64,21 @@ impl RequestManager {
}
}
// 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()) {

View File

@ -39,6 +39,8 @@ pub fn get_relative_s(p: String, api_props: &ApiProps) -> String {
final_p = final_p.strip_prefix("/remote.php/dav/files/").unwrap().to_string();
final_p = final_p.strip_prefix(&api_props.username).unwrap().to_string();
final_p = final_p.strip_prefix(&api_props.root).unwrap().to_string();
if final_p.starts_with("/") {
final_p = final_p.strip_prefix("/").unwrap().to_string();
}
final_p
}