stellar_scaffold_cli/commands/
mod.rs

1use std::str::FromStr;
2
3use clap::{command, CommandFactory, FromArgMatches, Parser};
4
5pub mod build;
6pub mod init;
7pub mod update_env;
8pub mod watch;
9
10const ABOUT: &str = "Build smart contracts with frontend support";
11
12#[derive(Parser, Debug)]
13#[command(
14    name = "stellar-scaffold",
15    about = ABOUT,
16    disable_help_subcommand = true,
17)]
18pub struct Root {
19    #[command(subcommand)]
20    pub cmd: Cmd,
21}
22
23impl Root {
24    pub fn new() -> Result<Self, clap::Error> {
25        let mut matches = Self::command().get_matches();
26        Self::from_arg_matches_mut(&mut matches)
27    }
28
29    pub fn from_arg_matches<I, T>(itr: I) -> Result<Self, clap::Error>
30    where
31        I: IntoIterator<Item = T>,
32        T: Into<std::ffi::OsString> + Clone,
33    {
34        Self::from_arg_matches_mut(&mut Self::command().get_matches_from(itr))
35    }
36    pub async fn run(&mut self) -> Result<(), Error> {
37        match &mut self.cmd {
38            Cmd::Init(init_info) => init_info.run()?,
39            Cmd::Build(build_info) => build_info.run().await?,
40            Cmd::UpdateEnv(e) => e.run()?,
41            Cmd::Watch(watch_info) => watch_info.run().await?,
42        }
43        Ok(())
44    }
45}
46
47impl FromStr for Root {
48    type Err = clap::Error;
49
50    fn from_str(s: &str) -> Result<Self, Self::Err> {
51        Self::from_arg_matches(s.split_whitespace())
52    }
53}
54
55#[derive(Parser, Debug)]
56pub enum Cmd {
57    /// Initialize the project
58    Init(init::Cmd),
59
60    /// Build contracts, resolving dependencies in the correct order. If you have an `environments.toml` file, it will also follow its instructions to configure the environment set by the `STELLAR_SCAFFOLD_ENV` environment variable, turning your contracts into frontend packages (NPM dependencies).
61    Build(build::Command),
62
63    /// Update an environment variable in a .env file
64    UpdateEnv(update_env::Cmd),
65
66    /// Monitor contracts and environments.toml for changes and rebuild as needed
67    Watch(watch::Cmd),
68}
69
70#[derive(thiserror::Error, Debug)]
71pub enum Error {
72    // TODO: stop using Debug for displaying errors
73    #[error(transparent)]
74    Init(#[from] init::Error),
75    #[error(transparent)]
76    BuildContracts(#[from] build::Error),
77    #[error(transparent)]
78    UpdateEnv(#[from] update_env::Error),
79    #[error(transparent)]
80    Watch(#[from] watch::Error),
81}