Skip to content

Commit

Permalink
Use the shared bitwarden-cli in bws
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Apr 11, 2024
1 parent 975ac0c commit 0d093b8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 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.

3 changes: 3 additions & 0 deletions crates/bitwarden-cli/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ impl Color {
}
}

/**
* Conditionally install color_eyre. If colors are disabled we also disable error colors.
*/
pub fn install_color_eyre(color: Color) -> color_eyre::Result<(), color_eyre::Report> {
if color.is_enabled() {
color_eyre::install()
Expand Down
1 change: 1 addition & 0 deletions crates/bws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bat = { version = "0.24.0", features = [
"regex-onig",
], default-features = false }
bitwarden = { workspace = true, features = ["secrets"] }
bitwarden-cli = { workspace = true }
chrono = { version = "0.4.35", features = [
"clock",
"std",
Expand Down
14 changes: 4 additions & 10 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use bitwarden::{
},
},
};
use bitwarden_cli::{install_color_eyre, Color};
use clap::{ArgGroup, CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use color_eyre::eyre::{bail, Result};
Expand All @@ -24,7 +25,7 @@ mod render;
mod state;

use config::ProfileKey;
use render::{serialize_response, Color, Output};
use render::{serialize_response, Output};
use uuid::Uuid;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -236,16 +237,9 @@ const SERVER_URL_KEY_VAR_NAME: &str = "BWS_SERVER_URL";
#[allow(clippy::comparison_chain)]
async fn process_commands() -> Result<()> {
let cli = Cli::parse();
let color = cli.color;

Check warning on line 240 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L240

Added line #L240 was not covered by tests

let color = cli.color.is_enabled();
if color {
color_eyre::install()?;
} else {
// Use an empty theme to disable error coloring
color_eyre::config::HookBuilder::new()
.theme(color_eyre::config::Theme::new())
.install()?;
}
install_color_eyre(color)?;

Check warning on line 242 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L242

Added line #L242 was not covered by tests

let Some(command) = cli.command else {
let mut cmd = Cli::command();
Expand Down
24 changes: 4 additions & 20 deletions crates/bws/src/render.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bitwarden::secrets_manager::{projects::ProjectResponse, secrets::SecretResponse};
use bitwarden_cli::Color;
use chrono::{DateTime, Utc};
use clap::ValueEnum;
use comfy_table::Table;
Expand All @@ -15,29 +16,12 @@ pub(crate) enum Output {
None,
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
pub(crate) enum Color {
No,
Yes,
Auto,
}

impl Color {
pub(crate) fn is_enabled(self) -> bool {
match self {
Color::No => false,
Color::Yes => true,
Color::Auto => supports_color::on(supports_color::Stream::Stdout).is_some(),
}
}
}

const ASCII_HEADER_ONLY: &str = " -- ";

pub(crate) fn serialize_response<T: Serialize + TableSerialize<N>, const N: usize>(
data: T,
output: Output,
color: bool,
color: Color,

Check warning on line 24 in crates/bws/src/render.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/render.rs#L24

Added line #L24 was not covered by tests
) {
match output {
Output::JSON => {
Expand Down Expand Up @@ -101,8 +85,8 @@ pub(crate) fn serialize_response<T: Serialize + TableSerialize<N>, const N: usiz
}
}

fn pretty_print(language: &str, data: &str, color: bool) {
if color {
fn pretty_print(language: &str, data: &str, color: Color) {
if color.is_enabled() {

Check warning on line 89 in crates/bws/src/render.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/render.rs#L88-L89

Added lines #L88 - L89 were not covered by tests
bat::PrettyPrinter::new()
.input_from_bytes(data.as_bytes())
.language(language)
Expand Down

0 comments on commit 0d093b8

Please sign in to comment.