|
| 1 | +use crate::lib::api_client::{install_code, Blob}; |
| 2 | +use crate::lib::env::{ClientEnv, ProjectConfigEnv}; |
| 3 | +use crate::lib::error::DfxResult; |
| 4 | +use clap::{App, Arg, ArgMatches, SubCommand}; |
| 5 | +use std::path::PathBuf; |
| 6 | +use tokio::runtime::Runtime; |
| 7 | + |
| 8 | +fn is_number(v: String) -> Result<(), String> { |
| 9 | + v.parse::<u64>() |
| 10 | + .map_err(|_| String::from("The value must be a number.")) |
| 11 | + .map(|_| ()) |
| 12 | +} |
| 13 | + |
| 14 | +pub fn construct() -> App<'static, 'static> { |
| 15 | + SubCommand::with_name("install") |
| 16 | + .about("Install a canister.") |
| 17 | + .arg( |
| 18 | + Arg::with_name("canister") |
| 19 | + .takes_value(true) |
| 20 | + .help("The canister ID (a number).") |
| 21 | + .required(true) |
| 22 | + .validator(is_number), |
| 23 | + ) |
| 24 | + .arg( |
| 25 | + Arg::with_name("wasm") |
| 26 | + .help("The wasm file to use.") |
| 27 | + .required(true), |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult |
| 32 | +where |
| 33 | + T: ClientEnv + ProjectConfigEnv, |
| 34 | +{ |
| 35 | + // Read the config. |
| 36 | + let config = env.get_config().unwrap(); |
| 37 | + let project_root = config.get_path().parent().unwrap(); |
| 38 | + |
| 39 | + let canister_id = args.value_of("canister").unwrap().parse::<u64>()?; |
| 40 | + let wasm_path = args.value_of("wasm").unwrap(); |
| 41 | + let wasm_path = PathBuf::from(project_root).join(wasm_path); |
| 42 | + |
| 43 | + let wasm = std::fs::read(wasm_path)?; |
| 44 | + let client = env.get_client(); |
| 45 | + |
| 46 | + let install = install_code(client, canister_id, Blob(wasm), None); |
| 47 | + |
| 48 | + let mut runtime = Runtime::new().expect("Unable to create a runtime"); |
| 49 | + runtime.block_on(install)?; |
| 50 | + |
| 51 | + Ok(()) |
| 52 | +} |
0 commit comments