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

[BEEEP] SM-1005 - Add env output option #320

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ thiserror = "1.0.40"
tokio = { version = "1.28.2", features = ["rt-multi-thread", "macros"] }
toml = "0.8.0"
uuid = { version = "^1.3.3", features = ["serde"] }
regex = { version = "1.10.2", features=["std", "perf"], default-features=false }

bitwarden = { path = "../bitwarden", version = "0.3.1", features = ["secrets"] }

Expand Down
26 changes: 26 additions & 0 deletions crates/bws/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Serialize;
pub(crate) enum Output {
JSON,
YAML,
Env,
Table,
TSV,
None,
Expand Down Expand Up @@ -49,6 +50,31 @@ pub(crate) fn serialize_response<T: Serialize + TableSerialize<N>, const N: usiz
let text = serde_yaml::to_string(&data).unwrap();
pretty_print("yaml", &text, color);
}
Output::Env => {
let valid_key_regex = regex::Regex::new("^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap();

let mut commented_out = false;
let mut text: Vec<String> = data
.get_values()
.into_iter()
.map(|row| {
if valid_key_regex.is_match(&row[1]) {
format!("{}=\"{}\"", row[1], row[2])
} else {
commented_out = true;
format!("# {}=\"{}\"", row[1], row[2].replace("\n", "\n# "))
tangowithfoxtrot marked this conversation as resolved.
Show resolved Hide resolved
}
})
.collect();

if commented_out {
text.push(String::from(
"\n# one or more secrets have been commented-out due to a problematic key name",
));
}

pretty_print("sh", &format!("{}\n", text.join("\n")), color);
}
Output::Table => {
let mut table = Table::new();
table
Expand Down
Loading