stellar_registry_cli/commands/
mod.rs

1use std::str::FromStr;
2
3use clap::{command, CommandFactory, FromArgMatches, Parser};
4
5pub mod create_alias;
6pub mod deploy;
7pub mod download;
8pub mod publish;
9pub mod upgrade;
10pub mod version;
11
12const ABOUT: &str = "Add, manage, and use Wasm packages & named contracts in the Stellar Registry";
13
14#[derive(Parser, Debug)]
15#[command(
16    name = "stellar-registry",
17    about = 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::Deploy(deploy) => deploy.run().await?,
43            Cmd::Download(cmd) => cmd.run().await?,
44            Cmd::Publish(p) => p.run().await?,
45            Cmd::CreateAlias(i) => i.run().await?,
46            Cmd::Version(p) => p.run(),
47            Cmd::Upgrade(u) => u.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    /// Deploy a named contract from a published Wasm
64    Deploy(Box<deploy::Cmd>),
65    /// Download a Wasm binary, optionally creating a local file
66    Download(Box<download::Cmd>),
67    /// Create a local `stellar contract alias` from a named registry contract
68    CreateAlias(Box<create_alias::Cmd>),
69    /// Publish Wasm to registry with package name and semantic version
70    Publish(Box<publish::Cmd>),
71    /// Version of the scaffold-registry-cli
72    Version(version::Cmd),
73    /// Upgrade a contract using a published Wasm
74    Upgrade(Box<upgrade::Cmd>),
75}
76
77#[derive(thiserror::Error, Debug)]
78pub enum Error {
79    #[error(transparent)]
80    Deploy(#[from] deploy::Error),
81    #[error(transparent)]
82    Fetch(#[from] download::Error),
83    #[error(transparent)]
84    Install(#[from] create_alias::Error),
85    #[error(transparent)]
86    Publish(#[from] publish::Error),
87    #[error(transparent)]
88    Upgrade(#[from] upgrade::Error),
89}