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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
# Local (directories)
.icp

# This directory contains canister ID mappings which are encouraged
# to be included in source control of icp canister projects.
# They are ignored here because we are developing icp-cli itself.
.icpdata

# pocket-ic binary
pocket-ic
2 changes: 1 addition & 1 deletion crates/icp-cli/src/commands/canister/binding_env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub(crate) async fn exec(ctx: &Context, args: &BindingArgs) -> Result<(), Comman
// Check that all the canisters in this environment have an id
// We need to have all the ids to generate environment variables
// for the bindings
let canisters_with_ids: HashSet<&String> = canister_list.iter().map(|(n, _p)| n).collect();
let canisters_with_ids: HashSet<&String> = canister_list.keys().collect();
debug!("Canisters with ids: {:?}", canisters_with_ids);

let missing_canisters: Vec<String> = env
Expand Down
21 changes: 4 additions & 17 deletions crates/icp-cli/src/commands/canister/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
options::{EnvironmentOpt, IdentityOpt},
progress::{ProgressManager, ProgressManagerSettings},
};
use icp::store_id::{Key, LookupIdError, RegisterError};
use icp::store_id::{LookupIdError, RegisterError};

pub(crate) const DEFAULT_CANISTER_CYCLES: u128 = 2 * TRILLION;

Expand Down Expand Up @@ -175,14 +175,7 @@ pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), Command
let cexist: Vec<_> = env
.canisters
.values()
.filter_map(|(_, c)| {
ctx.ids
.lookup(&Key {
environment: env.name.to_owned(),
canister: c.name.to_owned(),
})
.ok()
})
.filter_map(|(_, c)| ctx.ids.lookup(&env.name, &c.name).ok())
.collect();

// Agent
Expand Down Expand Up @@ -240,13 +233,7 @@ pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), Command
// Indicate to user that the canister is created
pb.set_message("Creating...");

// Create canister-environment association-key
let k = Key {
environment: env_ref.name.to_owned(),
canister: name.to_string(),
};

match ctx.ids.lookup(&k) {
match ctx.ids.lookup(&env_ref.name, name) {
// Exists (skip)
Ok(principal) => {
return Err(CommandError::CanisterExists { principal });
Expand Down Expand Up @@ -323,7 +310,7 @@ pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), Command
};

// Register the canister ID
ctx.ids.register(&k, &cid)?;
ctx.ids.register(&env_ref.name, name, cid)?;

Ok::<_, CommandError>(())
}
Expand Down
7 changes: 2 additions & 5 deletions crates/icp-cli/src/commands/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
options::{EnvironmentOpt, IdentityOpt},
progress::{ProgressManager, ProgressManagerSettings},
};
use icp::store_id::{Key, LookupIdError};
use icp::store_id::LookupIdError;

#[derive(Args, Debug)]
pub(crate) struct SyncArgs {
Expand Down Expand Up @@ -128,10 +128,7 @@ pub(crate) async fn exec(ctx: &Context, args: &SyncArgs) -> Result<(), CommandEr
let mut pb = progress_manager.create_multi_step_progress_bar(&c.name, "Sync");

// Get canister principal ID
let cid = ctx.ids.lookup(&Key {
environment: env.name.to_owned(),
canister: c.name.to_owned(),
})?;
let cid = ctx.ids.lookup(&env.name, &c.name)?;

// Create an async closure that handles the sync process for this specific canister
let fut = {
Expand Down
2 changes: 1 addition & 1 deletion crates/icp-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Cli {
)]
project_dir: Option<PathBuf>,

#[arg(long, default_value = ".icp/ids.json")]
#[arg(long, default_value = ".icpdata/")]
id_store: PathBuf,

#[arg(long, default_value = ".icp/artifacts")]
Expand Down
6 changes: 6 additions & 0 deletions crates/icp-cli/tests/canister_create_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ fn canister_create() {
.args(["canister", "create", "--environment", "my-environment"])
.assert()
.success();

let id_mapping_path = project_dir.join(".icpdata").join("my-environment.ids.json");
assert!(
id_mapping_path.exists(),
"ID mapping file should exist at {id_mapping_path}"
);
}

#[test]
Expand Down
9 changes: 2 additions & 7 deletions crates/icp/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use snafu::{OptionExt, ResultExt, Snafu};

mod init;

use crate::store_id::Key;

pub use init::initialize;

/// Selection type for networks - similar to IdentitySelection
Expand Down Expand Up @@ -175,10 +173,7 @@ impl Context {
// Lookup the canister id
let cid = self
.ids
.lookup(&Key {
environment: env.name.to_owned(),
canister: canister_name.to_owned(),
})
.lookup(&env.name, canister_name)
.context(CanisterIdLookupSnafu {
canister_name: canister_name.to_owned(),
environment_name: environment.name().to_owned(),
Expand Down Expand Up @@ -336,7 +331,7 @@ impl Context {
Context {
term: Term::stderr(),
dirs: Arc::new(crate::directories::UnimplementedMockDirs),
ids: Arc::new(crate::store_id::MockInMemoryIdStore::new()),
ids: Arc::new(crate::store_id::mock::MockInMemoryIdStore::new()),
artifacts: Arc::new(crate::store_artifact::MockInMemoryArtifactStore::new()),
project: Arc::new(crate::MockProjectLoader::minimal()),
identity: Arc::new(crate::identity::MockIdentityLoader::anonymous()),
Expand Down
26 changes: 5 additions & 21 deletions crates/icp/src/context/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::store_id::MockInMemoryIdStore;
use crate::store_id::mock::MockInMemoryIdStore;
use crate::{MockProjectLoader, identity::MockIdentityLoader, network::MockNetworkAccessor};

#[tokio::test]
Expand Down Expand Up @@ -115,22 +115,14 @@ async fn test_get_network_not_found() {

#[tokio::test]
async fn test_get_canister_id_for_env_success() {
use crate::store_id::{Access as IdAccess, Key};
use crate::store_id::Access as IdAccess;
use candid::Principal;

let ids_store = Arc::new(MockInMemoryIdStore::new());

// Register a canister ID for the dev environment
let canister_id = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
ids_store
.register(
&Key {
environment: "dev".to_string(),
canister: "backend".to_string(),
},
&canister_id,
)
.unwrap();
ids_store.register("dev", "backend", canister_id).unwrap();

let ctx = Context {
project: Arc::new(MockProjectLoader::complex()),
Expand Down Expand Up @@ -364,22 +356,14 @@ async fn test_get_agent_for_url_success() {

#[tokio::test]
async fn test_get_canister_id() {
use crate::store_id::{Access as IdAccess, Key};
use crate::store_id::Access as IdAccess;
use candid::Principal;

let ids_store = Arc::new(MockInMemoryIdStore::new());

// Register a canister ID for the dev environment
let canister_id = Principal::from_text("rrkah-fqaaa-aaaaa-aaaaq-cai").unwrap();
ids_store
.register(
&Key {
environment: "dev".to_string(),
canister: "backend".to_string(),
},
&canister_id,
)
.unwrap();
ids_store.register("dev", "backend", canister_id).unwrap();

let ctx = Context {
project: Arc::new(MockProjectLoader::complex()),
Expand Down
Loading