Skip to content

Commit

Permalink
Add contract alias show. (#1567)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Aug 27, 2024
1 parent a781e44 commit 5b4776c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
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,

#[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}");

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

0 comments on commit 5b4776c

Please sign in to comment.