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

🐛 fix --stdin CLI option #57

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ result
# editors
.idea/
.vscode/
tests/
tests.txt
100 changes: 99 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ categories = ["command-line-utilities"]

[dependencies]
anyhow = "1.0.71"
assert_cmd = "2.0.12"
clap = { version = "4.3.0", optional = true, features = ["unicode", "derive"] }
clap-stdin = "0.3.0"
env_logger = "0.10.0"
log = "0.4.17"
nu-cmd-lang = "0.88.1"
Expand Down
1 change: 1 addition & 0 deletions simplefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lkjlkj
23 changes: 19 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![doc = include_str!("../README.md")]

use clap::Parser;
use clap_stdin::MaybeStdin;
use log::{error, info, trace};
use nu_formatter::config::Config;
use std::{
Expand All @@ -20,16 +21,17 @@ enum ExitCode {
struct Cli {
#[arg(
required_unless_present("stdin"),
help = "one of more Nushell files you want to format"
help = "one of more Nushell files/folders you want to format"
)]
files: Vec<PathBuf>,
#[clap(default_value = "-")]
#[arg(
short,
long,
conflicts_with = "files",
help = "a string of Nushell directly given to the formatter"
)]
stdin: Option<String>,
stdin: Option<MaybeStdin<String>>,
#[arg(short, long, help = "the configuration file")]
config: Option<PathBuf>,
}
Expand Down Expand Up @@ -75,7 +77,7 @@ fn main() {
}

/// format a string passed via stdin and output it directly to stdout
fn format_string(string: Option<String>, options: &Config) -> ExitCode {
fn format_string(string: Option<MaybeStdin<String>>, options: &Config) -> ExitCode {
let output = nu_formatter::format_string(&string.unwrap(), options);
println!("output: \n{output}");

Expand Down Expand Up @@ -136,10 +138,23 @@ fn is_file_extension(file: &Path, extension: &str) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use assert_cmd::Command as AssertCommand;
use clap::CommandFactory;
use std::fs::File;

#[test]
fn clap_cli_construction() {
use clap::CommandFactory;
Cli::command().debug_assert();
}

#[test]
fn pipe_stdin_to_cli() {
// TODO: create a file instead of reading one in the repo
let mut file = File::create("tests.txt").unwrap();
file.write_all(b"Hello, world!").unwrap();
let mut binding = AssertCommand::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
AucaCoyan marked this conversation as resolved.
Show resolved Hide resolved
let result = dbg!(binding.arg("-s").arg("-").pipe_stdin("tests.txt").ok());

result.unwrap().assert().success();
}
}
Loading