Skip to content

Commit bce5796

Browse files
authored
fix processing stdin with envset keys (#7)
* feat: Add support for piping input to keys command * fix: Add missing print_keys_from_map import to main.rs * changelog
1 parent 0849f0e commit bce5796

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# changelog
2+
3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- piping .env to stdin and running envset keys will now print the keys instead of writing them to .env

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ brew install schpet/tap/envset
1818
cargo install envset
1919
```
2020

21-
otherwise, grab the [latest release](https://github.com/schpet/envset/releases).
21+
otherwise, grab the [latest release](https://github.com/schpet/envset/releases/latest).
2222

2323
## usage
2424

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,18 @@ pub fn print_lines<W: Write>(lines: &[parser::Line], writer: &mut W, use_color:
225225

226226
pub fn print_env_keys_to_writer<W: Write>(file_path: &str, writer: &mut W) {
227227
if let Ok(env_vars) = read_env_vars(file_path) {
228-
for key in env_vars.keys() {
229-
writeln!(writer, "{}", key).unwrap();
230-
}
228+
print_keys_from_map(&env_vars, writer);
231229
} else {
232230
eprintln!("Error reading .env file");
233231
}
234232
}
235233

234+
pub fn print_keys_from_map<W: Write>(env_vars: &HashMap<String, String>, writer: &mut W) {
235+
for key in env_vars.keys() {
236+
writeln!(writer, "{}", key).unwrap();
237+
}
238+
}
239+
236240
pub fn delete_env_vars(
237241
content: &str,
238242
keys: &[String],

src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::process;
77

88
use envset::{
99
add_env_vars, parse_args, parse_stdin, print_env_file_contents, print_env_keys_to_writer,
10-
print_env_vars, print_env_vars_as_json, print_parse_tree, read_env_file_contents,
11-
read_env_vars,
10+
print_env_vars, print_env_vars_as_json, print_keys_from_map, print_parse_tree,
11+
read_env_file_contents, read_env_vars,
1212
};
1313

1414
fn print_diff(old_content: &str, new_content: &str, use_color: bool) {
@@ -128,7 +128,12 @@ fn main() {
128128
return; // Exit after printing
129129
}
130130
Some(Commands::Keys) => {
131-
print_env_keys_to_writer(&cli.file, &mut std::io::stdout());
131+
if !atty::is(Stream::Stdin) {
132+
let env_vars = parse_stdin();
133+
print_keys_from_map(&env_vars, &mut std::io::stdout());
134+
} else {
135+
print_env_keys_to_writer(&cli.file, &mut std::io::stdout());
136+
}
132137
}
133138
Some(Commands::Delete { keys }) => match read_env_file_contents(&cli.file) {
134139
Ok(old_content) => match envset::delete_env_vars(&old_content, keys) {

0 commit comments

Comments
 (0)