Browse Source

Initial commit

master
Brandon Anzaldi 1 year ago
commit
8820c18540
4 changed files with 1651 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 1576
    0
      Cargo.lock
  3. 10
    0
      Cargo.toml
  4. 63
    0
      src/main.rs

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
/target
**/*.rs.bk

+ 1576
- 0
Cargo.lock
File diff suppressed because it is too large
View File


+ 10
- 0
Cargo.toml View File

@@ -0,0 +1,10 @@
[package]
name = "hastie"
version = "0.1.0"
authors = ["Brandon Anzaldi <brandon@brandonanzaldi.com>"]
edition = "2018"

[dependencies]
clap = "2.33.0"
reqwest = "0.9.18"
atty = "0.2"

+ 63
- 0
src/main.rs View File

@@ -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(())
}

Loading…
Cancel
Save