Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat!: Allow reading from stdin in absence of path argument #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod query_parser;

use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;
use std::str;

Expand All @@ -17,11 +18,11 @@ use query_parser::{Query, TpathSegment, parse_query};
enum Args {
/// Print some data from the file
Get {
/// Path to the TOML file to read
#[structopt(parse(from_os_str))]
path: PathBuf,
/// Query within the TOML data (e.g. `dependencies.serde`, `foo[0].bar`)
query: String,
/// Path to the TOML file to read
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
#[structopt(flatten)]
opts: GetOpts,
},
Expand Down Expand Up @@ -71,9 +72,17 @@ fn read_parse(path: PathBuf) -> Result<Document, Error> {
Ok(data.parse::<Document>()?)
}

fn get(path: PathBuf, query: &str, opts: GetOpts) -> Result<(), Error> {
fn get(path: Option<PathBuf>, query: &str, opts: GetOpts) -> Result<(), Error> {
let tpath = parse_query_cli(query)?.0;
let doc = read_parse(path)?;
let doc: Document = match path {
Some(path) => read_parse(path)?,
None => {
let mut buf: Vec<u8> = vec![];
io::stdin().read_to_end(&mut buf)?;
let data = str::from_utf8(&buf)?;
data.parse::<Document>()?
}
};

if opts.output_toml {
print_toml_fragment(&doc, &tpath);
Expand Down