Skip to content

Commit d59dbfc

Browse files
committedJan 30, 2025·
feat(rust): show env values in environment command
1 parent c7a3bdc commit d59dbfc

File tree

3 files changed

+89
-20
lines changed

3 files changed

+89
-20
lines changed
 

‎implementations/rust/ockam/ockam_command/src/branding/compile_env_vars.rs

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ const COMPILE_CONTROLLER_IDENTIFIER: &str = env!("COMPILE_OCKAM_CONTROLLER_IDENT
2525
/// in the format `command1=customName,command2,command3`
2626
const COMPILE_COMMANDS: &str = env!("COMPILE_OCKAM_COMMANDS");
2727

28+
pub fn get_compile_time_vars() -> Vec<(&'static str, &'static str)> {
29+
vec![
30+
(COMPILE_OCKAM_DEVELOPER, COMPILE_DEVELOPER),
31+
(COMPILE_OCKAM_HOME, COMPILE_HOME),
32+
(COMPILE_OCKAM_COMMAND_BIN_NAME, COMPILE_BIN_NAME),
33+
(COMPILE_OCKAM_COMMAND_BRAND_NAME, COMPILE_BRAND_NAME),
34+
(COMPILE_OCKAM_COMMAND_SUPPORT_EMAIL, COMPILE_SUPPORT_EMAIL),
35+
(COMPILE_OCKAM_CONTROLLER_ADDRESS, COMPILE_CONTROLLER_ADDRESS),
36+
(
37+
COMPILE_OCKAM_CONTROLLER_IDENTIFIER,
38+
COMPILE_CONTROLLER_IDENTIFIER,
39+
),
40+
(COMPILE_OCKAM_COMMANDS, COMPILE_COMMANDS),
41+
]
42+
}
43+
2844
pub fn load_compile_time_vars() {
2945
// If OCKAM_DEVELOPER is not set, set it to the COMPILE_OCKAM_DEVELOPER value
3046
if get_env_ignore_error::<bool>(ockam_api::logs::env_variables::OCKAM_DEVELOPER).is_none() {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use crate::{docs, pager};
12
use clap::Args;
2-
3-
use crate::docs;
3+
use ockam_api::colors::{color_primary, color_warn};
4+
use ockam_api::fmt_log;
5+
use ockam_api::output::Output;
6+
use ockam_api::terminal::{INDENTATION, PADDING};
47

58
const ENV_INFO: &str = include_str!("./static/env_info.txt");
69

@@ -10,11 +13,46 @@ pub struct EnvironmentCommand {}
1013

1114
impl EnvironmentCommand {
1215
pub fn run(self) -> miette::Result<()> {
13-
println!("{}", ENV_INFO);
16+
pager::render_output(&format!("{}\n\n{}", Self::info(), Self::values()));
1417
Ok(())
1518
}
1619

1720
pub fn name(&self) -> String {
18-
"show environment variables".to_string()
21+
"environment".to_string()
22+
}
23+
24+
fn info() -> String {
25+
ENV_INFO.padded_display()
26+
}
27+
28+
fn values() -> String {
29+
// get all the env vars and filter those containing OCKAM in their name
30+
let runtime_vars = std::env::vars()
31+
.filter(|(k, _)| k.contains("OCKAM"))
32+
.map(|(k, v)| {
33+
format!(
34+
"{}{}{}={}",
35+
PADDING,
36+
INDENTATION,
37+
color_primary(k),
38+
color_warn(v)
39+
)
40+
})
41+
.collect::<Vec<String>>()
42+
.join("\n");
43+
let compile_vars = crate::branding::compile_env_vars::get_compile_time_vars()
44+
.iter()
45+
.map(|(k, v)| {
46+
format!(
47+
"{}{}{}={}",
48+
PADDING,
49+
INDENTATION,
50+
color_primary(k),
51+
color_warn(v)
52+
)
53+
})
54+
.collect::<Vec<String>>()
55+
.join("\n");
56+
fmt_log!("Values:\n{}\n{}", runtime_vars, compile_vars)
1957
}
2058
}

‎implementations/rust/ockam/ockam_command/src/pager.rs

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::Write;
22
use std::path::PathBuf;
33
use std::process;
4-
use std::process::Stdio;
4+
use std::process::{ExitStatus, Stdio};
55

66
use console::Term;
77
use miette::IntoDiagnostic;
@@ -10,20 +10,20 @@ use ockam_core::env::get_env_with_default;
1010

1111
use crate::util::exitcode;
1212

13-
pub fn render_help(help: clap::Error) {
13+
pub fn render_output(s: &str) {
1414
let pager = get_env_with_default("PAGER", "less".to_string()).expect("Invalid PAGER value");
1515
match which::which(pager) {
1616
Ok(pager_binary_path) => {
17-
paginate_with(pager_binary_path, help).expect("Failed to paginate help");
17+
paginate_with(pager_binary_path, s).expect("Failed to paginate output");
1818
}
19-
// The pager binary was not found, so we just print the help without pagination
19+
// The pager binary was not found, so we just print the output without pagination
2020
Err(_) => {
21-
help.exit();
21+
println!("{}", s);
2222
}
2323
}
2424
}
2525

26-
fn paginate_with(pager_binary_path: PathBuf, help: clap::Error) -> miette::Result<()> {
26+
fn paginate_with(pager_binary_path: PathBuf, s: &str) -> miette::Result<ExitStatus> {
2727
let pager = pager_binary_path.file_name().unwrap().to_string_lossy();
2828
let mut pager_cmd = process::Command::new(pager.as_ref());
2929
if pager.as_ref() == "less" {
@@ -39,18 +39,33 @@ fn paginate_with(pager_binary_path: PathBuf, help: clap::Error) -> miette::Resul
3939
.stdin
4040
.take()
4141
.expect("Failed to get pager's stdin");
42-
// Strip ANSI escape sequences if stdout is not a TTY (e.g. when piping to another command)
43-
let rendered_text = if Term::stdout().is_term() {
44-
help.render().ansi().to_string()
45-
} else {
46-
help.render().to_string()
47-
};
4842
// Write the rendered text to the pager's stdin
49-
pager_stdin
50-
.write_all(rendered_text.as_bytes())
51-
.into_diagnostic()?;
43+
pager_stdin.write_all(s.as_bytes()).into_diagnostic()?;
5244
}
53-
let _ = pager_process.wait();
45+
pager_process.wait().into_diagnostic()
46+
}
47+
48+
pub fn render_help(help: clap::Error) {
49+
let pager = get_env_with_default("PAGER", "less".to_string()).expect("Invalid PAGER value");
50+
match which::which(pager) {
51+
Ok(pager_binary_path) => {
52+
paginate_help_with(pager_binary_path, help).expect("Failed to paginate help");
53+
}
54+
// The pager binary was not found, so we just print the help without pagination
55+
Err(_) => {
56+
help.exit();
57+
}
58+
}
59+
}
60+
61+
fn paginate_help_with(pager_binary_path: PathBuf, help: clap::Error) -> miette::Result<()> {
62+
// Strip ANSI escape sequences if stdout is not a TTY (e.g. when piping to another command)
63+
let rendered_text = if Term::stdout().is_term() {
64+
help.render().ansi().to_string()
65+
} else {
66+
help.render().to_string()
67+
};
68+
paginate_with(pager_binary_path, &rendered_text)?;
5469
let code = if help.use_stderr() {
5570
exitcode::USAGE
5671
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.