-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showcase how state can be used in the cli
- Loading branch information
Showing
12 changed files
with
304 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::sync::Arc; | ||
|
||
use bitwarden_db::{params, Database, DatabaseError, DatabaseTrait}; | ||
use tokio::sync::Mutex; | ||
|
||
pub struct SettingsRepository { | ||
db: Arc<Mutex<Database>>, | ||
} | ||
|
||
impl SettingsRepository { | ||
pub fn new(db: Arc<Mutex<Database>>) -> Self { | ||
Self { db: db.clone() } | ||
} | ||
|
||
pub async fn get(&self, key: &str) -> Result<Option<String>, DatabaseError> { | ||
let guard = self.db.lock().await; | ||
|
||
let res = guard | ||
.query_map( | ||
"SELECT value FROM settings WHERE key = ?1", | ||
[key], | ||
|row| -> Result<String, _> { row.get(0) }, | ||
) | ||
.await? | ||
.first() | ||
.map(|x| x.to_owned()); | ||
|
||
Ok(res) | ||
} | ||
|
||
pub async fn set(&self, key: &str, value: &str) -> Result<(), DatabaseError> { | ||
let guard = self.db.lock().await; | ||
|
||
guard | ||
.execute( | ||
"INSERT OR REPLACE INTO settings (key, value) VALUES (?1, ?2)", | ||
params![key, value], | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub(crate) mod vault; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use bitwarden::{ | ||
auth::login::AuthSettings, | ||
mobile::crypto::{InitUserCryptoMethod, InitUserCryptoRequest}, | ||
vault::{CipherListView, ClientVaultExt}, | ||
Client, Error, | ||
}; | ||
use bitwarden_cli::Color; | ||
|
||
use clap::Subcommand; | ||
|
||
use crate::render::{serialize_response, Output, OutputSettings, TableSerialize}; | ||
|
||
#[derive(Subcommand, Clone)] | ||
pub(crate) enum VaultCommands { | ||
Get { id: String }, | ||
List {}, | ||
Create {}, | ||
} | ||
|
||
pub(crate) async fn process_command( | ||
command: VaultCommands, | ||
client: Client, | ||
password: Option<String>, | ||
) -> Result<(), Error> { | ||
// TODO: This should be moved into the SDK | ||
let setting = client | ||
.platform() | ||
.settings_repository | ||
.get("auth") | ||
.await | ||
.unwrap() | ||
.unwrap(); | ||
let setting = serde_json::from_str::<AuthSettings>(&setting)?; | ||
|
||
client | ||
.crypto() | ||
.initialize_user_crypto(InitUserCryptoRequest { | ||
kdf_params: setting.kdf, | ||
email: setting.email, | ||
private_key: setting.private_key, | ||
method: InitUserCryptoMethod::Password { | ||
password: password.unwrap(), | ||
user_key: setting.user_key, | ||
}, | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
match command { | ||
VaultCommands::Get { id } => todo!(), | ||
VaultCommands::List {} => { | ||
let ciphers = client.vault().cipher_repository.get_all().await.unwrap(); | ||
|
||
let dec = client.vault().ciphers().decrypt_list(ciphers)?; | ||
|
||
/*for cipher in dec { | ||
println!("{}", cipher.name); | ||
}*/ | ||
|
||
let output_settings = OutputSettings::new(Output::Table, Color::Auto); | ||
serialize_response(dec, output_settings); | ||
|
||
Ok(()) | ||
} | ||
VaultCommands::Create {} => todo!(), | ||
} | ||
} | ||
|
||
impl TableSerialize<2> for CipherListView { | ||
fn get_headers() -> [&'static str; 2] { | ||
["ID", "Name"] | ||
} | ||
|
||
fn get_values(&self) -> Vec<[String; 2]> { | ||
vec![[self.id.unwrap_or_default().to_string(), self.name.clone()]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.