Add a dedicated credentials file

This commit is contained in:
raychu86 2020-08-16 00:36:22 -07:00
parent 9c129ab61f
commit 3f8134f890
3 changed files with 50 additions and 48 deletions

View File

@ -9,57 +9,20 @@
use crate::{
cli::CLI,
cli_types::*,
credentials::*,
errors::{
CLIError::LoginError,
LoginError::{CannotGetToken, ConnectionUnavalaible, WrongLoginOrPassword},
},
};
use dirs::home_dir;
use lazy_static::lazy_static;
use std::{
collections::HashMap,
fs::{create_dir_all, File},
io,
io::prelude::*,
path::{Path, PathBuf},
};
const PACKAGE_MANAGER_URL: &str = "https://apm-backend-dev.herokuapp.com/";
const LOGIN_URL: &str = "api/account/authenticate";
use std::collections::HashMap;
const LEO_CREDENTIALS_FILE: &str = "credentials";
lazy_static! {
static ref LEO_CREDENTIALS_DIR: PathBuf = {
let mut path = home_dir().expect("Invalid home directory");
path.push(".leo");
path
};
static ref LEO_CREDENTIALS_PATH: PathBuf = {
let mut path = LEO_CREDENTIALS_DIR.to_path_buf();
path.push(LEO_CREDENTIALS_FILE);
path
};
}
pub const LOGIN_URL: &str = "api/account/authenticate";
#[derive(Debug)]
pub struct LoginCommand;
impl LoginCommand {
fn write_token(token: &str) -> Result<(), io::Error> {
let mut credentials = File::create(&LEO_CREDENTIALS_PATH.to_path_buf())?;
credentials.write_all(&token.as_bytes())?;
Ok(())
}
pub fn read_token() -> Result<String, io::Error> {
let mut credentials = File::open(&LEO_CREDENTIALS_PATH.to_path_buf())?;
let mut buf = String::new();
credentials.read_to_string(&mut buf)?;
Ok(buf)
}
}
impl CLI for LoginCommand {
// Format: token, username, password
type Options = (Option<String>, Option<String>, Option<String>);
@ -136,16 +99,11 @@ impl CLI for LoginCommand {
// Login using JWT
(_, _, _) => {
// TODO JWT
LoginCommand::read_token()?
read_token()?
}
};
// Create Leo credentials directory if it not exists
if !Path::new(&LEO_CREDENTIALS_DIR.to_path_buf()).exists() {
create_dir_all(&LEO_CREDENTIALS_DIR.to_path_buf())?;
}
LoginCommand::write_token(token.as_str())?;
write_token(token.as_str())?;
log::info!("Successfully logged in");
Ok(())
}

43
leo/credentials.rs Normal file
View File

@ -0,0 +1,43 @@
use dirs::home_dir;
use lazy_static::lazy_static;
use std::{
fs::{create_dir_all, File},
io,
io::prelude::*,
path::{Path, PathBuf},
};
pub const PACKAGE_MANAGER_URL: &str = "https://apm-backend-dev.herokuapp.com/";
pub const LEO_CREDENTIALS_FILE: &str = "credentials";
lazy_static! {
pub static ref LEO_CREDENTIALS_DIR: PathBuf = {
let mut path = home_dir().expect("Invalid home directory");
path.push(".leo");
path
};
pub static ref LEO_CREDENTIALS_PATH: PathBuf = {
let mut path = LEO_CREDENTIALS_DIR.to_path_buf();
path.push(LEO_CREDENTIALS_FILE);
path
};
}
pub fn write_token(token: &str) -> Result<(), io::Error> {
// Create Leo credentials directory if it not exists
if !Path::new(&LEO_CREDENTIALS_DIR.to_path_buf()).exists() {
create_dir_all(&LEO_CREDENTIALS_DIR.to_path_buf())?;
}
let mut credentials = File::create(&LEO_CREDENTIALS_PATH.to_path_buf())?;
credentials.write_all(&token.as_bytes())?;
Ok(())
}
pub fn read_token() -> Result<String, io::Error> {
let mut credentials = File::open(&LEO_CREDENTIALS_PATH.to_path_buf())?;
let mut buf = String::new();
credentials.read_to_string(&mut buf)?;
Ok(buf)
}

View File

@ -1,9 +1,10 @@
#[macro_use]
extern crate thiserror;
#[cfg_attr(tarpaulin, skip)]
pub mod cli;
pub mod cli_types;
pub mod commands;
#[cfg_attr(tarpaulin, skip)]
pub mod credentials;
pub mod errors;
pub mod logger;