-
Notifications
You must be signed in to change notification settings - Fork 99
feat: canister metadata command #2335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| load ../utils/_ | ||
|
|
||
| setup() { | ||
| standard_setup | ||
|
|
||
| dfx_new | ||
| } | ||
|
|
||
| teardown() { | ||
| dfx_stop | ||
|
|
||
| standard_teardown | ||
| } | ||
|
|
||
|
|
||
| @test "can read canister metadata from replica" { | ||
| dfx_new hello | ||
| dfx_start | ||
|
|
||
| assert_command dfx deploy | ||
|
|
||
| dfx canister metadata hello_backend candid:service >metadata.txt | ||
| assert_command diff .dfx/local/canisters/hello_backend/hello_backend.did ./metadata.txt | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use crate::lib::error::DfxResult; | ||
| use crate::lib::models::canister_id_store::CanisterIdStore; | ||
| use crate::lib::root_key::fetch_root_key_if_needed; | ||
| use crate::Environment; | ||
|
|
||
| use anyhow::{anyhow, Context}; | ||
| use clap::Parser; | ||
| use ic_types::Principal; | ||
| use std::io::{stdout, Write}; | ||
|
|
||
| /// Sign a canister call and generate message file. | ||
| #[derive(Parser)] | ||
| pub struct CanisterMetadataOpts { | ||
| /// Specifies the name of the canister to call. | ||
| canister_name: String, | ||
|
|
||
| /// Specifies the name of the metadata to retrieve. | ||
| metadata_name: String, | ||
| } | ||
|
|
||
| pub async fn exec(env: &dyn Environment, opts: CanisterMetadataOpts) -> DfxResult { | ||
| let agent = env | ||
| .get_agent() | ||
| .ok_or_else(|| anyhow!("Cannot get HTTP client from environment."))?; | ||
|
|
||
| let callee_canister = opts.canister_name.as_str(); | ||
| let canister_id_store = CanisterIdStore::for_env(env)?; | ||
|
|
||
| let canister_id = Principal::from_text(callee_canister) | ||
| .or_else(|_| canister_id_store.get(callee_canister))?; | ||
|
|
||
| fetch_root_key_if_needed(env).await?; | ||
| let metadata = agent | ||
| .read_state_canister_metadata(canister_id, &opts.metadata_name, false) | ||
| .await | ||
| .with_context(|| format!("Failed to read controllers of canister {}.", canister_id))?; | ||
|
|
||
| stdout().write_all(&metadata)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure the best way to display this. Some of the metadata may not be printable ascii. Maybe we can check if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to have an
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chenyan-dfinity @sesi200 I wondered about these too. Not sure of the best way either. The thinking was that by writing the raw data bytes to stdout, they can be written to a file or piped to another program. While the metadata may not be printable ascii, what if the developer wants to run the actual contents through What would the proposed rules be? Would they more clear than always writing as-is to stdout? The below seem overly complicated to me.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no strong opinion either way and have no problem merging this as-is. I was mostly thinking about invoking dfx from somewhere that doesn't have such trivial redirect mechanics, but I might just be overthinking it. My approach would have been to print to |
||
|
|
||
| Ok(()) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and merged this already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing this!