-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from dfinity-lab/hansl/canister-install
New Feature: dfx canister install
- Loading branch information
Showing
13 changed files
with
334 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ wabt = "0.9.2" | |
[dev-dependencies] | ||
env_logger = "0.6" | ||
mockito = "0.20.0" | ||
tempfile = "3.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::lib::api_client::{install_code, Blob}; | ||
use crate::lib::env::{ClientEnv, ProjectConfigEnv}; | ||
use crate::lib::error::DfxResult; | ||
use clap::{App, Arg, ArgMatches, SubCommand}; | ||
use std::path::PathBuf; | ||
use tokio::runtime::Runtime; | ||
|
||
fn is_number(v: String) -> Result<(), String> { | ||
v.parse::<u64>() | ||
.map_err(|_| String::from("The value must be a number.")) | ||
.map(|_| ()) | ||
} | ||
|
||
pub fn construct() -> App<'static, 'static> { | ||
SubCommand::with_name("install") | ||
.about("Install a canister.") | ||
.arg( | ||
Arg::with_name("canister") | ||
.takes_value(true) | ||
.help("The canister ID (a number).") | ||
.required(true) | ||
.validator(is_number), | ||
) | ||
.arg( | ||
Arg::with_name("wasm") | ||
.help("The wasm file to use.") | ||
.required(true), | ||
) | ||
} | ||
|
||
pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
// Read the config. | ||
let config = env.get_config().unwrap(); | ||
let project_root = config.get_path().parent().unwrap(); | ||
|
||
let canister_id = args.value_of("canister").unwrap().parse::<u64>()?; | ||
let wasm_path = args.value_of("wasm").unwrap(); | ||
let wasm_path = PathBuf::from(project_root).join(wasm_path); | ||
|
||
let wasm = std::fs::read(wasm_path)?; | ||
let client = env.get_client(); | ||
|
||
let install = install_code(client, canister_id, Blob(wasm), None); | ||
|
||
let mut runtime = Runtime::new().expect("Unable to create a runtime"); | ||
runtime.block_on(install)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::commands::CliCommand; | ||
use crate::lib::env::{ClientEnv, ProjectConfigEnv}; | ||
use crate::lib::error::{DfxError, DfxResult}; | ||
use clap::{App, ArgMatches, SubCommand}; | ||
|
||
mod install; | ||
|
||
fn builtins<T>() -> Vec<CliCommand<T>> | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
vec![CliCommand::new(install::construct(), install::exec)] | ||
} | ||
|
||
pub fn construct<T>() -> App<'static, 'static> | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
SubCommand::with_name("canister") | ||
.about("Manage canisters from a network.") | ||
.subcommands( | ||
builtins::<T>() | ||
.into_iter() | ||
.map(|x| x.get_subcommand().clone()), | ||
) | ||
} | ||
|
||
pub fn exec<T>(env: &T, args: &ArgMatches<'_>) -> DfxResult | ||
where | ||
T: ClientEnv + ProjectConfigEnv, | ||
{ | ||
let subcommand = args.subcommand(); | ||
|
||
if let (name, Some(subcommand_args)) = subcommand { | ||
match builtins().into_iter().find(|x| name == x.get_name()) { | ||
Some(cmd) => cmd.execute(env, subcommand_args), | ||
None => Err(DfxError::UnknownCommand(format!( | ||
"Command {} not found.", | ||
name | ||
))), | ||
} | ||
} else { | ||
construct::<T>().write_help(&mut std::io::stderr())?; | ||
println!(); | ||
println!(); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
pub mod cache; | ||
pub mod dfinity; | ||
|
||
pub const DFX_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
static mut DFX_VERSION: Option<String> = None; | ||
/// Returns the version of DFX that was built. | ||
/// In debug, add a timestamp of the upstream compilation at the end of version to ensure all | ||
/// debug runs are unique (and cached uniquely). | ||
/// That timestamp is taken from the DFX_TIMESTAMP_DEBUG_MODE_ONLY env var that is set in | ||
/// Nix. | ||
pub fn dfx_version() -> &'static str { | ||
unsafe { | ||
match &DFX_VERSION { | ||
Some(x) => x.as_str(), | ||
None => { | ||
let version = env!("CARGO_PKG_VERSION"); | ||
|
||
#[cfg(debug_assertions)] | ||
{ | ||
DFX_VERSION = Some(format!( | ||
"{}-{}", | ||
version, | ||
std::env::var("DFX_TIMESTAMP_DEBUG_MODE_ONLY") | ||
.unwrap_or_else(|_| "local-debug".to_owned()) | ||
)); | ||
} | ||
|
||
dfx_version() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.