Skip to content

Commit

Permalink
Add contract alias ls. (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Aug 31, 2024
1 parent a0be0f7 commit d90cd78
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Utilities to manage contract aliases
* `remove` — Remove contract alias
* `add` — Add contract alias
* `show` — Show the contract id associated with a given alias
* `ls` — List all aliases



Expand Down Expand Up @@ -228,6 +229,19 @@ Show the contract id associated with a given alias



## `stellar contract alias ls`

List all aliases

**Usage:** `stellar contract alias ls [OPTIONS]`

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

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."



## `stellar contract bindings`

Generate code client bindings for a contract
Expand Down
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ flate2 = "1.0.30"
bytesize = "1.3.0"
humantime = "2.1.0"
phf = { version = "0.11.2", features = ["macros"] }
glob = "0.3.1"

# For hyper-tls
[target.'cfg(unix)'.dependencies]
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
@@ -1,6 +1,7 @@
use crate::commands::global;

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

Expand All @@ -14,6 +15,9 @@ pub enum Cmd {

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

/// List all aliases
Ls(ls::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -26,6 +30,9 @@ pub enum Error {

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

#[error(transparent)]
Ls(#[from] ls::Error),
}

impl Cmd {
Expand All @@ -34,6 +41,7 @@ impl Cmd {
Cmd::Remove(remove) => remove.run(global_args)?,
Cmd::Add(add) => add.run(global_args)?,
Cmd::Show(show) => show.run(global_args)?,
Cmd::Ls(ls) => ls.run()?,
}
Ok(())
}
Expand Down
104 changes: 104 additions & 0 deletions cmd/soroban-cli/src/commands/contract/alias/ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::{fs, process};

use clap::{command, Parser};

use crate::commands::config::network;
use crate::config::{alias, locator};

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,
}

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

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

#[error(transparent)]
PatternError(#[from] glob::PatternError),

#[error(transparent)]
GlobError(#[from] glob::GlobError),

#[error(transparent)]
IoError(#[from] std::io::Error),
}

#[derive(Debug, Clone)]
struct AliasEntry {
alias: String,
contract: String,
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let config_dir = self.config_locator.config_dir()?;
let pattern = config_dir
.join("contract-ids")
.join("*.json")
.to_string_lossy()
.into_owned();

let paths = glob::glob(&pattern)?;
let mut found = false;
let mut map: HashMap<String, Vec<AliasEntry>> = HashMap::new();

for path in paths {
let path = path?;

if let Some(alias) = path.file_stem() {
let alias = alias.to_string_lossy().into_owned();
let content = fs::read_to_string(path)?;
let data: alias::Data = serde_json::from_str(&content).unwrap_or_default();

for network_passphrase in data.ids.keys() {
let network_passphrase = network_passphrase.to_string();
let contract = data
.ids
.get(&network_passphrase)
.map(ToString::to_string)
.unwrap_or_default();
let entry = AliasEntry {
alias: alias.clone(),
contract,
};

let list = map.entry(network_passphrase.clone()).or_default();

list.push(entry.clone());
}
}
}

for network_passphrase in map.keys() {
if let Some(list) = map.clone().get_mut(network_passphrase) {
println!("ℹ️ Aliases available for network '{network_passphrase}'");

list.sort_by(|a, b| a.alias.cmp(&b.alias));

for entry in list {
found = true;
println!("{}: {}", entry.alias, entry.contract);
}

println!();
}
}

if !found {
eprintln!("⚠️ No aliases defined for network");

process::exit(1);
}

Ok(())
}
}

0 comments on commit d90cd78

Please sign in to comment.