Skip to content

Commit

Permalink
feat(pastes): allow piping in from stdin
Browse files Browse the repository at this point in the history
Signed-off-by: Amogh Lele <amolele@gmail.com>
  • Loading branch information
SphericalKat committed May 6, 2022
1 parent cafb9d5 commit d6b9472
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
node_modules
node_modules
.vscode
1 change: 1 addition & 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 @@ -12,6 +12,7 @@ include = ["src/**/*", "../LICENSE-*", "./README.md"]

[dependencies]
anyhow = "1.0.57"
atty = "0.2.14"
clap = { version = "3.1.16", features = ["derive"] }
colored = "2.0.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
Expand Down
26 changes: 25 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use std::{
env,
ffi::OsString,
io::{self, Read},
};

use clap::{Parser, Subcommand};
use paste::{Paste, PasteCommands};

Expand All @@ -19,9 +25,27 @@ enum Commands {
Paste(Paste),
}

fn stdin_to_args() -> Vec<OsString> {
let mut args: Vec<OsString> = env::args_os().collect();

let mut body = String::from("");
io::stdin().read_to_string(&mut body).unwrap();
args.push(body.into());

args
}

fn main() {
// parse CLI arguments
let args = Cli::parse();
let args: Cli;

// no input from stdin
if atty::is(atty::Stream::Stdin) {
args = Cli::parse();
} else {
// parse input from stdin
args = Cli::parse_from(stdin_to_args().iter());
}

// match commands
match args.command {
Expand Down

0 comments on commit d6b9472

Please sign in to comment.