stellar_registry_cli/commands/
mod.rs

1use std::str::FromStr;
2
3use clap::{command, CommandFactory, FromArgMatches, Parser};
4
5pub mod deploy;
6pub mod install;
7
8const ABOUT: &str = "Publish and install Soroban contracts";
9
10// long_about is shown when someone uses `--help`; short help when using `-h`
11const LONG_ABOUT: &str = "LONG ABOUT";
12
13#[derive(Parser, Debug)]
14#[command(
15    name = "stellar-registry",
16    about = ABOUT,
17    long_about = ABOUT.to_string() + LONG_ABOUT,
18    disable_help_subcommand = true,
19)]
20pub struct Root {
21    // #[clap(flatten)]
22    // pub global_args: global::Args,
23    #[command(subcommand)]
24    pub cmd: Cmd,
25}
26
27impl Root {
28    pub fn new() -> Result<Self, clap::Error> {
29        let mut matches = Self::command().get_matches();
30        Self::from_arg_matches_mut(&mut matches)
31    }
32
33    pub fn from_arg_matches<I, T>(itr: I) -> Result<Self, clap::Error>
34    where
35        I: IntoIterator<Item = T>,
36        T: Into<std::ffi::OsString> + Clone,
37    {
38        Self::from_arg_matches_mut(&mut Self::command().get_matches_from(itr))
39    }
40    pub async fn run(&mut self) -> Result<(), Error> {
41        match &mut self.cmd {
42            Cmd::Install(i) => i.run().await?,
43            Cmd::Deploy(deploy) => deploy.run().await?,
44        }
45        Ok(())
46    }
47}
48
49impl FromStr for Root {
50    type Err = clap::Error;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Self::from_arg_matches(s.split_whitespace())
54    }
55}
56
57#[derive(Parser, Debug)]
58pub enum Cmd {
59    /// deploy contract from deployed Wasm
60    Deploy(Box<deploy::Cmd>),
61    /// install contracts
62    Install(install::Cmd),
63}
64
65#[derive(thiserror::Error, Debug)]
66pub enum Error {
67    #[error(transparent)]
68    Deploy(#[from] deploy::Error),
69    #[error(transparent)]
70    Install(#[from] install::Error),
71}