Skip to content

Commit

Permalink
feat: add support for install arguments on deploy (#1087)
Browse files Browse the repository at this point in the history
* feat: add support for install arguments on deploy

* .

* .

* .

* Update e2e/bats/assets/greet_arg/greet.mo

Co-authored-by: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com>

* .

Co-authored-by: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com>
  • Loading branch information
Hans and chenyan-dfinity authored Oct 2, 2020
1 parent c187e35 commit b02f33d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
7 changes: 7 additions & 0 deletions e2e/bats/assets/greet_arg/greet.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
actor class Greet(name: Text) {

public query func greet() : async Text {
"Hello, " # name # "!"
}

}
1 change: 1 addition & 0 deletions e2e/bats/assets/greet_arg/patch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dfx config canisters/hello/main greet.mo
13 changes: 13 additions & 0 deletions e2e/bats/deploy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@ teardown() {
assert_match 'attempting install'
}

@test "dfx deploy supports arguments" {
dfx_new hello
install_asset greet_arg
dfx_start
assert_command dfx canister create --all

assert_command dfx deploy --argument '("World")'
assert_match 'attempting install'

assert_command dfx canister call hello greet
assert_match 'Hello, World'
}

19 changes: 18 additions & 1 deletion src/dfx/src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ pub fn construct() -> App<'static, 'static> {
.long("network")
.takes_value(true),
)
.arg(
Arg::with_name("argument")
.help(UserMessage::ArgumentValue.to_str())
.long("argument")
.takes_value(true),
)
.arg(
Arg::with_name("type")
.help(UserMessage::ArgumentType.to_str())
.long("type")
.takes_value(true)
.requires("argument")
.possible_values(&["idl", "raw"]),
)
}

pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
Expand All @@ -29,5 +43,8 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
let timeout = expiry_duration();
let canister = args.value_of("canister_name");

deploy_canisters(&env, canister, timeout)
let argument = args.value_of("argument");
let argument_type = args.value_of("type");

deploy_canisters(&env, canister, argument, argument_type, timeout)
}
13 changes: 12 additions & 1 deletion src/dfx/src/lib/operations/canister/deploy_canisters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::lib::models::canister::CanisterPool;
use crate::lib::models::canister_id_store::CanisterIdStore;
use crate::lib::operations::canister::create_canister;
use crate::lib::operations::canister::install_canister;
use crate::util::{blob_from_arguments, get_candid_init_type};
use ic_agent::AgentError;
use ic_utils::interfaces::management_canister::InstallMode;
use slog::{info, warn};
Expand All @@ -16,6 +17,8 @@ use tokio::runtime::Runtime;
pub fn deploy_canisters(
env: &dyn Environment,
some_canister: Option<&str>,
argument: Option<&str>,
argument_type: Option<&str>,
timeout: Duration,
) -> DfxResult {
let log = env.get_logger();
Expand All @@ -41,6 +44,8 @@ pub fn deploy_canisters(
&canister_names,
&initial_canister_id_store,
&config,
argument,
argument_type,
timeout,
)?;

Expand Down Expand Up @@ -92,6 +97,8 @@ fn install_canisters(
canister_names: &[String],
initial_canister_id_store: &CanisterIdStore,
config: &Config,
argument: Option<&str>,
argument_type: Option<&str>,
timeout: Duration,
) -> DfxResult {
info!(env.get_logger(), "Installing canisters...");
Expand All @@ -112,7 +119,11 @@ fn install_canisters(

let canister_id = canister_id_store.get(&canister_name)?;
let canister_info = CanisterInfo::load(&config, &canister_name, Some(canister_id))?;
let install_args = [];

let maybe_path = canister_info.get_output_idl_path();
let init_type = maybe_path.and_then(|path| get_candid_init_type(&path));
let install_args = blob_from_arguments(argument, argument_type, &init_type)?;

let compute_allocation = None;
let memory_allocation = None;
let result = runtime.block_on(install_canister(
Expand Down

0 comments on commit b02f33d

Please sign in to comment.