soroban_cli/commands/contract/alias/
ls.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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(())
    }
}