|
1 | 1 | use clap::Args; |
2 | | -use ic_agent::AgentError; |
3 | 2 | use ic_utils::interfaces::management_canister::CanisterStatusResult; |
4 | | -use icp::{agent, identity, network}; |
5 | 3 | use itertools::Itertools; |
6 | 4 |
|
7 | 5 | use icp::context::Context; |
8 | 6 |
|
9 | | -use crate::options::{EnvironmentOpt, IdentityOpt}; |
10 | | -use icp::store_id::{Key, LookupError as LookupIdError}; |
| 7 | +use crate::commands::args; |
11 | 8 |
|
12 | 9 | #[derive(Debug, Args)] |
13 | 10 | pub(crate) struct InfoArgs { |
14 | | - /// The name of the canister within the current project |
15 | | - pub(crate) name: String, |
16 | | - |
17 | | - #[command(flatten)] |
18 | | - identity: IdentityOpt, |
19 | | - |
20 | 11 | #[command(flatten)] |
21 | | - environment: EnvironmentOpt, |
| 12 | + pub(crate) cmd_args: args::CanisterCommandArgs, |
22 | 13 | } |
23 | 14 |
|
24 | 15 | #[derive(Debug, thiserror::Error)] |
25 | 16 | pub(crate) enum CommandError { |
26 | 17 | #[error(transparent)] |
27 | | - Project(#[from] icp::LoadError), |
28 | | - |
29 | | - #[error(transparent)] |
30 | | - Identity(#[from] identity::LoadError), |
31 | | - |
32 | | - #[error("project does not contain an environment named '{name}'")] |
33 | | - EnvironmentNotFound { name: String }, |
34 | | - |
35 | | - #[error(transparent)] |
36 | | - Access(#[from] network::AccessError), |
37 | | - |
38 | | - #[error(transparent)] |
39 | | - Agent(#[from] agent::CreateError), |
40 | | - |
41 | | - #[error("environment '{environment}' does not include canister '{canister}'")] |
42 | | - EnvironmentCanister { |
43 | | - environment: String, |
44 | | - canister: String, |
45 | | - }, |
46 | | - |
47 | | - #[error(transparent)] |
48 | | - Lookup(#[from] LookupIdError), |
| 18 | + Call(#[from] ic_agent::AgentError), |
49 | 19 |
|
50 | 20 | #[error(transparent)] |
51 | | - Status(#[from] AgentError), |
| 21 | + GetCanisterIdAndAgent(#[from] icp::context::GetCanisterIdAndAgentError), |
52 | 22 | } |
53 | 23 |
|
54 | 24 | pub(crate) async fn exec(ctx: &Context, args: &InfoArgs) -> Result<(), CommandError> { |
55 | | - // Load project |
56 | | - let p = ctx.project.load().await?; |
57 | | - |
58 | | - // Load identity |
59 | | - let id = ctx.identity.load(args.identity.clone().into()).await?; |
60 | | - |
61 | | - // Load target environment |
62 | | - let env = |
63 | | - p.environments |
64 | | - .get(args.environment.name()) |
65 | | - .ok_or(CommandError::EnvironmentNotFound { |
66 | | - name: args.environment.name().to_owned(), |
67 | | - })?; |
68 | | - |
69 | | - // Access network |
70 | | - let access = ctx.network.access(&env.network).await?; |
71 | | - |
72 | | - // Agent |
73 | | - let agent = ctx.agent.create(id, &access.url).await?; |
74 | | - |
75 | | - if let Some(k) = access.root_key { |
76 | | - agent.set_root_key(k); |
77 | | - } |
78 | | - |
79 | | - // Ensure canister is included in the environment |
80 | | - if !env.canisters.contains_key(&args.name) { |
81 | | - return Err(CommandError::EnvironmentCanister { |
82 | | - environment: env.name.to_owned(), |
83 | | - canister: args.name.to_owned(), |
84 | | - }); |
85 | | - } |
86 | | - |
87 | | - // Lookup the canister id |
88 | | - let cid = ctx.ids.lookup(&Key { |
89 | | - network: env.network.name.to_owned(), |
90 | | - environment: env.name.to_owned(), |
91 | | - canister: args.name.to_owned(), |
92 | | - })?; |
| 25 | + let selections = args.cmd_args.selections(); |
| 26 | + |
| 27 | + let (cid, agent) = ctx |
| 28 | + .get_canister_id_and_agent( |
| 29 | + &selections.canister, |
| 30 | + &selections.environment, |
| 31 | + &selections.network, |
| 32 | + &selections.identity, |
| 33 | + ) |
| 34 | + .await?; |
93 | 35 |
|
94 | 36 | // Management Interface |
95 | 37 | let mgmt = ic_utils::interfaces::ManagementCanister::create(&agent); |
|
0 commit comments