Skip to content

Commit

Permalink
Adds --stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Aug 31, 2024
1 parent 4bfbd85 commit 8cfa6ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ $ createnv
| `--target` | File to write the result | `.env` |
| `--source` | File to use as a sample | `.env.sample` |
| `--chars-for-random-string` | Characters used to create random strings | All ASCII letters, numbers and a few extra characters (`!@#$%^&*(-_=+)`) |
| `--stdout` | Write to `stdout` instead of a file | |
| `--overwrite` | Do not ask before overwriting files | |
| `--use-default` | Do not ask for input on fields that have a default value | |

Expand Down
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ fn main() -> Result<()> {
.default_value(DEFAULT_ENV)
.help("File to write the result"),
)
.arg(
clap::Arg::new("stdout")
.long("stdout")
.help("Writes to stdout instead of to a target file")
.action(ArgAction::SetTrue)
.conflicts_with("target"),
)
.arg(
Arg::new("source")
.long("source")
Expand Down Expand Up @@ -80,9 +87,10 @@ fn main() -> Result<()> {
)
.get_matches();

let to_stdout = matches.get_one::<bool>("stdout").unwrap();
let target = matches.get_one::<String>("target").unwrap();
let overwrite = matches.get_one::<bool>("overwrite").unwrap();
if !overwrite && !should_write_to(target)? {
if !to_stdout && !overwrite && !should_write_to(target)? {
exit(0);
}

Expand All @@ -95,7 +103,11 @@ fn main() -> Result<()> {
let mut parser = Parser::new(source.as_str(), chars, use_default)?;
parser.parse(&mut stdin().lock())?;

let mut output = File::create(target)?;
output.write_all(parser.to_string().as_bytes())?;
if *to_stdout {
println!("\n{}", parser);
} else {
let mut output = File::create(target)?;
output.write_all(parser.to_string().as_bytes())?;
}
Ok(())
}

0 comments on commit 8cfa6ce

Please sign in to comment.