From ebdb241dafc478f751ec7fe503cbb8c9f63a3f0f Mon Sep 17 00:00:00 2001 From: Vivienne Date: Tue, 21 Oct 2025 10:06:47 +0200 Subject: [PATCH] alternative cid resolution --- crates/icp-cli/src/commands/canister/start.rs | 21 +--------- crates/icp-cli/src/store_id.rs | 39 ++++++++++++++++++- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/crates/icp-cli/src/commands/canister/start.rs b/crates/icp-cli/src/commands/canister/start.rs index eebd0bc5..df280b02 100644 --- a/crates/icp-cli/src/commands/canister/start.rs +++ b/crates/icp-cli/src/commands/canister/start.rs @@ -108,11 +108,6 @@ pub(crate) async fn exec(ctx: &Context, args: &StartArgs) -> Result<(), CommandE } Mode::Project(pdir) => { - // Argument (Canister) - let args::Canister::Name(name) = &args.canister else { - return Err(CommandError::Args); - }; - // Argument (Environment) let args::Environment::Name(env) = args.environment.clone().unwrap_or_default(); @@ -138,20 +133,8 @@ pub(crate) async fn exec(ctx: &Context, args: &StartArgs) -> Result<(), CommandE agent.set_root_key(k); } - // Ensure canister is included in the environment - if !env.canisters.contains_key(name) { - return Err(CommandError::EnvironmentCanister { - environment: env.name.to_owned(), - canister: name.to_owned(), - }); - } - - // Lookup the canister id - let cid = ctx.ids.lookup(&Key { - network: env.network.name.to_owned(), - environment: env.name.to_owned(), - canister: name.to_owned(), - })?; + // Argument (Canister) + let cid = ctx.ids.resolve(&args.canister, env)?; (agent, cid) } diff --git a/crates/icp-cli/src/store_id.rs b/crates/icp-cli/src/store_id.rs index a07439c6..a4584de9 100644 --- a/crates/icp-cli/src/store_id.rs +++ b/crates/icp-cli/src/store_id.rs @@ -1,10 +1,12 @@ use std::{io::ErrorKind, sync::Mutex}; use ic_agent::export::Principal; -use icp::{fs::json, prelude::*}; +use icp::{Environment, fs::json, prelude::*}; use serde::{Deserialize, Serialize}; use snafu::{ResultExt, Snafu}; +use crate::commands::args; + /// An association-key, used for associating an existing canister to an ID on a network #[derive(Clone, Debug, Serialize, Deserialize)] pub(crate) struct Key { @@ -48,6 +50,16 @@ pub(crate) enum LookupError { ))] IdNotFound { key: Key }, + #[snafu(display( + "could not find canister '{}' in environment '{}'", + canister, + environment + ))] + EnvironmentCanister { + canister: String, + environment: String, + }, + #[snafu(display("could not find canisters in environment '{}'", name))] EnvironmentNotFound { name: String }, } @@ -101,6 +113,31 @@ impl IdStore { Ok(()) } + pub(crate) fn resolve( + &self, + canister: &args::Canister, + environment: &Environment, + ) -> Result { + match canister { + args::Canister::Name(name) => { + if environment.canisters.contains_key(name) { + let key = Key { + network: environment.network.name.to_owned(), + environment: environment.name.to_owned(), + canister: name.to_owned(), + }; + self.lookup(&key) + } else { + return Err(LookupError::EnvironmentCanister { + environment: environment.name.to_owned(), + canister: name.to_owned(), + }); + } + } + args::Canister::Principal(principal) => Ok(principal.to_owned()), + } + } + pub(crate) fn lookup(&self, key: &Key) -> Result { // Lock ID Store let _g = self.lock.lock().expect("failed to acquire id store lock");