mirror of
https://github.com/Tilo-K/pastebin-cli.git
synced 2026-01-11 01:11:02 +00:00
Refactoring keys
This commit is contained in:
81
src/keys.rs
Normal file
81
src/keys.rs
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
use rpassword::read_password;
|
||||||
|
use std::fs;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::pastebin;
|
||||||
|
|
||||||
|
pub static API_KEY: Mutex<String> = Mutex::new(String::new());
|
||||||
|
pub static USER_KEY: Mutex<String> = Mutex::new(String::new());
|
||||||
|
|
||||||
|
pub fn get_api_key() {
|
||||||
|
let home_dir = match home::home_dir() {
|
||||||
|
Some(path) => path,
|
||||||
|
None => panic!("Impossible to get your home dir!"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let key_file = home_dir.join(".pastebin_key");
|
||||||
|
let key = match fs::read_to_string(&key_file) {
|
||||||
|
Ok(d) => d,
|
||||||
|
Err(_) => "".to_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if key == "" {
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
print!("Pastebin API key: ");
|
||||||
|
io::stdout().lock().flush().unwrap();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("Error reading API_KEY");
|
||||||
|
|
||||||
|
API_KEY.lock().unwrap().push_str(&input);
|
||||||
|
|
||||||
|
fs::write(key_file, input).expect("Error writing API KEY");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
API_KEY.lock().unwrap().push_str(&key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_user_key() {
|
||||||
|
let home_dir = match home::home_dir() {
|
||||||
|
Some(path) => path,
|
||||||
|
None => panic!("Impossible to get your home dir!"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let key_file = home_dir.join(".pastebin_userkey");
|
||||||
|
let key = match fs::read_to_string(&key_file) {
|
||||||
|
Ok(d) => d,
|
||||||
|
Err(_) => "".to_owned(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if key == "" {
|
||||||
|
let mut username = String::new();
|
||||||
|
let mut password = String::new();
|
||||||
|
|
||||||
|
print!("Username: ");
|
||||||
|
io::stdout().lock().flush().unwrap();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut username)
|
||||||
|
.expect("Error reading Username");
|
||||||
|
|
||||||
|
print!("Password: ");
|
||||||
|
io::stdout().lock().flush().unwrap();
|
||||||
|
password = read_password().unwrap();
|
||||||
|
|
||||||
|
let key = pastebin::get_user_key(&API_KEY.lock().unwrap(), username, password)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
USER_KEY.lock().unwrap().push_str(&key);
|
||||||
|
|
||||||
|
fs::write(key_file, key).expect("Error writing API KEY");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
USER_KEY.lock().unwrap().push_str(&key);
|
||||||
|
}
|
||||||
121
src/main.rs
121
src/main.rs
@@ -1,86 +1,10 @@
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use rpassword::read_password;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Mutex;
|
|
||||||
|
mod keys;
|
||||||
mod pastebin;
|
mod pastebin;
|
||||||
|
|
||||||
static API_KEY: Mutex<String> = Mutex::new(String::new());
|
|
||||||
static USER_KEY: Mutex<String> = Mutex::new(String::new());
|
|
||||||
|
|
||||||
fn get_api_key() {
|
|
||||||
let home_dir = match home::home_dir() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => panic!("Impossible to get your home dir!"),
|
|
||||||
};
|
|
||||||
|
|
||||||
let key_file = home_dir.join(".pastebin_key");
|
|
||||||
let key = match fs::read_to_string(&key_file) {
|
|
||||||
Ok(d) => d,
|
|
||||||
Err(_) => "".to_owned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if key == "" {
|
|
||||||
let mut input = String::new();
|
|
||||||
|
|
||||||
print!("Pastebin API key: ");
|
|
||||||
io::stdout().lock().flush().unwrap();
|
|
||||||
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut input)
|
|
||||||
.expect("Error reading API_KEY");
|
|
||||||
|
|
||||||
API_KEY.lock().unwrap().push_str(&input);
|
|
||||||
|
|
||||||
fs::write(key_file, input).expect("Error writing API KEY");
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
API_KEY.lock().unwrap().push_str(&key);
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_user_key() {
|
|
||||||
let home_dir = match home::home_dir() {
|
|
||||||
Some(path) => path,
|
|
||||||
None => panic!("Impossible to get your home dir!"),
|
|
||||||
};
|
|
||||||
|
|
||||||
let key_file = home_dir.join(".pastebin_userkey");
|
|
||||||
let key = match fs::read_to_string(&key_file) {
|
|
||||||
Ok(d) => d,
|
|
||||||
Err(_) => "".to_owned(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if key == "" {
|
|
||||||
let mut username = String::new();
|
|
||||||
let mut password = String::new();
|
|
||||||
|
|
||||||
print!("Username: ");
|
|
||||||
io::stdout().lock().flush().unwrap();
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut username)
|
|
||||||
.expect("Error reading Username");
|
|
||||||
|
|
||||||
print!("Password: ");
|
|
||||||
io::stdout().lock().flush().unwrap();
|
|
||||||
password = read_password().unwrap();
|
|
||||||
|
|
||||||
let key = pastebin::get_user_key(&API_KEY.lock().unwrap(), username, password)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
USER_KEY.lock().unwrap().push_str(&key);
|
|
||||||
|
|
||||||
fs::write(key_file, key).expect("Error writing API KEY");
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
USER_KEY.lock().unwrap().push_str(&key);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
#[command(author, version, about, long_about= None)]
|
#[command(author, version, about, long_about= None)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
@@ -90,32 +14,26 @@ struct Cli {
|
|||||||
|
|
||||||
#[derive(Subcommand, Debug, Clone)]
|
#[derive(Subcommand, Debug, Clone)]
|
||||||
enum Action {
|
enum Action {
|
||||||
Create {
|
Create { file_path: std::path::PathBuf },
|
||||||
file_path: std::path::PathBuf,
|
Delete { paste_id: String },
|
||||||
},
|
/*
|
||||||
Delete {
|
|
||||||
paste_id: String,
|
|
||||||
},
|
|
||||||
Edit {
|
Edit {
|
||||||
paste_id: String,
|
paste_id: String,
|
||||||
file_path: std::path::PathBuf,
|
file_path: std::path::PathBuf,
|
||||||
},
|
},
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
get_api_key();
|
keys::get_api_key();
|
||||||
get_user_key().await;
|
keys::get_user_key().await;
|
||||||
|
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
match args.action {
|
match args.action {
|
||||||
Action::Create { file_path } => create(file_path).await,
|
Action::Create { file_path } => create(file_path).await,
|
||||||
Action::Delete { paste_id } => delete(paste_id).await,
|
Action::Delete { paste_id } => delete(paste_id).await,
|
||||||
Action::Edit {
|
|
||||||
paste_id,
|
|
||||||
file_path,
|
|
||||||
} => edit(paste_id, file_path).await,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,8 +42,8 @@ async fn create(file_path: PathBuf) {
|
|||||||
let file_content = fs::read_to_string(&file_path).unwrap();
|
let file_content = fs::read_to_string(&file_path).unwrap();
|
||||||
|
|
||||||
let paste_id = pastebin::create_paste(
|
let paste_id = pastebin::create_paste(
|
||||||
&API_KEY.lock().unwrap(),
|
&keys::API_KEY.lock().unwrap(),
|
||||||
&USER_KEY.lock().unwrap(),
|
&keys::USER_KEY.lock().unwrap(),
|
||||||
file_name.to_owned(),
|
file_name.to_owned(),
|
||||||
file_content,
|
file_content,
|
||||||
None,
|
None,
|
||||||
@@ -138,8 +56,8 @@ async fn create(file_path: PathBuf) {
|
|||||||
|
|
||||||
async fn delete(paste_id: String) {
|
async fn delete(paste_id: String) {
|
||||||
let resp = pastebin::delete_paste(
|
let resp = pastebin::delete_paste(
|
||||||
&API_KEY.lock().unwrap(),
|
&keys::API_KEY.lock().unwrap(),
|
||||||
&USER_KEY.lock().unwrap(),
|
&keys::USER_KEY.lock().unwrap(),
|
||||||
paste_id,
|
paste_id,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
@@ -149,18 +67,5 @@ async fn delete(paste_id: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn edit(paste_id: String, file_path: PathBuf) {
|
async fn edit(paste_id: String, file_path: PathBuf) {
|
||||||
let file_name = file_path.file_name().unwrap().to_str().unwrap();
|
todo!("No offical way to do that yet");
|
||||||
let file_content = fs::read_to_string(&file_path).unwrap();
|
|
||||||
|
|
||||||
let resp = pastebin::create_paste(
|
|
||||||
&API_KEY.lock().unwrap(),
|
|
||||||
&USER_KEY.lock().unwrap(),
|
|
||||||
file_name.to_owned(),
|
|
||||||
file_content,
|
|
||||||
Some(paste_id),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
println!("{}", resp);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user