mirror of
https://github.com/Tilo-K/pastebin-cli.git
synced 2026-01-09 16:31:01 +00:00
Basic Setup
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1261
Cargo.lock
generated
Normal file
1261
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "pastebin-cli"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { version = "4.4.6", features = ["derive"] }
|
||||||
|
home = "0.5.5"
|
||||||
|
reqwest = "0.11.22"
|
||||||
|
tokio = { version = "1.33.0", features = ["full"] }
|
||||||
75
src/main.rs
Normal file
75
src/main.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
use std::fs;
|
||||||
|
use std::io;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
mod pastebin;
|
||||||
|
|
||||||
|
static API_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();
|
||||||
|
|
||||||
|
println!("Pastebin API key: ");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser, Debug, Clone)]
|
||||||
|
#[command(author, version, about, long_about= None)]
|
||||||
|
struct Cli {
|
||||||
|
#[command(subcommand)]
|
||||||
|
action: Action,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand, Debug, Clone)]
|
||||||
|
enum Action {
|
||||||
|
Create {
|
||||||
|
file_path: std::path::PathBuf,
|
||||||
|
},
|
||||||
|
Delete {
|
||||||
|
paste_id: String,
|
||||||
|
},
|
||||||
|
Edit {
|
||||||
|
paste_id: String,
|
||||||
|
file_path: std::path::PathBuf,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
get_api_key();
|
||||||
|
|
||||||
|
let args = Cli::parse();
|
||||||
|
|
||||||
|
match args.action {
|
||||||
|
Action::Create { file_path } => todo!(),
|
||||||
|
Action::Delete { paste_id } => todo!(),
|
||||||
|
Action::Edit {
|
||||||
|
paste_id,
|
||||||
|
file_path,
|
||||||
|
} => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/pastebin.rs
Normal file
51
src/pastebin.rs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
pub async fn create_paste(
|
||||||
|
api_key: &str,
|
||||||
|
name: String,
|
||||||
|
content: String,
|
||||||
|
paste_id: Option<String>,
|
||||||
|
) -> Result<String, Box<dyn Error>> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let mut req = client
|
||||||
|
.post("https://pastebin.com/api/api_post.php")
|
||||||
|
.query(&[
|
||||||
|
("api_dev_key", api_key),
|
||||||
|
("api_option", "paste"),
|
||||||
|
("api_paste_code", &content),
|
||||||
|
("api_paste_private", "1"),
|
||||||
|
("api_paste_expire_date", "1W"),
|
||||||
|
("api_paste_name", &name),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (paste_id.is_some()) {
|
||||||
|
req = req.query(&[("api_paste_key", &paste_id.unwrap())]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let resp = req.send().await?;
|
||||||
|
if resp.status() == 200 {
|
||||||
|
let resp_body = resp.text().await?;
|
||||||
|
return Ok(resp_body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(resp.status().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_paste(api_key: &str, paste_id: String) -> Result<String, Box<dyn Error>> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let req = client
|
||||||
|
.post("https://pastebin.com/api/api_post.php")
|
||||||
|
.query(&[
|
||||||
|
("api_dev_key", api_key),
|
||||||
|
("api_option", "delete"),
|
||||||
|
("api_paste_key", &paste_id),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if req.status() == 200 {
|
||||||
|
return Ok("Deleted".to_owned());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(req.status().to_string())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user