Skip to content

Commit

Permalink
Use the shared bitwarden-cli in bws (#705)
Browse files Browse the repository at this point in the history
Now that we are publishing the `bitwarden-cli` crate we should start
using the shared functionality in the `bws` crate. And continually
extract common code out from it.
  • Loading branch information
Hinton authored Apr 11, 2024
1 parent b4205cc commit e7b6c1d
Show file tree
Hide file tree
Showing 5 changed files with 16 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.

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

impl Color {
/**
* Evaluate if colors are supported
*/
pub fn is_enabled(self) -> bool {
match self {
Color::No => false,
Expand All @@ -17,6 +20,9 @@ impl Color {
}
}

/**
* Installs color_eyre, if Color is disabled we use an empty theme to 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;

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)?;

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,
) {
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() {
bat::PrettyPrinter::new()
.input_from_bytes(data.as_bytes())
.language(language)
Expand Down

0 comments on commit e7b6c1d

Please sign in to comment.