Skip to content

Commit

Permalink
enchanced readabillity of code, also added better 0x0 api cov
Browse files Browse the repository at this point in the history
  • Loading branch information
kyee-rs committed Dec 17, 2022
1 parent 69f22b4 commit 0dfc1ac
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ clap = { version = "*", features = ["derive"] }
clap_complete = "4.0.6"
colored = { version = "*" }
reqwest = { version = "*", features = ["json", "blocking", "multipart"] }
uuid = { version = "*", features = ["v4", "fast-rng", "macro-diagnostics"] }
4 changes: 2 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ if [ -f /bin/0x0 ]; then
echo "0x0 already installed"
echo "reinstalling..."
sudo rm /bin/0x0
sudo wget https://github.com/voxelin/0x0.st/releases/download/v.1.1.1/0x0 -q --show-progress
sudo wget https://github.com/voxelin/0x0.st/releases/latest/download/0x0 -q --show-progress
sudo chmod +x 0x0
echo "0x0 reinstalled successfully!"
exit 1
fi

sudo wget https://github.com/voxelin/0x0.st/releases/download/v.1.1.1/0x0 -q --show-progress
sudo wget https://github.com/voxelin/0x0.st/releases/latest/download/0x0 -q --show-progress
sudo chmod +x 0x0

if [ -f /bin/0x0 ]; then
Expand Down
61 changes: 40 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use clap::{value_parser, Arg, ArgAction, Command, ValueHint};
use clap_complete::{generate, Generator, Shell};
use colored::Colorize;
use std::io;
use std::{io, path::PathBuf};
use uuid::Uuid;
/// Simple program to upload file to 0x0.st
fn build_cli() -> Command {
Expand All @@ -11,15 +12,30 @@ fn build_cli() -> Command {
.arg(
Arg::new("file")
.help("File to upload")
.alias("f")
.value_parser(value_parser!(PathBuf))
.action(ArgAction::Set)
.value_hint(ValueHint::AnyPath),
)
.arg(
Arg::new("expires")
.help("Expire time in hours (e.g. 3)")
.short('e')
.long("expires")
.value_parser(value_parser!(i8))
.action(ArgAction::Set)
.value_hint(ValueHint::Other),
)
.arg(
Arg::new("secret")
.help("Use UUIDv4 to cypher your url. [personal recommendation]")
.action(ArgAction::SetTrue)
.short('s'),
)
.arg(
Arg::new("generator")
.long("generate")
.action(ArgAction::Set)
.alias("g")
.short('g')
.value_parser(value_parser!(Shell)),
)
}
Expand All @@ -30,13 +46,20 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {

fn main() {
let matches = build_cli().get_matches(); // Get the arguments from the user

// -------------------------------------------------------------------------------------
if let Some(generator) = matches.get_one::<Shell>("generator").copied() {
let mut cmd = build_cli();
print_completions(generator, &mut cmd);
std::process::exit(0);
} // Generate the completions
}
// -------------------------------------------------------------------------------------

let expires = matches.get_one::<i8>("expires"); // Get the expire time
let secret = matches.get_one::<bool>("secret"); // Get the secret state
let file = matches.get_one::<PathBuf>("file"); // Get the file path

let file = matches.get_one::<String>("file"); // Get the file path
// Check if the user provided a file
if file.is_none() {
println!(
"{}",
Expand All @@ -46,26 +69,22 @@ fn main() {
}

// Create the multipart form
let uploader = reqwest::blocking::multipart::Form::new().file("file", file.unwrap());
if uploader.is_err() {
// Check for the errors
println!(
"{}",
format!("Error! Check the file path again.").bold().red()
);
std::process::exit(1);
} else if uploader.is_ok() {
// Or print the uploading message
println!("- {}", format!("Uploading file...").bold().yellow());
println!("- {}", format!("Done!").bold().green());
let mut form = reqwest::blocking::multipart::Form::new();
form = form.file("file", file.unwrap()).unwrap();
if let Some(expires) = expires {
form = form.text("expires", expires.to_string());
}
if secret.is_some() {
form = form.text("secret", Uuid::new_v4().to_string());
}

// Print the uploading message
println!("- {}", format!("Uploading file...").bold().yellow());
println!("- {}", format!("Done!").bold().green());

// Make Post request to 0x0.st
let client = reqwest::blocking::Client::new();
let res = client
.post("https://0x0.st")
.multipart(uploader.unwrap())
.send();
let res = client.post("https://0x0.st").multipart(form).send();

// Check for the errors
if res.is_err() {
Expand Down

0 comments on commit 0dfc1ac

Please sign in to comment.