feat(credential): allow to add credential
This commit is contained in:
@@ -7,7 +7,7 @@ pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
|
||||
root.push("config");
|
||||
|
||||
// check if there is already a remote with this name
|
||||
if get_remote(name)
|
||||
if get_remote(name).is_some()
|
||||
{
|
||||
eprintln!("error: remote {} already exists.", name);
|
||||
std::process::exit(3);
|
||||
@@ -26,24 +26,50 @@ pub fn add_remote(name: &str, url: &str) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_remote(name: &str) -> bool {
|
||||
pub fn get_remote(name: &str) -> Option<String> {
|
||||
let mut root = path::nextsync();
|
||||
root.push("config");
|
||||
|
||||
let target = String::from(format!("[remote \"{}\"]", name));
|
||||
|
||||
if let Ok(lines) = read::read_lines(root) {
|
||||
for line_result in lines {
|
||||
if let Ok(line) = line_result {
|
||||
if let Ok(mut lines) = read::read_lines(root) {
|
||||
while let Some(line_res) = lines.next() {
|
||||
if let Ok(line) = line_res {
|
||||
if line == target {
|
||||
return true;
|
||||
// get the first line starting with 'url = '
|
||||
while let Some(line_res) = lines.next() {
|
||||
if let Ok(line) = line_res {
|
||||
let mut splitted_res = line.split("=");
|
||||
if let Some(splitted) = splitted_res.next() {
|
||||
if splitted == "\turl " {
|
||||
return Some(splitted_res.next().unwrap().to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn add_core(name: &str, value: &str) -> io::Result<()> {
|
||||
let mut root = path::nextsync();
|
||||
root.push("config");
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(root)?;
|
||||
|
||||
writeln!(file, "[core]")?;
|
||||
writeln!(file, "\t{} = {}", name, value)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set(var: &str, val: &str) -> io::Result<()> {
|
||||
|
||||
53
src/commands/credential.rs
Normal file
53
src/commands/credential.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use clap::Values;
|
||||
use crate::commands::clone::get_url_props;
|
||||
use crate::services::api::ApiError::RequestError;
|
||||
|
||||
use crate::services::login::Login;
|
||||
use crate::services::api_call::ApiCall;
|
||||
use crate::commands::config;
|
||||
|
||||
pub struct CredentialArgs<'a> {
|
||||
pub username: Option<Values<'a>>,
|
||||
pub password: Option<Values<'a>>,
|
||||
}
|
||||
|
||||
pub fn credential_add(args: CredentialArgs) {
|
||||
let username = args.username.unwrap().next().unwrap();
|
||||
|
||||
let password = match args.password {
|
||||
Some(mut pwd) => pwd.next().unwrap().to_owned(),
|
||||
None => {
|
||||
println!("Please enter the password for {}: ", username);
|
||||
rpassword::read_password().unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
let get_token = Login::new()
|
||||
.set_auth(username, &password)
|
||||
.set_host(Some(host))
|
||||
.send_login();
|
||||
|
||||
|
||||
if let Err(err) = get_token {
|
||||
if let RequestError(err) = err {
|
||||
eprintln!("fatal: Failed to get token for these credential. ({})", err);
|
||||
}
|
||||
else {
|
||||
eprintln!("fatal: Failed to get token for these credential.");
|
||||
}
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let _ = config::add_core(username, get_token.unwrap().as_str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user