znap_cli/
lib.rs

1use anyhow::Result;
2use clap::Parser;
3mod commands;
4mod template;
5mod utils;
6
7#[derive(Debug, Parser)]
8#[clap(version)]
9pub struct Opts {
10    #[clap(subcommand)]
11    pub command: Command,
12}
13
14#[derive(Debug, Parser)]
15pub enum Command {
16    /// Build a collection from the workspace
17    Build {
18        /// The name of the collection
19        name: String,
20    },
21    /// Serves all collections from the workspace
22    Serve {
23        /// The name of the collection
24        name: String,
25        /// Address that will be used for the server once running.
26        #[clap(short, long)]
27        address: Option<String>,
28        /// Port that wuill be used for the server once running.
29        #[clap(short, long)]
30        port: Option<u16>,
31        /// Protocol that wuill be used for the server once running.
32        #[clap(long)]
33        protocol: Option<String>,
34    },
35    /// Runs the test suite for the workspace
36    Test,
37    /// Deploys a workspace using shuttle
38    Deploy {
39        /// The name of the collection
40        name: String,
41        /// The name of the project in shuttle
42        project: String,
43    },
44    /// Cleans all the temp files
45    Clean,
46    /// Initializes a new workspace
47    Init {
48        /// The name of the workspace
49        name: String,
50        /// Skip writing the files.
51        #[clap(short, long)]
52        dry_run: bool,
53    },
54    /// Create a new collection in the workspace
55    New {
56        name: String,
57        /// Skip writing the files.
58        #[clap(short, long)]
59        dry_run: bool,
60    },
61}
62
63fn process_command(opts: Opts) -> Result<()> {
64    match &opts.command {
65        Command::Build { name } => {
66            commands::build::run(name);
67            Ok(())
68        }
69        Command::Serve {
70            name,
71            address,
72            port,
73            protocol,
74        } => {
75            commands::serve::run(name, address.as_deref(), port.as_ref(), protocol.as_deref());
76            Ok(())
77        }
78        Command::Test => {
79            commands::test::run();
80            Ok(())
81        }
82        Command::Clean => {
83            commands::clean::run();
84            Ok(())
85        }
86        Command::Init { name, dry_run } => {
87            commands::init::run(name, *dry_run);
88            Ok(())
89        }
90        Command::New { name, dry_run } => {
91            commands::new::run(name, *dry_run);
92            Ok(())
93        }
94        Command::Deploy { name, project } => {
95            commands::deploy::run(name, project);
96            Ok(())
97        }
98    }
99}
100
101pub fn entry(opts: Opts) -> Result<()> {
102    process_command(opts)
103}