Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cli/golem-cli/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::config::{
ApplicationEnvironmentConfig, AuthenticationConfig, AuthenticationConfigWithSource,
AuthenticationSource, OAuth2AuthenticationConfig, OAuth2AuthenticationData,
};
use crate::error::service::AnyhowMapServiceError;
use crate::error::service::{MapServiceError, ServiceError};
use crate::log::LogColorize;
use anyhow::{Context, anyhow, bail};
use colored::Colorize;
Expand Down Expand Up @@ -165,15 +165,16 @@ impl Auth {

let client = MeClientLive { context };

client.current_login_token().await.map_service_error()
Ok(client.current_login_token().await.map_service_error()?)
}

async fn start_oauth2(&self) -> anyhow::Result<OAuth2WebflowData> {
info!("Start OAuth2 workflow");
self.login_client
Ok(self
.login_client
.start_oauth_2_webflow(&OAuth2Provider::Github, Some("https://golem.cloud"))
.await
.map_service_error()
.map_service_error()?)
}

async fn complete_oauth2(
Expand All @@ -200,7 +201,7 @@ impl Auth {

sleep(delay).await;
}
_ => return Err(err).map_service_error(),
_ => return Err(ServiceError::from(err).into()),
},
}
}
Expand Down
101 changes: 90 additions & 11 deletions cli/golem-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,13 +887,26 @@ pub mod shared_args {
}

impl PostDeployArgs {
fn effective_action(&self, env_args: &PostDeployArgs) -> PostDeployAction {
if let Some(update_mode) = self.update_agents {
PostDeployAction::Update(update_mode)
} else if self.reset {
PostDeployAction::Reset
} else if self.redeploy_agents {
PostDeployAction::Redeploy
} else if let Some(update_mode) = env_args.update_agents {
PostDeployAction::Update(update_mode)
} else if env_args.reset {
PostDeployAction::Reset
} else if env_args.redeploy_agents {
PostDeployAction::Redeploy
} else {
PostDeployAction::None
}
}

pub fn is_any_set(&self, env_args: &PostDeployArgs) -> bool {
env_args.update_agents.is_some()
|| env_args.redeploy_agents
|| env_args.reset
|| self.update_agents.is_some()
|| self.redeploy_agents
|| self.reset
!matches!(self.effective_action(env_args), PostDeployAction::None)
}

pub fn none() -> Self {
Expand All @@ -905,16 +918,33 @@ pub mod shared_args {
}

pub fn delete_agents(&self, env_args: &PostDeployArgs) -> bool {
(env_args.reset || self.reset) && !self.redeploy_agents && self.update_agents.is_none()
matches!(self.effective_action(env_args), PostDeployAction::Reset)
}

pub fn allow_incompatible_changes(&self, env_args: &PostDeployArgs) -> bool {
matches!(self.effective_action(env_args), PostDeployAction::Reset)
}

pub fn update_agents_mode(&self, env_args: &PostDeployArgs) -> Option<AgentUpdateMode> {
match self.effective_action(env_args) {
PostDeployAction::Update(update_mode) => Some(update_mode),
_ => None,
}
}

pub fn redeploy_agents(&self, env_args: &PostDeployArgs) -> bool {
(env_args.redeploy_agents || self.redeploy_agents)
&& !self.reset
&& self.update_agents.is_none()
matches!(self.effective_action(env_args), PostDeployAction::Redeploy)
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum PostDeployAction {
None,
Update(AgentUpdateMode),
Redeploy,
Reset,
}

#[derive(Debug, Args)]
pub struct AccountIdOptionalArg {
/// Account ID
Expand Down Expand Up @@ -1911,12 +1941,13 @@ pub fn help_target_to_command(target: ShowClapHelpTarget) -> Command {

#[cfg(test)]
mod test {

use crate::command::shared_args::PostDeployArgs;
use crate::command::{
GolemCliCommand, GolemCliSubcommand, builtin_exec_subcommands,
help_target_to_subcommand_names,
};
use crate::error::ShowClapHelpTarget;
use crate::model::worker::AgentUpdateMode;
use clap::builder::StyledStr;
use clap::{Command, CommandFactory};
use itertools::Itertools;
Expand Down Expand Up @@ -2200,4 +2231,52 @@ mod test {
}
}
}

fn post_deploy_args(
update_agents: Option<AgentUpdateMode>,
redeploy_agents: bool,
reset: bool,
) -> PostDeployArgs {
PostDeployArgs {
update_agents,
redeploy_agents,
reset,
}
}

#[test]
fn post_deploy_args_cli_update_mode_overrides_env_reset() {
let cli_args = post_deploy_args(Some(AgentUpdateMode::Manual), false, false);
let env_args = post_deploy_args(None, false, true);

assert_eq!(
cli_args.update_agents_mode(&env_args),
Some(AgentUpdateMode::Manual)
);
assert!(!cli_args.delete_agents(&env_args));
assert!(!cli_args.redeploy_agents(&env_args));
assert!(!cli_args.allow_incompatible_changes(&env_args));
}

#[test]
fn post_deploy_args_env_reset_enables_delete_and_incompatible_changes() {
let cli_args = PostDeployArgs::none();
let env_args = post_deploy_args(None, false, true);

assert_eq!(cli_args.update_agents_mode(&env_args), None);
assert!(cli_args.delete_agents(&env_args));
assert!(cli_args.allow_incompatible_changes(&env_args));
assert!(!cli_args.redeploy_agents(&env_args));
}

#[test]
fn post_deploy_args_env_redeploy_does_not_delete_agents() {
let cli_args = PostDeployArgs::none();
let env_args = post_deploy_args(None, true, false);

assert_eq!(cli_args.update_agents_mode(&env_args), None);
assert!(cli_args.redeploy_agents(&env_args));
assert!(!cli_args.delete_agents(&env_args));
assert!(!cli_args.allow_incompatible_changes(&env_args));
}
}
7 changes: 4 additions & 3 deletions cli/golem-cli/src/command_handler/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::command::account::AccountSubcommand;
use crate::command_handler::Handlers;
use crate::context::Context;
use crate::error::NonSuccessfulExit;
use crate::error::service::AnyhowMapServiceError;
use crate::error::service::MapServiceError;
use crate::log::log_error;
use crate::log::log_warn_action;
use crate::model::text::account::{AccountGetView, AccountNewView};
Expand Down Expand Up @@ -138,13 +138,14 @@ impl AccountCommandHandler {
}

async fn get(&self, account_id: Option<AccountId>) -> anyhow::Result<Account> {
self.ctx
Ok(self
.ctx
.golem_clients()
.await?
.account
.get_account(&self.select_account_id_or_err(account_id).await?.0)
.await
.map_service_error()
.map_service_error()?)
}

pub async fn account_id_or_err(&self) -> anyhow::Result<AccountId> {
Expand Down
8 changes: 4 additions & 4 deletions cli/golem-cli/src/command_handler/api/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::command::api::deployment::ApiDeploymentSubcommand;
use crate::command_handler::Handlers;
use crate::context::Context;
use crate::error::service::{AnyhowMapServiceError, ServiceError};
use crate::error::service::{MapServiceError, ServiceError};
use crate::log::{LogColorize, LogIndent, log_action, log_warn_action};
use crate::model::environment::{EnvironmentResolveMode, ResolvedEnvironmentIdentity};
use crate::model::http_api::{HttpApiDeploymentDeployProperties, McpDeploymentDeployProperties};
Expand Down Expand Up @@ -130,11 +130,11 @@ impl ApiDeploymentCommandHandler {
return Ok(Some(deployment));
};

clients
Ok(clients
.api_deployment
.get_http_api_deployment_revision(&deployment.id.0, (*revision).into())
.await
.map_service_error_not_found_as_opt()
.map_service_error_not_found_as_opt()?)
},
)
.await
Expand Down Expand Up @@ -206,7 +206,7 @@ impl ApiDeploymentCommandHandler {
)
.await
.map_service_error()
.map_err(Arc::new)
.map_err(|err| Arc::new(err.into()))
}
})
.await
Expand Down
7 changes: 4 additions & 3 deletions cli/golem-cli/src/command_handler/api/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::command_handler::Handlers;
use crate::context::Context;
use crate::error::service::AnyhowMapServiceError;
use crate::error::service::MapServiceError;
use crate::model::text::http_api_domain::{DomainRegistrationNewView, HttpApiDomainListView};

