1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use anyhow::Result;
use clap::Parser;
mod commands;
mod template;
mod utils;

#[derive(Debug, Parser)]
#[clap(version)]
pub struct Opts {
    #[clap(subcommand)]
    pub command: Command,
}

#[derive(Debug, Parser)]
pub enum Command {
    /// Builds all collections from the workspace
    Build,
    /// Serves all collections from the workspace
    Serve {
        #[clap(short, long, default_value = "127.0.0.1")]
        address: String,
        #[clap(short, long, default_value = "3000")]
        port: u16,
    },
    /// Runs the test suite for the workspace
    Test,
    /// Cleans all the temp files
    Clean,
    /// Initializes a new workspace
    Init {
        /// The name of the workspace
        name: String,
        /// Skip writing the files.
        #[clap(short, long)]
        dry_run: bool,
    },
    /// Create a new collection in the workspace
    New {
        name: String,
        /// Skip writing the files.
        #[clap(short, long)]
        dry_run: bool,
    },
}

fn process_command(opts: Opts) -> Result<()> {
    match &opts.command {
        Command::Build => Ok(commands::build::run()),
        Command::Serve { address, port } => Ok(commands::serve::run(&address, *port)),
        Command::Test => Ok(commands::test::run()),
        Command::Clean => Ok(commands::clean::run()),
        Command::Init { name, dry_run } => Ok(commands::init::run(&name, &dry_run)),
        Command::New { name, dry_run } => Ok(commands::new::run(&name, &dry_run)),
    }
}

pub fn entry(opts: Opts) -> Result<()> {
    process_command(opts)
}