Skip to content

Commit 3d43277

Browse files
authored
chore: consequently use Selections (#186)
1 parent 4c2e0ef commit 3d43277

File tree

27 files changed

+315
-250
lines changed

27 files changed

+315
-250
lines changed

crates/icp-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ tiny-bip39.workspace = true
5353
tokio.workspace = true
5454
tracing-subscriber.workspace = true
5555
tracing.workspace = true
56+
url.workspace = true
5657

5758
[dev-dependencies]
5859
assert_cmd = "2"
@@ -65,7 +66,6 @@ predicates = "3"
6566
rand.workspace = true
6667
serde_yaml.workspace = true
6768
serial_test = { version = "3.2.0", features = ["file_locks"] }
68-
url.workspace = true
6969
uuid.workspace = true
7070

7171
[lints]

crates/icp-cli/src/commands/args.rs

Lines changed: 10 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@ use clap::Args;
55
use icp::context::{CanisterSelection, EnvironmentSelection, NetworkSelection};
66
use icp::identity::IdentitySelection;
77

8-
use crate::options::IdentityOpt;
8+
use crate::options::{EnvironmentOpt, IdentityOpt, NetworkOpt};
99

1010
#[derive(Args, Debug)]
1111
pub(crate) struct CanisterEnvironmentArgs {
1212
/// Name or principal of canister to target
1313
/// When using a name an environment must be specified
1414
pub(crate) canister: Canister,
1515

16-
/// Name of the target environment
17-
#[arg(long)]
18-
pub(crate) environment: Option<Environment>,
16+
#[command(flatten)]
17+
pub(crate) environment: EnvironmentOpt,
1918
}
2019

2120
impl CanisterEnvironmentArgs {
2221
/// Convert arguments into selection enums for canister and environment
2322
pub(crate) fn selections(&self) -> (CanisterSelection, EnvironmentSelection) {
2423
let canister_selection: CanisterSelection = self.canister.clone().into();
25-
let environment_selection: EnvironmentSelection =
26-
self.environment.clone().unwrap_or_default().into();
24+
let environment_selection: EnvironmentSelection = self.environment.clone().into();
2725
(canister_selection, environment_selection)
2826
}
2927
}
@@ -35,15 +33,12 @@ pub(crate) struct CanisterCommandArgs {
3533
/// When using a name an environment must be specified
3634
pub(crate) canister: Canister,
3735

38-
/// Name of the network to target, conflicts with environment argument
39-
#[arg(long, conflicts_with = "environment")]
40-
pub(crate) network: Option<Network>,
36+
#[command(flatten)]
37+
pub(crate) network: NetworkOpt,
4138

42-
/// Name of the target environment
43-
#[arg(long)]
44-
pub(crate) environment: Option<Environment>,
39+
#[command(flatten)]
40+
pub(crate) environment: EnvironmentOpt,
4541

46-
/// The identity to use for this request
4742
#[command(flatten)]
4843
pub(crate) identity: IdentityOpt,
4944
}
@@ -60,12 +55,8 @@ impl CanisterCommandArgs {
6055
/// Convert command arguments into selection enums
6156
pub(crate) fn selections(&self) -> CommandSelections {
6257
let canister_selection: CanisterSelection = self.canister.clone().into();
63-
let environment_selection: EnvironmentSelection =
64-
self.environment.clone().unwrap_or_default().into();
65-
let network_selection: NetworkSelection = match self.network.clone() {
66-
Some(network) => network.into_selection(),
67-
None => NetworkSelection::FromEnvironment,
68-
};
58+
let environment_selection: EnvironmentSelection = self.environment.clone().into();
59+
let network_selection: NetworkSelection = self.network.clone().into();
6960
let identity_selection: IdentitySelection = self.identity.clone().into();
7061

7162
CommandSelections {
@@ -111,73 +102,6 @@ impl Display for Canister {
111102
}
112103
}
113104

114-
#[derive(Clone, Debug, PartialEq)]
115-
pub(crate) enum Network {
116-
Name(String),
117-
Url(String),
118-
}
119-
120-
impl From<&str> for Network {
121-
fn from(v: &str) -> Self {
122-
if v.starts_with("http://") || v.starts_with("https://") {
123-
return Self::Url(v.to_string());
124-
}
125-
126-
Self::Name(v.to_string())
127-
}
128-
}
129-
130-
impl Network {
131-
pub(crate) fn into_selection(self) -> NetworkSelection {
132-
match self {
133-
Network::Name(name) => NetworkSelection::Named(name),
134-
Network::Url(url) => NetworkSelection::Url(url),
135-
}
136-
}
137-
}
138-
139-
#[derive(Clone, Debug, PartialEq)]
140-
pub(crate) enum Environment {
141-
Name(String),
142-
Default(String),
143-
}
144-
145-
impl Environment {
146-
pub(crate) fn name(&self) -> &str {
147-
match self {
148-
Environment::Name(name) => name,
149-
Environment::Default(name) => name,
150-
}
151-
}
152-
}
153-
154-
impl Default for Environment {
155-
fn default() -> Self {
156-
Self::Default("local".to_string())
157-
}
158-
}
159-
160-
impl From<&str> for Environment {
161-
fn from(v: &str) -> Self {
162-
Self::Name(v.to_string())
163-
}
164-
}
165-
166-
impl From<Environment> for EnvironmentSelection {
167-
fn from(v: Environment) -> Self {
168-
match v {
169-
Environment::Name(name) => EnvironmentSelection::Named(name),
170-
Environment::Default(_) => EnvironmentSelection::Default,
171-
}
172-
}
173-
}
174-
175-
impl Display for Environment {
176-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177-
write!(f, "{}", self.name())
178-
}
179-
}
180-
181105
#[cfg(test)]
182106
mod tests {
183107
use candid::Principal;
@@ -201,22 +125,4 @@ mod tests {
201125
Canister::Principal(Principal::from_text(cid).expect("failed to parse principal")),
202126
);
203127
}
204-
205-
#[test]
206-
fn network_by_name() {
207-
assert_eq!(
208-
Network::from("my-network"),
209-
Network::Name("my-network".to_string()),
210-
);
211-
}
212-
213-
#[test]
214-
fn network_by_url_http() {
215-
let url = "http://www.example.com";
216-
217-
assert_eq!(
218-
Network::from(url),
219-
Network::Url("http://www.example.com".to_string()),
220-
);
221-
}
222128
}

crates/icp-cli/src/commands/canister/binding_env_vars.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use ic_agent::AgentError;
1010
use ic_utils::interfaces::management_canister::builders::EnvironmentVariable;
1111
use icp::{
1212
agent,
13-
context::{GetAgentForEnvError, GetCanisterIdForEnvError, GetEnvironmentError},
13+
context::{
14+
EnvironmentSelection, GetAgentForEnvError, GetCanisterIdForEnvError, GetEnvironmentError,
15+
},
1416
identity, network,
1517
};
1618
use tracing::debug;
@@ -48,7 +50,7 @@ pub(crate) enum CommandError {
4850
Access(#[from] network::AccessError),
4951

5052
#[error(transparent)]
51-
Agent(#[from] agent::CreateError),
53+
Agent(#[from] agent::CreateAgentError),
5254

5355
#[error("Could not find canister id(s) for '{}' in environment '{environment}' make sure they are created first", canister_names.join(", "))]
5456
CanisterNotCreated {
@@ -76,12 +78,14 @@ pub(crate) enum CommandError {
7678
}
7779

7880
pub(crate) async fn exec(ctx: &Context, args: &BindingArgs) -> Result<(), CommandError> {
81+
let environment_selection: EnvironmentSelection = args.environment.clone().into();
82+
7983
// Load target environment
80-
let env = ctx.get_environment(args.environment.name()).await?;
84+
let env = ctx.get_environment(&environment_selection).await?;
8185

8286
// Agent
8387
let agent = ctx
84-
.get_agent_for_env(&args.identity.clone().into(), args.environment.name())
88+
.get_agent_for_env(&args.identity.clone().into(), &environment_selection)
8589
.await?;
8690

8791
let target_canisters = match args.names.is_empty() {
@@ -91,9 +95,11 @@ pub(crate) async fn exec(ctx: &Context, args: &BindingArgs) -> Result<(), Comman
9195

9296
let env_canisters = &env.canisters;
9397
let canisters = try_join_all(target_canisters.into_iter().map(|name| {
94-
let env_name = args.environment.name();
98+
let environment_selection = environment_selection.clone();
9599
async move {
96-
let cid = ctx.get_canister_id_for_env(&name, env_name).await?;
100+
let cid = ctx
101+
.get_canister_id_for_env(&name, &environment_selection)
102+
.await?;
97103
let (_, info) = env_canisters
98104
.get(&name)
99105
.expect("Canister id exists but no canister info");

crates/icp-cli/src/commands/canister/create.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use futures::{StreamExt, stream::FuturesOrdered};
77
use ic_agent::{Agent, AgentError, export::Principal};
88
use icp::{
99
agent,
10-
context::{GetAgentForEnvError, GetEnvironmentError},
10+
context::{EnvironmentSelection, GetAgentForEnvError, GetEnvironmentError},
1111
identity, network,
1212
prelude::*,
1313
};
@@ -94,7 +94,7 @@ pub(crate) enum CommandError {
9494
Access(#[from] network::AccessError),
9595

9696
#[error(transparent)]
97-
Agent(#[from] agent::CreateError),
97+
Agent(#[from] agent::CreateAgentError),
9898

9999
#[error("project does not contain a canister named '{name}'")]
100100
CanisterNotFound { name: String },
@@ -137,11 +137,13 @@ pub(crate) enum CommandError {
137137
// The cycles ledger will take cycles out of the user's account, and attaches them to a call to CMC::create_canister.
138138
// The CMC will then pick a subnet according to the user's preferences and permissions, and create a canister on that subnet.
139139
pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), CommandError> {
140+
let environment_selection: EnvironmentSelection = args.environment.clone().into();
141+
140142
// Load project
141143
let p = ctx.project.load().await?;
142144

143145
// Load target environment
144-
let env = ctx.get_environment(args.environment.name()).await?;
146+
let env = ctx.get_environment(&environment_selection).await?;
145147

146148
let target_canisters = match args.names.is_empty() {
147149
true => env.get_canister_names(),
@@ -185,7 +187,7 @@ pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), Command
185187

186188
// Agent
187189
let agent = ctx
188-
.get_agent_for_env(&args.identity.clone().into(), args.environment.name())
190+
.get_agent_for_env(&args.identity.clone().into(), &environment_selection)
189191
.await?;
190192

191193
// Select which subnet to deploy the canisters to

crates/icp-cli/src/commands/canister/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) enum CommandError {
2727
Access(#[from] network::AccessError),
2828

2929
#[error(transparent)]
30-
Agent(#[from] agent::CreateError),
30+
Agent(#[from] agent::CreateAgentError),
3131

3232
#[error(transparent)]
3333
Lookup(#[from] LookupIdError),

crates/icp-cli/src/commands/canister/install.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use ic_agent::AgentError;
44
use ic_utils::interfaces::management_canister::builders::CanisterInstallMode;
55
use icp::{
66
agent,
7-
context::{GetAgentForEnvError, GetCanisterIdForEnvError, GetEnvironmentError},
7+
context::{
8+
EnvironmentSelection, GetAgentForEnvError, GetCanisterIdForEnvError, GetEnvironmentError,
9+
},
810
identity, network,
911
};
1012
use tracing::debug;
@@ -46,7 +48,7 @@ pub(crate) enum CommandError {
4648
Access(#[from] network::AccessError),
4749

4850
#[error(transparent)]
49-
Agent(#[from] agent::CreateError),
51+
Agent(#[from] agent::CreateAgentError),
5052

5153
#[error(transparent)]
5254
LookupCanisterId(#[from] LookupIdError),
@@ -68,12 +70,14 @@ pub(crate) enum CommandError {
6870
}
6971

7072
pub(crate) async fn exec(ctx: &Context, args: &InstallArgs) -> Result<(), CommandError> {
73+
let environment_selection: EnvironmentSelection = args.environment.clone().into();
74+
7175
// Load target environment
72-
let env = ctx.get_environment(args.environment.name()).await?;
76+
let env = ctx.get_environment(&environment_selection).await?;
7377

7478
// Agent
7579
let agent = ctx
76-
.get_agent_for_env(&args.identity.clone().into(), args.environment.name())
80+
.get_agent_for_env(&args.identity.clone().into(), &environment_selection)
7781
.await?;
7882

7983
let target_canisters = match args.names.is_empty() {
@@ -82,9 +86,11 @@ pub(crate) async fn exec(ctx: &Context, args: &InstallArgs) -> Result<(), Comman
8286
};
8387

8488
let canisters = try_join_all(target_canisters.into_iter().map(|name| {
85-
let env_name = args.environment.name();
89+
let environment_selection = environment_selection.clone();
8690
async move {
87-
let cid = ctx.get_canister_id_for_env(&name, env_name).await?;
91+
let cid = ctx
92+
.get_canister_id_for_env(&name, &environment_selection)
93+
.await?;
8894
Ok::<_, CommandError>((name, cid))
8995
}
9096
}))

crates/icp-cli/src/commands/canister/settings/show.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) enum CommandError {
2626
Access(#[from] network::AccessError),
2727

2828
#[error(transparent)]
29-
Agent(#[from] agent::CreateError),
29+
Agent(#[from] agent::CreateAgentError),
3030

3131
#[error(transparent)]
3232
Lookup(#[from] LookupIdError),

crates/icp-cli/src/commands/canister/settings/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub(crate) enum CommandError {
116116
Access(#[from] network::AccessError),
117117

118118
#[error(transparent)]
119-
Agent(#[from] agent::CreateError),
119+
Agent(#[from] agent::CreateAgentError),
120120

121121
#[error("invalid environment variable '{variable}'")]
122122
InvalidEnvironmentVariable { variable: String },

crates/icp-cli/src/commands/canister/start.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) enum CommandError {
2525
Access(#[from] network::AccessError),
2626

2727
#[error(transparent)]
28-
Agent(#[from] agent::CreateError),
28+
Agent(#[from] agent::CreateAgentError),
2929

3030
#[error(transparent)]
3131
LookupCanisterId(#[from] LookupIdError),

crates/icp-cli/src/commands/canister/stop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub(crate) enum CommandError {
2525
Access(#[from] network::AccessError),
2626

2727
#[error(transparent)]
28-
Agent(#[from] agent::CreateError),
28+
Agent(#[from] agent::CreateAgentError),
2929

3030
#[error(transparent)]
3131
LookupCanisterId(#[from] LookupIdError),

0 commit comments

Comments
 (0)