Skip to content
Closed
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
21 changes: 2 additions & 19 deletions crates/icp-cli/src/commands/canister/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)
}
Expand Down
39 changes: 38 additions & 1 deletion crates/icp-cli/src/store_id.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 },
}
Expand Down Expand Up @@ -101,6 +113,31 @@ impl IdStore {
Ok(())
}

pub(crate) fn resolve(
&self,
canister: &args::Canister,
environment: &Environment,
) -> Result<Principal, LookupError> {
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<Principal, LookupError> {
// Lock ID Store
let _g = self.lock.lock().expect("failed to acquire id store lock");
Expand Down
Loading