Private
Public Access
1
0

kpcli: adds 'db' subcommand for interacting with the database

This commit is contained in:
2025-01-08 13:32:55 -08:00
parent 89f8d21ebb
commit 793faab721
10 changed files with 197 additions and 29 deletions

View File

@@ -3,9 +3,28 @@ use kordophone::api::http_client::HTTPAPIClient;
use kordophone::api::http_client::Credentials;
use dotenv;
use anyhow::Result;
use clap::Subcommand;
use crate::printers::ConversationPrinter;
pub fn make_api_client_from_env() -> HTTPAPIClient {
dotenv::dotenv().ok();
// read from env
let base_url = std::env::var("KORDOPHONE_API_URL")
.expect("KORDOPHONE_API_URL must be set");
let credentials = Credentials {
username: std::env::var("KORDOPHONE_USERNAME")
.expect("KORDOPHONE_USERNAME must be set"),
password: std::env::var("KORDOPHONE_PASSWORD")
.expect("KORDOPHONE_PASSWORD must be set"),
};
HTTPAPIClient::new(base_url.parse().unwrap(), credentials.into())
}
#[derive(Subcommand)]
pub enum Commands {
/// Prints all known conversations on the server.
@@ -16,7 +35,7 @@ pub enum Commands {
}
impl Commands {
pub async fn run(cmd: Commands) -> Result<(), Box<dyn std::error::Error>> {
pub async fn run(cmd: Commands) -> Result<()> {
let mut client = ClientCli::new();
match cmd {
Commands::Version => client.print_version().await,
@@ -31,34 +50,20 @@ struct ClientCli {
impl ClientCli {
pub fn new() -> Self {
dotenv::dotenv().ok();
// read from env
let base_url = std::env::var("KORDOPHONE_API_URL")
.expect("KORDOPHONE_API_URL must be set");
let credentials = Credentials {
username: std::env::var("KORDOPHONE_USERNAME")
.expect("KORDOPHONE_USERNAME must be set"),
password: std::env::var("KORDOPHONE_PASSWORD")
.expect("KORDOPHONE_PASSWORD must be set"),
};
let api = HTTPAPIClient::new(base_url.parse().unwrap(), credentials.into());
let api = make_api_client_from_env();
Self { api: api }
}
pub async fn print_version(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub async fn print_version(&mut self) -> Result<()> {
let version = self.api.get_version().await?;
println!("Version: {}", version);
Ok(())
}
pub async fn print_conversations(&mut self) -> Result<(), Box<dyn std::error::Error>> {
pub async fn print_conversations(&mut self) -> Result<()> {
let conversations = self.api.get_conversations().await?;
for conversation in conversations {
println!("{}", ConversationPrinter::new(&conversation));
println!("{}", ConversationPrinter::new(&conversation.into()));
}
Ok(())