Skip to content
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

Add contract alias show. #1567

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Utilities to manage contract aliases

* `remove` — Remove contract alias
* `add` — Add contract alias
* `show` — Show the contract id associated with a given alias



Expand Down Expand Up @@ -207,6 +208,26 @@ Add contract alias



## `stellar contract alias show`

Show the contract id associated with a given alias

**Usage:** `stellar contract alias show [OPTIONS] <ALIAS>`

###### **Arguments:**

* `<ALIAS>` — The contract alias that will be displayed

###### **Options:**

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."
* `--rpc-url <RPC_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config



## `stellar contract bindings`

Generate code client bindings for a contract
Expand Down
8 changes: 8 additions & 0 deletions cmd/soroban-cli/src/commands/contract/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::commands::global;

pub mod add;
pub mod remove;
pub mod show;

#[derive(Debug, clap::Subcommand)]
pub enum Cmd {
Expand All @@ -10,6 +11,9 @@ pub enum Cmd {

/// Add contract alias
Add(add::Cmd),

/// Show the contract id associated with a given alias
Show(show::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -19,13 +23,17 @@ pub enum Error {

#[error(transparent)]
Add(#[from] add::Error),

#[error(transparent)]
Show(#[from] show::Error),
}

impl Cmd {
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
match &self {
Cmd::Remove(remove) => remove.run(global_args)?,
Cmd::Add(add) => add.run(global_args)?,
Cmd::Show(show) => show.run(global_args)?,
}
Ok(())
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/soroban-cli/src/commands/contract/alias/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ pub enum Error {
#[error(transparent)]
Network(#[from] network::Error),

#[error("no contract found with alias `{alias}`")]
NoContract { alias: String },
#[error("no contract found with alias '{alias}' for network '{network_passphrase}'")]
NoContract {
alias: String,
network_passphrase: String,
},
}

impl Cmd {
Expand All @@ -44,6 +47,7 @@ impl Cmd {
else {
return Err(Error::NoContract {
alias: alias.into(),
network_passphrase: network_passphrase.into(),
});
};

Expand Down
62 changes: 62 additions & 0 deletions cmd/soroban-cli/src/commands/contract/alias/show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::fmt::Debug;

use clap::{command, Parser};

use crate::commands::{config::network, global};
use crate::config::locator;
use crate::print::Print;

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,
fnando marked this conversation as resolved.
Show resolved Hide resolved

#[command(flatten)]
network: network::Args,

/// The contract alias that will be displayed.
pub alias: String,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Locator(#[from] locator::Error),

#[error(transparent)]
Network(#[from] network::Error),

#[error("no contract found with alias '{alias}' for network '{network_passphrase}'")]
NoContract {
alias: String,
network_passphrase: String,
},
}

impl Cmd {
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
let alias = &self.alias;
let network = self.network.get(&self.config_locator)?;
let network_passphrase = &network.network_passphrase;

if let Some(contract) = self
.config_locator
.get_contract_id(&self.alias, network_passphrase)?
{
print.infoln(format!(
"Contract alias '{alias}' references {contract} on network '{network_passphrase}'"
));

println!("{contract}");
Comment on lines +48 to +52
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏻 The split of user friendly message on stderr along with outputting the contract addresses to stdout is 👨🏻‍🍳💋.


Ok(())
} else {
Err(Error::NoContract {
alias: alias.into(),
network_passphrase: network_passphrase.into(),
})
}
}
}
Loading