Skip to main content

stellar_registry_cli/commands/
mod.rs

1use std::str::FromStr;
2
3use clap::{CommandFactory, FromArgMatches, Parser, command};
4
5pub mod create_alias;
6pub mod current_version;
7pub mod deploy;
8pub mod deploy_unnamed;
9pub mod download;
10pub mod fetch_contract_id;
11pub mod fetch_hash;
12pub mod global;
13pub mod publish;
14pub mod publish_hash;
15pub mod register_contract;
16pub mod rename_contract;
17pub mod update_contract_address;
18pub mod update_contract_owner;
19pub mod upgrade;
20pub mod version;
21
22const ABOUT: &str = "Add, manage, and use Wasm packages & named contracts in the Stellar Registry";
23
24#[derive(Parser, Debug)]
25#[command(
26    name = "stellar-registry",
27    about = ABOUT,
28    disable_help_subcommand = true,
29)]
30pub struct Root {
31    // #[clap(flatten)]
32    // pub global_args: global::Args,
33    #[command(subcommand)]
34    pub cmd: Cmd,
35}
36
37impl Root {
38    pub fn new() -> Result<Self, clap::Error> {
39        let mut matches = Self::command().get_matches();
40        Self::from_arg_matches_mut(&mut matches)
41    }
42
43    pub fn from_arg_matches<I, T>(itr: I) -> Result<Self, clap::Error>
44    where
45        I: IntoIterator<Item = T>,
46        T: Into<std::ffi::OsString> + Clone,
47    {
48        Self::from_arg_matches_mut(&mut Self::command().get_matches_from(itr))
49    }
50    pub async fn run(&mut self) -> Result<(), Error> {
51        match &mut self.cmd {
52            Cmd::CurrentVersion(cmd) => cmd.run().await?,
53            Cmd::Deploy(deploy) => deploy.run().await?,
54            Cmd::DeployUnnamed(cmd) => cmd.run().await?,
55            Cmd::Download(cmd) => cmd.run().await?,
56            Cmd::FetchContractId(cmd) => cmd.run().await?,
57            Cmd::FetchHash(cmd) => cmd.run().await?,
58            Cmd::Publish(p) => p.run().await?,
59            Cmd::PublishHash(cmd) => cmd.run().await?,
60            Cmd::CreateAlias(i) => i.run().await?,
61            Cmd::RegisterContract(cmd) => cmd.run().await?,
62            Cmd::RenameContract(cmd) => cmd.run().await?,
63            Cmd::UpdateContractAddress(cmd) => cmd.run().await?,
64            Cmd::UpdateContractOwner(cmd) => cmd.run().await?,
65            Cmd::Version(p) => p.run(),
66            Cmd::Upgrade(u) => u.run().await?,
67        }
68        Ok(())
69    }
70}
71
72impl FromStr for Root {
73    type Err = clap::Error;
74
75    fn from_str(s: &str) -> Result<Self, Self::Err> {
76        Self::from_arg_matches(s.split_whitespace())
77    }
78}
79
80#[derive(Parser, Debug)]
81pub enum Cmd {
82    /// Create a local `stellar contract alias` from a named registry contract
83    CreateAlias(Box<create_alias::Cmd>),
84    /// Get the current (latest) version of a published Wasm
85    CurrentVersion(Box<current_version::Cmd>),
86    /// Deploy a named contract from a published Wasm
87    Deploy(Box<deploy::Cmd>),
88    /// Deploy a contract from a published Wasm without registering a name
89    DeployUnnamed(Box<deploy_unnamed::Cmd>),
90    /// Download a Wasm binary, optionally creating a local file
91    Download(Box<download::Cmd>),
92    /// Look up the contract ID of a deployed contract by name
93    FetchContractId(Box<fetch_contract_id::Cmd>),
94    /// Fetch the hash of a published Wasm binary
95    FetchHash(Box<fetch_hash::Cmd>),
96    /// Publish Wasm to registry with package name and semantic version
97    Publish(Box<publish::Cmd>),
98    /// Publish a Wasm hash (already uploaded) to registry
99    PublishHash(Box<publish_hash::Cmd>),
100    /// Register an existing contract with a name in the registry
101    RegisterContract(Box<register_contract::Cmd>),
102    /// Rename a registered contract
103    RenameContract(Box<rename_contract::Cmd>),
104    /// Update the contract address of a registered contract
105    UpdateContractAddress(Box<update_contract_address::Cmd>),
106    /// Update the owner of a registered contract
107    UpdateContractOwner(Box<update_contract_owner::Cmd>),
108    /// Upgrade a contract using a published Wasm
109    Upgrade(Box<upgrade::Cmd>),
110    /// Version of the scaffold-registry-cli
111    Version(version::Cmd),
112}
113
114#[derive(thiserror::Error, Debug)]
115pub enum Error {
116    #[error(transparent)]
117    CreateAlias(#[from] create_alias::Error),
118    #[error(transparent)]
119    CurrentVersion(#[from] current_version::Error),
120    #[error(transparent)]
121    Deploy(#[from] deploy::Error),
122    #[error(transparent)]
123    DeployUnnamed(#[from] deploy_unnamed::Error),
124    #[error(transparent)]
125    Download(#[from] download::Error),
126    #[error(transparent)]
127    FetchContractId(#[from] fetch_contract_id::Error),
128    #[error(transparent)]
129    FetchHash(#[from] fetch_hash::Error),
130    #[error(transparent)]
131    Publish(#[from] publish::Error),
132    #[error(transparent)]
133    PublishHash(#[from] publish_hash::Error),
134    #[error(transparent)]
135    RegisterContract(#[from] register_contract::Error),
136    #[error(transparent)]
137    RenameContract(#[from] rename_contract::Error),
138    #[error(transparent)]
139    UpdateContractAddress(#[from] update_contract_address::Error),
140    #[error(transparent)]
141    UpdateContractOwner(#[from] update_contract_owner::Error),
142    #[error(transparent)]
143    Upgrade(#[from] upgrade::Error),
144}