From 3f8134f890c6ffb0e5b089ce4cc2bbc58a8ce38d Mon Sep 17 00:00:00 2001 From: raychu86 Date: Sun, 16 Aug 2020 00:36:22 -0700 Subject: [PATCH] Add a dedicated credentials file --- leo/commands/login.rs | 52 +++++-------------------------------------- leo/credentials.rs | 43 +++++++++++++++++++++++++++++++++++ leo/lib.rs | 3 ++- 3 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 leo/credentials.rs diff --git a/leo/commands/login.rs b/leo/commands/login.rs index 7371296f11..a0b7204377 100644 --- a/leo/commands/login.rs +++ b/leo/commands/login.rs @@ -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 { - 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, Option, Option); @@ -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(()) } diff --git a/leo/credentials.rs b/leo/credentials.rs new file mode 100644 index 0000000000..3c0c8bfcfd --- /dev/null +++ b/leo/credentials.rs @@ -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 { + let mut credentials = File::open(&LEO_CREDENTIALS_PATH.to_path_buf())?; + let mut buf = String::new(); + credentials.read_to_string(&mut buf)?; + Ok(buf) +} diff --git a/leo/lib.rs b/leo/lib.rs index 9c27ffed9d..0f3a4fd486 100644 --- a/leo/lib.rs +++ b/leo/lib.rs @@ -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;