diff --git a/src/dfx/src/commands/canister/create.rs b/src/dfx/src/commands/canister/create.rs index 18daf3f104..9954edff2b 100644 --- a/src/dfx/src/commands/canister/create.rs +++ b/src/dfx/src/commands/canister/create.rs @@ -1,15 +1,9 @@ use crate::lib::environment::Environment; use crate::lib::error::{DfxError, DfxResult}; use crate::lib::message::UserMessage; -use crate::lib::models::canister_id_store::CanisterIdStore; -use crate::lib::progress_bar::ProgressBar; -use crate::lib::provider::get_network_context; -use crate::lib::waiter::create_waiter; +use crate::lib::operations::canister::create_canister; use clap::{App, Arg, ArgMatches, SubCommand}; -use ic_agent::ManagementCanister; -use std::format; -use tokio::runtime::Runtime; pub fn construct() -> App<'static, 'static> { SubCommand::with_name("create") @@ -30,55 +24,6 @@ pub fn construct() -> App<'static, 'static> { ) } -fn create_canister(env: &dyn Environment, canister_name: &str) -> DfxResult { - let message = format!("Creating canister {:?}...", canister_name); - let b = ProgressBar::new_spinner(&message); - - env.get_config() - .ok_or(DfxError::CommandMustBeRunInAProject)?; - - let mgr = ManagementCanister::new( - env.get_agent() - .ok_or(DfxError::CommandMustBeRunInAProject)?, - ); - let mut runtime = Runtime::new().expect("Unable to create a runtime"); - - let mut canister_id_store = CanisterIdStore::for_env(env)?; - - let network_name = get_network_context()?; - - let non_default_network = if network_name == "local" { - format!("") - } else { - format!("on network {:?} ", network_name) - }; - - match canister_id_store.find(&canister_name) { - Some(canister_id) => { - let message = format!( - "{:?} canister was already created {}and has canister id: {:?}", - canister_name, - non_default_network, - canister_id.to_text() - ); - b.finish_with_message(&message); - Ok(()) - } - None => { - let cid = runtime.block_on(mgr.create_canister(create_waiter()))?; - let canister_id = cid.to_text(); - let message = format!( - "{:?} canister created {}with canister id: {:?}", - canister_name, non_default_network, canister_id - ); - b.finish_with_message(&message); - canister_id_store.add(&canister_name, canister_id) - } - }?; - - Ok(()) -} - pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult { let config = env .get_config() diff --git a/src/dfx/src/commands/canister/install.rs b/src/dfx/src/commands/canister/install.rs index 64bb01dbfb..79217af9b5 100644 --- a/src/dfx/src/commands/canister/install.rs +++ b/src/dfx/src/commands/canister/install.rs @@ -1,14 +1,12 @@ use crate::lib::canister_info::CanisterInfo; use crate::lib::environment::Environment; use crate::lib::error::{DfxError, DfxResult}; -use crate::lib::installers::assets::post_install_store_assets; use crate::lib::message::UserMessage; use crate::lib::models::canister_id_store::CanisterIdStore; -use crate::lib::waiter::create_waiter; +use crate::lib::operations::canister::install_canister; use clap::{App, Arg, ArgMatches, SubCommand}; -use ic_agent::{Agent, CanisterAttributes, ComputeAllocation, InstallMode, ManagementCanister}; -use slog::info; +use ic_agent::{ComputeAllocation, InstallMode}; use std::convert::TryFrom; use std::str::FromStr; use tokio::runtime::Runtime; @@ -55,49 +53,6 @@ pub fn construct() -> App<'static, 'static> { ) } -async fn install_canister( - env: &dyn Environment, - agent: &Agent, - canister_info: &CanisterInfo, - compute_allocation: Option, - mode: InstallMode, -) -> DfxResult { - let mgr = ManagementCanister::new(agent); - let log = env.get_logger(); - let canister_id = canister_info.get_canister_id().map_err(|_| { - DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned()) - })?; - - info!( - log, - "Installing code for canister {}, with canister_id {}", - canister_info.get_name(), - canister_id.to_text(), - ); - - let wasm_path = canister_info - .get_output_wasm_path() - .expect("Cannot get WASM output path."); - let wasm = std::fs::read(wasm_path)?; - - mgr.install_code( - create_waiter(), - &canister_id, - mode, - &wasm, - &[], - &CanisterAttributes { compute_allocation }, - ) - .await - .map_err(DfxError::from)?; - - if canister_info.get_type() == "assets" { - post_install_store_assets(&canister_info, &agent).await?; - } - - Ok(()) -} - fn compute_allocation_validator(compute_allocation: String) -> Result<(), String> { if let Ok(num) = compute_allocation.parse::() { if num <= 100 { diff --git a/src/dfx/src/lib/mod.rs b/src/dfx/src/lib/mod.rs index ecc3b37fc6..801bea92be 100644 --- a/src/dfx/src/lib/mod.rs +++ b/src/dfx/src/lib/mod.rs @@ -10,6 +10,7 @@ pub mod logger; pub mod message; pub mod models; pub mod network; +pub mod operations; pub mod package_arguments; pub mod progress_bar; pub mod provider; diff --git a/src/dfx/src/lib/operations/canister/create_canister.rs b/src/dfx/src/lib/operations/canister/create_canister.rs new file mode 100644 index 0000000000..2d2f47f82b --- /dev/null +++ b/src/dfx/src/lib/operations/canister/create_canister.rs @@ -0,0 +1,59 @@ +use crate::lib::environment::Environment; +use crate::lib::error::{DfxError, DfxResult}; +use crate::lib::models::canister_id_store::CanisterIdStore; +use crate::lib::progress_bar::ProgressBar; +use crate::lib::provider::get_network_context; +use crate::lib::waiter::create_waiter; + +use ic_agent::ManagementCanister; +use std::format; +use tokio::runtime::Runtime; + +pub fn create_canister(env: &dyn Environment, canister_name: &str) -> DfxResult { + let message = format!("Creating canister {:?}...", canister_name); + let b = ProgressBar::new_spinner(&message); + + env.get_config() + .ok_or(DfxError::CommandMustBeRunInAProject)?; + + let mgr = ManagementCanister::new( + env.get_agent() + .ok_or(DfxError::CommandMustBeRunInAProject)?, + ); + let mut runtime = Runtime::new().expect("Unable to create a runtime"); + + let mut canister_id_store = CanisterIdStore::for_env(env)?; + + let network_name = get_network_context()?; + + let non_default_network = if network_name == "local" { + format!("") + } else { + format!("on network {:?} ", network_name) + }; + + match canister_id_store.find(&canister_name) { + Some(canister_id) => { + let message = format!( + "{:?} canister was already created {}and has canister id: {:?}", + canister_name, + non_default_network, + canister_id.to_text() + ); + b.finish_with_message(&message); + Ok(()) + } + None => { + let cid = runtime.block_on(mgr.create_canister(create_waiter()))?; + let canister_id = cid.to_text(); + let message = format!( + "{:?} canister created {}with canister id: {:?}", + canister_name, non_default_network, canister_id + ); + b.finish_with_message(&message); + canister_id_store.add(&canister_name, canister_id) + } + }?; + + Ok(()) +} diff --git a/src/dfx/src/lib/operations/canister/install_canister.rs b/src/dfx/src/lib/operations/canister/install_canister.rs new file mode 100644 index 0000000000..455cea56b9 --- /dev/null +++ b/src/dfx/src/lib/operations/canister/install_canister.rs @@ -0,0 +1,51 @@ +use crate::lib::canister_info::CanisterInfo; +use crate::lib::environment::Environment; +use crate::lib::error::{DfxError, DfxResult}; +use crate::lib::installers::assets::post_install_store_assets; +use crate::lib::waiter::create_waiter; + +use ic_agent::{Agent, CanisterAttributes, ComputeAllocation, InstallMode, ManagementCanister}; +use slog::info; + +pub async fn install_canister( + env: &dyn Environment, + agent: &Agent, + canister_info: &CanisterInfo, + compute_allocation: Option, + mode: InstallMode, +) -> DfxResult { + let mgr = ManagementCanister::new(agent); + let log = env.get_logger(); + let canister_id = canister_info.get_canister_id().map_err(|_| { + DfxError::CannotFindBuildOutputForCanister(canister_info.get_name().to_owned()) + })?; + + info!( + log, + "Installing code for canister {}, with canister_id {}", + canister_info.get_name(), + canister_id.to_text(), + ); + + let wasm_path = canister_info + .get_output_wasm_path() + .expect("Cannot get WASM output path."); + let wasm = std::fs::read(wasm_path)?; + + mgr.install_code( + create_waiter(), + &canister_id, + mode, + &wasm, + &[], + &CanisterAttributes { compute_allocation }, + ) + .await + .map_err(DfxError::from)?; + + if canister_info.get_type() == "assets" { + post_install_store_assets(&canister_info, &agent).await?; + } + + Ok(()) +} diff --git a/src/dfx/src/lib/operations/canister/mod.rs b/src/dfx/src/lib/operations/canister/mod.rs new file mode 100644 index 0000000000..0c8c5d56c1 --- /dev/null +++ b/src/dfx/src/lib/operations/canister/mod.rs @@ -0,0 +1,5 @@ +mod create_canister; +mod install_canister; + +pub use create_canister::create_canister; +pub use install_canister::install_canister; diff --git a/src/dfx/src/lib/operations/mod.rs b/src/dfx/src/lib/operations/mod.rs new file mode 100644 index 0000000000..1d28d2499e --- /dev/null +++ b/src/dfx/src/lib/operations/mod.rs @@ -0,0 +1 @@ +pub mod canister;