use crate::command::api::domain::ApiDomainSubcommand;
Expand Down Expand Up @@ -185,7 +185,8 @@ impl ApiDomainCommandHandler {
environment_id: &EnvironmentId,
domain: &Domain,
) -> anyhow::Result<DomainRegistration> {
self.ctx
Ok(self
.ctx
.golem_clients()
.await?
.api_domain
Expand All @@ -196,7 +197,7 @@ impl ApiDomainCommandHandler {
},
)
.await
.map_service_error()
.map_service_error()?)
}

async fn list_domains(
Expand Down
2 changes: 1 addition & 1 deletion cli/golem-cli/src/command_handler/api/security_scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::command::api::security_scheme::ApiSecuritySchemeSubcommand;
use crate::command_handler::Handlers;
use crate::context::Context;
use crate::error::NonSuccessfulExit;
use crate::error::service::AnyhowMapServiceError;
use crate::error::service::MapServiceError;
use crate::log::log_error;
use crate::model::environment::EnvironmentResolveMode;
use crate::model::text::http_api_security::{
Expand Down
2 changes: 1 addition & 1 deletion cli/golem-cli/src/command_handler/api_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use crate::command::api_token::ApiTokenSubcommand;
use crate::command_handler::Handlers;
use crate::context::Context;
use crate::error::service::AnyhowMapServiceError;
use crate::error::service::MapServiceError;
use crate::log::{LogColorize, log_warn_action};
use crate::model::text::token::{TokenListView, TokenNewView};
use chrono::{DateTime, Utc};
Expand Down
Loading
Loading