mirror of
https://github.com/Tilo-K/pastebin-cli.git
synced 2026-01-11 09:21:00 +00:00
Fixed creating and deleting
This commit is contained in:
103
src/main.rs
103
src/main.rs
@@ -1,11 +1,14 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
use rpassword::read_password;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
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() {
|
||||
@@ -22,7 +25,9 @@ fn get_api_key() {
|
||||
if key == "" {
|
||||
let mut input = String::new();
|
||||
|
||||
println!("Pastebin API key: ");
|
||||
print!("Pastebin API key: ");
|
||||
io::stdout().lock().flush().unwrap();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Error reading API_KEY");
|
||||
@@ -37,6 +42,45 @@ fn get_api_key() {
|
||||
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)]
|
||||
#[command(author, version, about, long_about= None)]
|
||||
struct Cli {
|
||||
@@ -61,15 +105,62 @@ enum Action {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
get_api_key();
|
||||
get_user_key().await;
|
||||
|
||||
let args = Cli::parse();
|
||||
|
||||
match args.action {
|
||||
Action::Create { file_path } => todo!(),
|
||||
Action::Delete { paste_id } => todo!(),
|
||||
Action::Create { file_path } => create(file_path).await,
|
||||
Action::Delete { paste_id } => delete(paste_id).await,
|
||||
Action::Edit {
|
||||
paste_id,
|
||||
file_path,
|
||||
} => todo!(),
|
||||
}
|
||||
} => edit(paste_id, file_path).await,
|
||||
};
|
||||
}
|
||||
|
||||
async fn create(file_path: PathBuf) {
|
||||
let file_name = file_path.file_name().unwrap().to_str().unwrap();
|
||||
let file_content = fs::read_to_string(&file_path).unwrap();
|
||||
|
||||
let paste_id = pastebin::create_paste(
|
||||
&API_KEY.lock().unwrap(),
|
||||
&USER_KEY.lock().unwrap(),
|
||||
file_name.to_owned(),
|
||||
file_content,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("{}", paste_id);
|
||||
}
|
||||
|
||||
async fn delete(paste_id: String) {
|
||||
let resp = pastebin::delete_paste(
|
||||
&API_KEY.lock().unwrap(),
|
||||
&USER_KEY.lock().unwrap(),
|
||||
paste_id,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("{}", resp);
|
||||
}
|
||||
|
||||
async fn edit(paste_id: String, file_path: PathBuf) {
|
||||
let file_name = file_path.file_name().unwrap().to_str().unwrap();
|
||||
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