Skip to content

Commit

Permalink
[BEEEP] SM-1005 - Add env output option (#320)
Browse files Browse the repository at this point in the history
## 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 <[email protected]>
  • Loading branch information
tangowithfoxtrot and dani-garcia authored Nov 16, 2023
1 parent 39891e0 commit a55c2d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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# "))
}
})
.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

0 comments on commit a55c2d7

Please sign in to comment.