Skip to content
Closed
Changes from 1 commit
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
70 changes: 23 additions & 47 deletions crates/icp-cli/src/commands/canister/settings/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ use byte_unit::{Byte, Unit};
use clap::{ArgAction, Args};
use ic_agent::{AgentError, export::Principal};
use ic_management_canister_types::{CanisterStatusResult, EnvironmentVariable, LogVisibility};
use icp::{agent, identity, network};
use icp::{
agent,
identity::{self, IdentitySelection},
network,
};

use crate::{
commands::Context,
commands::{Context, GetAgentForEnvError, GetCanisterIdForEnvError, GetEnvironmentError},
options::{EnvironmentOpt, IdentityOpt},
store_id::{Key, LookupError as LookupIdError},
store_id::LookupError as LookupIdError,
};

#[derive(Clone, Debug, Default, Args)]
Expand Down Expand Up @@ -119,21 +123,12 @@ pub(crate) enum CommandError {
#[error(transparent)]
Identity(#[from] identity::LoadError),

#[error("project does not contain an environment named '{name}'")]
EnvironmentNotFound { name: String },

#[error(transparent)]
Access(#[from] network::AccessError),

#[error(transparent)]
Agent(#[from] agent::CreateError),

#[error("environment '{environment}' does not include canister '{canister}'")]
EnvironmentCanister {
environment: String,
canister: String,
},

#[error("invalid environment variable '{variable}'")]
InvalidEnvironmentVariable { variable: String },

Expand All @@ -142,47 +137,27 @@ pub(crate) enum CommandError {

#[error(transparent)]
Update(#[from] AgentError),
}

pub(crate) async fn exec(ctx: &Context, args: &UpdateArgs) -> Result<(), CommandError> {
// Load project
let p = ctx.project.load().await?;

// Load identity
let id = ctx.identity.load(args.identity.clone().into()).await?;

// Load target environment
let env =
p.environments
.get(args.environment.name())
.ok_or(CommandError::EnvironmentNotFound {
name: args.environment.name().to_owned(),
})?;
#[error(transparent)]
GetAgentForEnv(#[from] GetAgentForEnvError),

// Access network
let access = ctx.network.access(&env.network).await?;
#[error(transparent)]
GetEnvironment(#[from] GetEnvironmentError),

// Agent
let agent = ctx.agent.create(id, &access.url).await?;
#[error(transparent)]
GetCanisterIdForEnv(#[from] GetCanisterIdForEnvError),
}

if let Some(k) = access.root_key {
agent.set_root_key(k);
}
pub(crate) async fn exec(ctx: &Context, args: &UpdateArgs) -> Result<(), CommandError> {
let identity_selection: IdentitySelection = args.identity.clone().into();
let environment = args.environment.name();

// Ensure canister is included in the environment
if !env.canisters.contains_key(&args.name) {
return Err(CommandError::EnvironmentCanister {
environment: env.name.to_owned(),
canister: args.name.to_owned(),
});
}
// Agent
let agent = ctx
.get_agent_for_env(&identity_selection, environment)
.await?;

// Lookup the canister id
let cid = ctx.ids.lookup(&Key {
network: env.network.name.to_owned(),
environment: env.name.to_owned(),
canister: args.name.to_owned(),
})?;
let cid = ctx.get_canister_id_for_env(&args.name, environment).await?;

// Management Interface
let mgmt = ic_utils::interfaces::ManagementCanister::create(&agent);
Expand Down Expand Up @@ -292,6 +267,7 @@ fn log_visibility_parser(log_visibility: &str) -> Result<LogVisibility, String>
}
}

#[allow(clippy::result_large_err)]
fn environment_variable_parser(env_var: &str) -> Result<EnvironmentVariable, CommandError> {
let (name, value) =
env_var
Expand Down