-
Notifications
You must be signed in to change notification settings - Fork 98
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}; | ||
|
|
||
| /// Displays metadata in a canister. | ||
| #[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)?; | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure the best way to display this. Some of the metadata may not be printable ascii. Maybe we can check if
metadatais printable, if not, we display the hex instead.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.
Would it make sense to have an
--outfileflag instead/in addition?Uh oh!
There was an error while loading. Please reload this page.
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.
@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
hexdumpor some other tool? Ifdfx canister metadata ... | hexdump -C, and the metadata is not entirely printable ascii, what would the expected behavior be?What would the proposed rules be? Would they more clear than always writing as-is to stdout? The below seem overly complicated to me.
--outfilespecified, write data to specified file as-is.--outfile -means write data as-is to stdout.--outfilespecified, and metadata is printable ascii, write that to stdout.--outfilespecified, and metadata is not all printable ascii, then write an explanation to stderr and display metadata as hex to stdout.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 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
--outfileif specified, otherwise print to stdout. Reason being that I'd want to give a way to not see garbage printed if it is expected.