Skip to content

Commit

Permalink
Adapted Questions subcommand to work with new --api option
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Oct 16, 2024
1 parent 01cf848 commit c02d644
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rust/agama-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub async fn run_command(cli: Cli) -> Result<(), ServiceError> {
let manager = build_manager().await?;
install(&manager, 3).await?
}
Commands::Questions(subcommand) => run_questions_cmd(subcommand).await?,
Commands::Questions(subcommand) => run_questions_cmd(client, subcommand).await?,
// TODO: logs command was originally designed with idea that agama's cli and agama
// installation runs on the same machine, so it is unable to do remote connection
Commands::Logs(subcommand) => run_logs_cmd(subcommand).await?,
Expand Down
16 changes: 8 additions & 8 deletions rust/agama-cli/src/questions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use agama_lib::proxies::Questions1Proxy;
use agama_lib::questions::http_client::HTTPClient;
use agama_lib::{connection, error::ServiceError};
use agama_lib::{base_http_client::BaseHTTPClient, connection, error::ServiceError};
use clap::{Args, Subcommand, ValueEnum};

// TODO: use for answers also JSON to be consistent
Expand Down Expand Up @@ -74,8 +74,8 @@ async fn set_answers(proxy: Questions1Proxy<'_>, path: String) -> Result<(), Ser
.map_err(|e| e.into())
}

async fn list_questions() -> Result<(), ServiceError> {
let client = HTTPClient::new()?;
async fn list_questions(client: BaseHTTPClient) -> Result<(), ServiceError> {
let client = HTTPClient::new(client)?;
let questions = client.list_questions().await?;
// FIXME: if performance is bad, we can skip converting json from http to struct and then
// serialize it, but it won't be pretty string
Expand All @@ -85,8 +85,8 @@ async fn list_questions() -> Result<(), ServiceError> {
Ok(())
}

async fn ask_question() -> Result<(), ServiceError> {
let client = HTTPClient::new()?;
async fn ask_question(client: BaseHTTPClient) -> Result<(), ServiceError> {
let client = HTTPClient::new(client)?;
let question = serde_json::from_reader(std::io::stdin())?;

let created_question = client.create_question(&question).await?;
Expand All @@ -104,14 +104,14 @@ async fn ask_question() -> Result<(), ServiceError> {
Ok(())
}

pub async fn run(subcommand: QuestionsCommands) -> Result<(), ServiceError> {
pub async fn run(client: BaseHTTPClient, subcommand: QuestionsCommands) -> Result<(), ServiceError> {
let connection = connection().await?;
let proxy = Questions1Proxy::new(&connection).await?;

match subcommand {
QuestionsCommands::Mode(value) => set_mode(proxy, value.value).await,
QuestionsCommands::Answers { path } => set_answers(proxy, path).await,
QuestionsCommands::List => list_questions().await,
QuestionsCommands::Ask => ask_question().await,
QuestionsCommands::List => list_questions(client).await,
QuestionsCommands::Ask => ask_question(client).await,
}
}
4 changes: 2 additions & 2 deletions rust/agama-lib/src/questions/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub struct HTTPClient {
}

impl HTTPClient {
pub fn new() -> Result<Self, ServiceError> {
pub fn new(client: BaseHTTPClient) -> Result<Self, ServiceError> {
Ok(Self {
client: BaseHTTPClient::default().authenticated()?,
client: client,
})
}

Expand Down

0 comments on commit c02d644

Please sign in to comment.