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
28 changes: 26 additions & 2 deletions src/dfx/src/commands/canister/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::commands::CliCommand;
use crate::lib::environment::Environment;
use crate::lib::environment::{ClientEnvironment, Environment};
use crate::lib::error::{DfxError, DfxResult};
use crate::lib::message::UserMessage;
use clap::{App, ArgMatches, SubCommand};
use clap::{App, Arg, ArgMatches, ArgSettings, SubCommand};

mod call;
mod install;
Expand All @@ -21,12 +21,36 @@ fn builtins() -> Vec<CliCommand> {
pub fn construct() -> App<'static, 'static> {
SubCommand::with_name("canister")
.about(UserMessage::ManageCanister.to_str())
.arg(
Arg::with_name("client")
.set(ArgSettings::Global)
.help(UserMessage::CanisterClient.to_str())
.long("client")
.validator(|v| {
reqwest::Url::parse(&v)
.map(|_| ())
.map_err(|_| "should be a valid URL.".to_string())
})
.takes_value(true),
)
.subcommands(builtins().into_iter().map(|x| x.get_subcommand().clone()))
}

pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
let subcommand = args.subcommand();

// Need storage for ClientEnvironment ownership.
let mut _client_env: Option<ClientEnvironment<'_>> = None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't you move this inside the branch? I guess it is a bit more readable (or does it complain that it does not move?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It reduces the depth of the code. You can add a commit to this branch if that’s blocking your review.

let env = if args.is_present("client") {
_client_env = Some(ClientEnvironment::new(
env,
args.value_of("client").expect("Could not find client."),
));
_client_env.as_ref().unwrap()
} else {
env
};

if let (name, Some(subcommand_args)) = subcommand {
match builtins().into_iter().find(|x| name == x.get_name()) {
Some(cmd) => cmd.execute(env, subcommand_args),
Expand Down
42 changes: 42 additions & 0 deletions src/dfx/src/lib/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,45 @@ impl Environment for EnvironmentImpl {
self.get_client()
}
}

pub struct ClientEnvironment<'a> {
backend: &'a dyn Environment,
client: Client,
}

impl<'a> ClientEnvironment<'a> {
pub fn new(backend: &'a dyn Environment, client_url: &str) -> Self {
ClientEnvironment {
backend,
client: Client::new(ClientConfig {
url: client_url.to_string(),
}),
}
}
}

impl<'a> Environment for ClientEnvironment<'a> {
fn get_cache(&self) -> Rc<dyn Cache> {
self.backend.get_cache()
}

fn get_config(&self) -> Option<Rc<Config>> {
self.backend.get_config()
}

fn is_in_project(&self) -> bool {
self.backend.is_in_project()
}

fn get_temp_dir(&self) -> &Path {
self.backend.get_temp_dir()
}

fn get_version(&self) -> &Version {
self.backend.get_version()
}

fn get_client(&self) -> Option<Client> {
Some(self.client.clone())
}
}
1 change: 1 addition & 0 deletions src/dfx/src/lib/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ user_message!(

// dfx canister mod
ManageCanister => "Manages canisters deployed on a network client.",
CanisterClient => "Override the client to connect to. By default uses the client set in dfx configuration.",

// dfx canister query
QueryCanister => "Sends a query request to a canister.",
Expand Down