From a55c2d7733be6ac49b259950301b4b423f3cb4c2 Mon Sep 17 00:00:00 2001 From: tangowithfoxtrot <5676771+tangowithfoxtrot@users.noreply.github.com> Date: Thu, 16 Nov 2023 12:25:30 -0800 Subject: [PATCH] [BEEEP] SM-1005 - Add env output option (#320) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Type of change - [ ] Bug fix - [x] New feature development - [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc) - [ ] Build/deploy pipeline (DevOps) - [ ] Other ## Objective Add an env output option to `bws`. This allows for easier command-line usage in scripts, particularly where a JSON or YAML parser is not desirable or available. Basic usage examples in Bash: - `source <(bws secret get ec9e0489-244e-4b3f-8782-b0a800fe562f -o env)` - `bws secret list -o env > .env` ## Code changes - **`crates/bws/src/render.rs`:** Output secrets in `key="value"` format ## Screenshots ![image](https://github.com/bitwarden/sdk/assets/5676771/72201357-dc4c-471e-b1f0-011297dbb978) ## Before you submit - Please add **unit tests** where it makes sense to do so (encouraged but not required) --------- Co-authored-by: Daniel GarcĂ­a --- Cargo.lock | 1 + crates/bws/Cargo.toml | 1 + crates/bws/src/render.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 184066772..6a02ad594 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -539,6 +539,7 @@ dependencies = [ "env_logger", "log", "openssl", + "regex", "serde", "serde_json", "serde_yaml", diff --git a/crates/bws/Cargo.toml b/crates/bws/Cargo.toml index 00ec40da7..9ccd5c546 100644 --- a/crates/bws/Cargo.toml +++ b/crates/bws/Cargo.toml @@ -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"] } diff --git a/crates/bws/src/render.rs b/crates/bws/src/render.rs index 4f2a32e9a..83c5cd532 100644 --- a/crates/bws/src/render.rs +++ b/crates/bws/src/render.rs @@ -9,6 +9,7 @@ use serde::Serialize; pub(crate) enum Output { JSON, YAML, + Env, Table, TSV, None, @@ -49,6 +50,31 @@ pub(crate) fn serialize_response, 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 = 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# ")) + } + }) + .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