stellar_registry_cli/commands/
mod.rs

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