2024-11-10 19:40:39 -08:00
|
|
|
use kordophone::APIInterface;
|
|
|
|
|
use kordophone::api::http_client::HTTPAPIClient;
|
|
|
|
|
use kordophone::api::http_client::Credentials;
|
|
|
|
|
|
|
|
|
|
use dotenv;
|
2025-01-08 13:32:55 -08:00
|
|
|
use anyhow::Result;
|
2024-12-08 15:08:15 -08:00
|
|
|
use clap::Subcommand;
|
2024-11-10 19:40:39 -08:00
|
|
|
use crate::printers::ConversationPrinter;
|
|
|
|
|
|
2025-01-08 13:32:55 -08:00
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 15:08:15 -08:00
|
|
|
#[derive(Subcommand)]
|
|
|
|
|
pub enum Commands {
|
|
|
|
|
/// Prints all known conversations on the server.
|
|
|
|
|
Conversations,
|
|
|
|
|
|
|
|
|
|
/// Prints the server Kordophone version.
|
|
|
|
|
Version,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Commands {
|
2025-01-08 13:32:55 -08:00
|
|
|
pub async fn run(cmd: Commands) -> Result<()> {
|
2024-12-08 15:08:15 -08:00
|
|
|
let mut client = ClientCli::new();
|
|
|
|
|
match cmd {
|
|
|
|
|
Commands::Version => client.print_version().await,
|
|
|
|
|
Commands::Conversations => client.print_conversations().await,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ClientCli {
|
2024-11-10 19:40:39 -08:00
|
|
|
api: HTTPAPIClient,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ClientCli {
|
|
|
|
|
pub fn new() -> Self {
|
2025-01-08 13:32:55 -08:00
|
|
|
let api = make_api_client_from_env();
|
2024-11-10 19:40:39 -08:00
|
|
|
Self { api: api }
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 13:32:55 -08:00
|
|
|
pub async fn print_version(&mut self) -> Result<()> {
|
2024-11-10 19:40:39 -08:00
|
|
|
let version = self.api.get_version().await?;
|
|
|
|
|
println!("Version: {}", version);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-08 13:32:55 -08:00
|
|
|
pub async fn print_conversations(&mut self) -> Result<()> {
|
2024-11-10 19:40:39 -08:00
|
|
|
let conversations = self.api.get_conversations().await?;
|
|
|
|
|
for conversation in conversations {
|
2025-01-08 13:32:55 -08:00
|
|
|
println!("{}", ConversationPrinter::new(&conversation.into()));
|
2024-11-10 19:40:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|