|
|
@@ -0,0 +1,63 @@ |
|
|
|
extern crate atty; |
|
|
|
extern crate clap; |
|
|
|
extern crate reqwest; |
|
|
|
|
|
|
|
use atty::Stream; |
|
|
|
use clap::{App, Arg}; |
|
|
|
use reqwest::header::{ACCEPT, CONTENT_TYPE, USER_AGENT}; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::fs::File; |
|
|
|
use std::io::{self, BufRead, BufReader}; |
|
|
|
use std::process; |
|
|
|
|
|
|
|
fn main() -> Result<(), Box<std::error::Error>> { |
|
|
|
let mut app = App::new("Hastie") |
|
|
|
.version("0.1.0") |
|
|
|
.author("Brandon Anzaldi <brandon@brandonanzaldi.com>") |
|
|
|
.about("A simple command-line client for Hasteb.in.") |
|
|
|
.arg( |
|
|
|
Arg::with_name("INPUT") |
|
|
|
.help("The input file to use. Defaults to stdin.") |
|
|
|
.index(1), |
|
|
|
); |
|
|
|
let mut help_text = Vec::new(); |
|
|
|
match app.write_long_help(&mut help_text) { |
|
|
|
Err(e) => panic!(e), |
|
|
|
_ => (), |
|
|
|
}; |
|
|
|
let matches = app.get_matches(); |
|
|
|
|
|
|
|
let mut reader: Box<BufRead> = match matches.value_of("INPUT") { |
|
|
|
None => { |
|
|
|
if atty::is(Stream::Stdin) { |
|
|
|
println!("{}", String::from_utf8(help_text).unwrap()); |
|
|
|
process::exit(0); |
|
|
|
} else { |
|
|
|
Box::new(BufReader::new(io::stdin())) |
|
|
|
} |
|
|
|
} |
|
|
|
Some(filename) => Box::new(BufReader::new(File::open(filename).unwrap())), |
|
|
|
}; |
|
|
|
|
|
|
|
let mut data = String::new(); |
|
|
|
reader.read_to_string(&mut data)?; |
|
|
|
|
|
|
|
let client = reqwest::Client::new(); |
|
|
|
let resp: HashMap<String, String> = client |
|
|
|
.post("https://hasteb.in/documents") |
|
|
|
.header(USER_AGENT, "hastie-rs/1.0.0") |
|
|
|
.header(CONTENT_TYPE, "application/json; charset=utf8") |
|
|
|
.header(ACCEPT, "application/json") |
|
|
|
.body(data) |
|
|
|
.send()? |
|
|
|
.json()?; |
|
|
|
|
|
|
|
let key = resp.get("key").unwrap_or_else(|| { |
|
|
|
println!("Error: Unable to retrieve upload key from Hasteb.in"); |
|
|
|
process::exit(1); |
|
|
|
}); |
|
|
|
|
|
|
|
println!("https://hasteb.in/{}", key); |
|
|
|
|
|
|
|
Ok(()) |
|
|
|
} |