Skip to content

Commit 65a7d0b

Browse files
committed
feat: add --format argument to print command
1 parent 44fac3b commit 65a7d0b

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ Usage:
140140

141141
Options:
142142
- `--help`/`-h` - show help message
143+
- `--format <FORMAT>`/`-f <FORMAT>` - set custom format.
144+
145+
By default, it uses format `{name} - "{value}"`. In format string `{name}` is replaced with name of the variable and `{value}` is replaced with it's value.
143146

144147
For example:
145148
```shell
@@ -150,7 +153,15 @@ SystemDrive = "C:"
150153
SystemRoot = "C:\\Windows"
151154
...
152155
```
153-
It will print all environment variables in format `VAR = "VALUE"`.
156+
```shell
157+
$ envfetch print --format "{name}: {value};"
158+
SHELL: powershell;
159+
windir: C:\\Windows;
160+
SystemDrive: C:;
161+
SystemRoot: C:\\Windows;
162+
...
163+
```
164+
It will print all environment variables in given format.
154165
#### Get
155166
Get value of environment variable
156167

src/commands.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn run_command(command: &Commands) -> ExitCode {
3333
return ExitCode::FAILURE;
3434
}
3535
}
36-
Commands::Print => print_env(),
36+
Commands::Print(opt) => print_env(opt),
3737
Commands::Load(opt) => {
3838
if let Err(error) = load(opt) {
3939
error!("{}", error);
@@ -72,9 +72,10 @@ pub fn run_command(command: &Commands) -> ExitCode {
7272
}
7373

7474
/// Print all environment variables
75-
pub fn print_env() {
75+
pub fn print_env(opt: &PrintArgs) {
76+
let format = &opt.format.clone().unwrap_or("{name} = \"{value}\"".to_owned());
7677
// Print all environment variables
77-
variables::print_env();
78+
variables::print_env(format);
7879
}
7980

8081
/// Load variables from dotenv-style file
@@ -237,7 +238,7 @@ mod tests {
237238
fn test_run_command_print() {
238239
unsafe { env::set_var("TEST_PRINT_RUN", "test_value") };
239240
with_captured_output(|| {
240-
run_command(&Commands::Print);
241+
run_command(&Commands::Print(PrintArgs { format: None }));
241242
});
242243
unsafe { env::remove_var("TEST_PRINT_RUN") };
243244
}
@@ -280,7 +281,7 @@ mod tests {
280281
unsafe { env::set_var("TEST_PRINT_VAR", "test_value") };
281282

282283
// Call function - just verify it executes without panicking
283-
print_env();
284+
print_env(&PrintArgs { format: None });
284285

285286
// Clean up
286287
unsafe { env::remove_var("TEST_PRINT_VAR") };
@@ -293,7 +294,7 @@ mod tests {
293294
unsafe { env::set_var("TEST_VAR_2", "value2") };
294295

295296
// Call function - just verify it executes without panicking
296-
print_env();
297+
print_env(&PrintArgs { format: None });
297298

298299
// Clean up
299300
unsafe { env::remove_var("TEST_VAR_1") };
@@ -728,7 +729,7 @@ mod tests {
728729
fn test_run_command_print_env() {
729730
unsafe { env::set_var("TEST_PRINT_ENV", "test_value") };
730731
with_captured_output(|| {
731-
assert_eq!(run_command(&Commands::Print), ExitCode::SUCCESS);
732+
assert_eq!(run_command(&Commands::Print(PrintArgs { format: None })), ExitCode::SUCCESS);
732733
});
733734
unsafe { env::remove_var("TEST_PRINT_ENV") };
734735
}

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod tests {
6363
#[test]
6464
fn test_print_command() {
6565
let args = Cli::parse_from(["envfetch", "print"]);
66-
assert_eq!(args.command, Commands::Print);
66+
assert_eq!(args.command, Commands::Print(PrintArgs { format: None }));
6767
}
6868

6969
#[test]

src/models.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ pub enum Commands {
3737
/// Load environment variables from dotenv file and optionally run given process.
3838
Load(LoadArgs),
3939
/// Print all environment variables.
40-
Print,
40+
Print(PrintArgs),
41+
}
42+
43+
/// Args for print command
44+
#[derive(Args, Debug, PartialEq, Eq)]
45+
pub struct PrintArgs {
46+
/// Set custom format, by default {name} = "{value}" is used.
47+
#[arg(long, short)]
48+
pub format: Option<String>,
4149
}
4250

4351
/// Args for get command

src/variables.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use colored::Colorize;
21
use std::env;
32

43
use crate::{models::ErrorKind, utils::*};
@@ -7,9 +6,10 @@ use crate::{models::ErrorKind, utils::*};
76
type VariablesList = Vec<(String, String)>;
87

98
/// Print all environment variables
10-
pub fn print_env() {
9+
pub fn print_env(format: &str) {
1110
for (key, value) in get_variables() {
12-
println!("{} = \"{}\"", key.blue(), value);
11+
let entry = format.replace("{name}", &key).replace("{value}", &value);
12+
println!("{}", entry);
1313
}
1414
}
1515

@@ -88,7 +88,7 @@ mod tests {
8888
#[test]
8989
fn test_print_env() {
9090
unsafe { env::set_var("TEST_PRINT_VAR", "test_value") };
91-
print_env();
91+
print_env("{name} = \"{value}\"");
9292
unsafe { env::remove_var("TEST_PRINT_VAR") };
9393
}
9494

@@ -134,7 +134,7 @@ mod tests {
134134
unsafe { env::set_var("TEST_VAR_1", "value1") };
135135
unsafe { env::set_var("TEST_VAR_2", "value2") };
136136

137-
print_env();
137+
print_env("{name} = \"{value}\"");
138138

139139
// Clean up
140140
unsafe { env::remove_var("TEST_VAR_1") };
@@ -145,7 +145,7 @@ mod tests {
145145
fn test_print_env_empty_value() {
146146
unsafe { env::set_var("TEST_EMPTY", "") };
147147

148-
print_env();
148+
print_env("{name} = \"{value}\"");
149149

150150
unsafe { env::remove_var("TEST_EMPTY") };
151151
}
@@ -154,7 +154,7 @@ mod tests {
154154
fn test_print_env_special_characters() {
155155
unsafe { env::set_var("TEST_SPECIAL", "value with spaces and $#@!") };
156156

157-
print_env();
157+
print_env("{name} = \"{value}\"");
158158

159159
unsafe { env::remove_var("TEST_SPECIAL") };
160160
}

0 commit comments

Comments
 (0)