Skip to content

Commit 7abf84a

Browse files
authored
feat: allow token/cycles commands to target networks (#207)
1 parent 28217f9 commit 7abf84a

File tree

28 files changed

+978
-361
lines changed

28 files changed

+978
-361
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,41 @@ impl CanisterCommandArgs {
6868
}
6969
}
7070

71+
// Common argument used for Token and Cycles commands
72+
#[derive(Args, Clone, Debug)]
73+
pub(crate) struct TokenCommandArgs {
74+
#[command(flatten)]
75+
pub(crate) network: NetworkOpt,
76+
77+
#[command(flatten)]
78+
pub(crate) environment: EnvironmentOpt,
79+
80+
#[command(flatten)]
81+
pub(crate) identity: IdentityOpt,
82+
}
83+
84+
/// Selections derived from TokenCommandArgs
85+
pub(crate) struct TokenCommandSelections {
86+
pub(crate) environment: EnvironmentSelection,
87+
pub(crate) network: NetworkSelection,
88+
pub(crate) identity: IdentitySelection,
89+
}
90+
91+
impl TokenCommandArgs {
92+
/// Convert command arguments into selection enums
93+
pub(crate) fn selections(&self) -> TokenCommandSelections {
94+
let environment_selection: EnvironmentSelection = self.environment.clone().into();
95+
let network_selection: NetworkSelection = self.network.clone().into();
96+
let identity_selection: IdentitySelection = self.identity.clone().into();
97+
98+
TokenCommandSelections {
99+
environment: environment_selection,
100+
network: network_selection,
101+
identity: identity_selection,
102+
}
103+
}
104+
}
105+
71106
#[derive(Clone, Debug, PartialEq)]
72107
pub(crate) enum Canister {
73108
Name(String),

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use candid::IDLArgs;
44
use clap::Args;
55
use dialoguer::console::Term;
66

7-
use icp::context::Context;
7+
use icp::context::{Context, GetAgentError, GetCanisterIdError};
88

99
use crate::commands::args;
1010

@@ -35,18 +35,27 @@ pub(crate) enum CommandError {
3535
Call(#[from] ic_agent::AgentError),
3636

3737
#[error(transparent)]
38-
GetCanisterIdAndAgent(#[from] icp::context::GetCanisterIdAndAgentError),
38+
GetAgent(#[from] GetAgentError),
39+
40+
#[error(transparent)]
41+
GetCanisterId(#[from] GetCanisterIdError),
3942
}
4043

4144
pub(crate) async fn exec(ctx: &Context, args: &CallArgs) -> Result<(), CommandError> {
4245
let selections = args.cmd_args.selections();
4346

44-
let (cid, agent) = ctx
45-
.get_canister_id_and_agent(
46-
&selections.canister,
47+
let agent = ctx
48+
.get_agent(
49+
&selections.identity,
50+
&selections.network,
4751
&selections.environment,
52+
)
53+
.await?;
54+
let cid = ctx
55+
.get_canister_id(
56+
&selections.canister,
4857
&selections.network,
49-
&selections.identity,
58+
&selections.environment,
5059
)
5160
.await?;
5261

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ pub(crate) async fn exec(ctx: &Context, args: &CreateArgs) -> Result<(), Command
154154
let (_, canister_info) = env.get_canister_info(&canister).map_err(|e| anyhow!(e))?;
155155

156156
if ctx
157-
.get_canister_id_for_env(&canister, &selections.environment)
157+
.get_canister_id_for_env(
158+
&icp::context::CanisterSelection::Named(canister.clone()),
159+
&selections.environment,
160+
)
158161
.await
159162
.is_ok()
160163
{

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::Args;
22
use ic_agent::AgentError;
33

4-
use icp::context::Context;
4+
use icp::context::{Context, GetAgentError, GetCanisterIdError};
55

66
use crate::commands::args;
77

@@ -17,18 +17,27 @@ pub(crate) enum CommandError {
1717
Delete(#[from] AgentError),
1818

1919
#[error(transparent)]
20-
GetCanisterIdAndAgent(#[from] icp::context::GetCanisterIdAndAgentError),
20+
GetAgent(#[from] GetAgentError),
21+
22+
#[error(transparent)]
23+
GetCanisterId(#[from] GetCanisterIdError),
2124
}
2225

2326
pub(crate) async fn exec(ctx: &Context, args: &DeleteArgs) -> Result<(), CommandError> {
2427
let selections = args.cmd_args.selections();
2528

26-
let (cid, agent) = ctx
27-
.get_canister_id_and_agent(
28-
&selections.canister,
29+
let agent = ctx
30+
.get_agent(
31+
&selections.identity,
32+
&selections.network,
2933
&selections.environment,
34+
)
35+
.await?;
36+
let cid = ctx
37+
.get_canister_id(
38+
&selections.canister,
3039
&selections.network,
31-
&selections.identity,
40+
&selections.environment,
3241
)
3342
.await?;
3443

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clap::Args;
22
use ic_agent::AgentError;
33
use ic_utils::interfaces::management_canister::CanisterStatusResult;
4-
use icp::{agent, context::GetCanisterIdAndAgentError, identity, network};
4+
use icp::{agent, identity, network};
55
use itertools::Itertools;
66

7-
use icp::context::Context;
7+
use icp::context::{Context, GetAgentError, GetCanisterIdError};
88

99
use crate::commands::args;
1010
use icp::store_id::LookupIdError;
@@ -36,17 +36,26 @@ pub(crate) enum CommandError {
3636
Status(#[from] AgentError),
3737

3838
#[error(transparent)]
39-
GetCanisterIdAndAgent(#[from] GetCanisterIdAndAgentError),
39+
GetAgent(#[from] GetAgentError),
40+
41+
#[error(transparent)]
42+
GetCanisterId(#[from] GetCanisterIdError),
4043
}
4144

4245
pub(crate) async fn exec(ctx: &Context, args: &InfoArgs) -> Result<(), CommandError> {
4346
let selections = args.cmd_args.selections();
44-
let (cid, agent) = ctx
45-
.get_canister_id_and_agent(
46-
&selections.canister,
47+
let agent = ctx
48+
.get_agent(
49+
&selections.identity,
50+
&selections.network,
4751
&selections.environment,
52+
)
53+
.await?;
54+
let cid = ctx
55+
.get_canister_id(
56+
&selections.canister,
4857
&selections.network,
49-
&selections.identity,
58+
&selections.environment,
5059
)
5160
.await?;
5261

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ic_utils::interfaces::ManagementCanister;
44
use icp::fs;
55
use icp::{context::CanisterSelection, prelude::*};
66

7-
use icp::context::Context;
7+
use icp::context::{Context, GetAgentError, GetCanisterIdError};
88

99
use crate::{
1010
commands::args,
@@ -34,7 +34,10 @@ pub(crate) enum CommandError {
3434
ReadWasmFile(#[from] fs::Error),
3535

3636
#[error(transparent)]
37-
GetCanisterIdAndAgent(#[from] icp::context::GetCanisterIdAndAgentError),
37+
GetAgent(#[from] GetAgentError),
38+
39+
#[error(transparent)]
40+
GetCanisterId(#[from] GetCanisterIdError),
3841

3942
#[error(transparent)]
4043
Unexpected(#[from] anyhow::Error),
@@ -62,12 +65,18 @@ pub(crate) async fn exec(ctx: &Context, args: &InstallArgs) -> Result<(), Comman
6265
.map_err(|e| anyhow!(e))?
6366
};
6467

65-
let (canister_id, agent) = ctx
66-
.get_canister_id_and_agent(
67-
&selections.canister,
68+
let agent = ctx
69+
.get_agent(
70+
&selections.identity,
71+
&selections.network,
6872
&selections.environment,
73+
)
74+
.await?;
75+
let canister_id = ctx
76+
.get_canister_id(
77+
&selections.canister,
6978
&selections.network,
70-
&selections.identity,
79+
&selections.environment,
7180
)
7281
.await?;
7382

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use ic_agent::{AgentError, export::Principal};
33
use ic_management_canister_types::{CanisterStatusResult, LogVisibility};
44
use icp::{agent, identity, network};
55

6-
use icp::context::{Context, GetAgentForEnvError, GetCanisterIdAndAgentError};
6+
use icp::context::{Context, GetAgentError, GetAgentForEnvError, GetCanisterIdError};
77

88
use crate::commands::args;
99
use icp::store_id::LookupIdError;
@@ -38,17 +38,26 @@ pub(crate) enum CommandError {
3838
GetAgentForEnv(#[from] GetAgentForEnvError),
3939

4040
#[error(transparent)]
41-
GetCanisterIdAndAgent(#[from] GetCanisterIdAndAgentError),
41+
GetAgent(#[from] GetAgentError),
42+
43+
#[error(transparent)]
44+
GetCanisterId(#[from] GetCanisterIdError),
4245
}
4346

4447
pub(crate) async fn exec(ctx: &Context, args: &ShowArgs) -> Result<(), CommandError> {
4548
let selections = args.cmd_args.selections();
46-
let (cid, agent) = ctx
47-
.get_canister_id_and_agent(
48-
&selections.canister,
49+
let agent = ctx
50+
.get_agent(
51+
&selections.identity,
52+
&selections.network,
4953
&selections.environment,
54+
)
55+
.await?;
56+
let cid = ctx
57+
.get_canister_id(
58+
&selections.canister,
5059
&selections.network,
51-
&selections.identity,
60+
&selections.environment,
5261
)
5362
.await?;
5463

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use clap::Args;
66
use ic_utils::interfaces::ManagementCanister;
77
use icp::context::{
8-
CanisterSelection, Context, GetCanisterIdAndAgentError, GetEnvCanisterError,
8+
CanisterSelection, Context, GetAgentError, GetCanisterIdError, GetEnvCanisterError,
99
GetEnvironmentError,
1010
};
1111
use snafu::Snafu;
@@ -19,7 +19,10 @@ pub(crate) struct SyncArgs {
1919
#[derive(Debug, Snafu)]
2020
pub(crate) enum CommandError {
2121
#[snafu(transparent)]
22-
GetIdAndAgent { source: GetCanisterIdAndAgentError },
22+
GetAgent { source: GetAgentError },
23+
24+
#[snafu(transparent)]
25+
GetCanisterId { source: GetCanisterIdError },
2326

2427
#[snafu(transparent)]
2528
GetEnvironment { source: GetEnvironmentError },
@@ -44,12 +47,18 @@ pub(crate) async fn exec(ctx: &Context, args: &SyncArgs) -> Result<(), CommandEr
4447
.get_canister_and_path_for_env(name, &selections.environment)
4548
.await?;
4649

47-
let (cid, agent) = ctx
48-
.get_canister_id_and_agent(
49-
&selections.canister,
50+
let agent = ctx
51+
.get_agent(
52+
&selections.identity,
53+
&selections.network,
5054
&selections.environment,
55+
)
56+
.await?;
57+
let cid = ctx
58+
.get_canister_id(
59+
&selections.canister,
5160
&selections.network,
52-
&selections.identity,
61+
&selections.environment,
5362
)
5463
.await?;
5564

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ic_agent::{AgentError, export::Principal};
88
use ic_management_canister_types::{CanisterStatusResult, EnvironmentVariable, LogVisibility};
99
use icp::{LoadError, agent, identity, network};
1010

11-
use icp::context::{CanisterSelection, Context, GetCanisterIdAndAgentError};
11+
use icp::context::{CanisterSelection, Context, GetAgentError, GetCanisterIdError};
1212
use snafu::{ResultExt, Snafu};
1313

1414
use crate::commands::args;
@@ -131,20 +131,29 @@ pub(crate) enum CommandError {
131131
Update { source: AgentError },
132132

133133
#[snafu(transparent)]
134-
GetCanisterIdAndAgent { source: GetCanisterIdAndAgentError },
134+
GetAgent { source: GetAgentError },
135+
136+
#[snafu(transparent)]
137+
GetCanisterId { source: GetCanisterIdError },
135138

136139
#[snafu(display("failed to write to terminal"))]
137140
WriteTerm { source: std::io::Error },
138141
}
139142

140143
pub(crate) async fn exec(ctx: &Context, args: &UpdateArgs) -> Result<(), CommandError> {
141144
let selections = args.cmd_args.selections();
142-
let (cid, agent) = ctx
143-
.get_canister_id_and_agent(
144-
&selections.canister,
145+
let agent = ctx
146+
.get_agent(
147+
&selections.identity,
148+
&selections.network,
145149
&selections.environment,
150+
)
151+
.await?;
152+
let cid = ctx
153+
.get_canister_id(
154+
&selections.canister,
146155
&selections.network,
147-
&selections.identity,
156+
&selections.environment,
148157
)
149158
.await?;
150159

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ pub(crate) struct ShowArgs {
1313
#[derive(Debug, thiserror::Error)]
1414
pub(crate) enum CommandError {
1515
#[error(transparent)]
16-
GetCanisterId(#[from] icp::context::GetCanisterIdError),
16+
GetCanisterId(#[from] icp::context::GetCanisterIdForEnvError),
1717
}
1818

1919
pub(crate) async fn exec(ctx: &Context, args: &ShowArgs) -> Result<(), CommandError> {
2020
let (canister_selection, environment_selection) = args.cmd_args.selections();
2121

2222
let cid = ctx
23-
.get_canister_id(&canister_selection, &environment_selection)
23+
.get_canister_id_for_env(&canister_selection, &environment_selection)
2424
.await?;
2525

2626
println!("{cid} => {}", args.cmd_args.canister);

0 commit comments

Comments
 (0)