-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.rs
87 lines (76 loc) · 2.56 KB
/
main.rs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#![allow(clippy::uninlined_format_args)]
use clap::{Parser, Subcommand};
use miette::Report;
use tracing::subscriber::set_default;
use tracing::Level;
use tracing_subscriber::layer::SubscriberExt;
mod commands;
use commands::{Build, ConfigSchema, Dev, GenerateCss, Serve};
pub mod formatter;
use crate::commands::Generate;
use formatter::OutputFormat;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[clap(subcommand)]
command: Command,
/// Whether to output more detailed debug information
#[clap(short, long)]
#[clap(help_heading = "GLOBAL OPTIONS", global = true)]
pub verbose: bool,
/// The format of the output
#[clap(long, value_enum)]
#[clap(default_value_t = OutputFormat::Human)]
#[clap(help_heading = "GLOBAL OPTIONS", global = true)]
pub output_format: OutputFormat,
}
#[derive(Subcommand, Debug)]
enum Command {
/// Build an oranda site.
Build(Build),
/// Start a local development server that recompiles your oranda site if a file changes.
Dev(Dev),
/// Start a file server to access your oranda site in a browser.
Serve(Serve),
/// Generate infrastructure files for oranda sites.
Generate(Generate),
#[clap(hide = true)]
ConfigSchema(ConfigSchema),
#[clap(hide = true)]
GenerateCss(GenerateCss),
}
fn main() {
let cli = Cli::parse();
axocli::CliAppBuilder::new("oranda")
.json_errors(cli.output_format == OutputFormat::Json)
.start(cli, run);
}
fn run(cli: &axocli::CliApp<Cli>) -> Result<(), Report> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.max_blocking_threads(128)
.enable_all()
.build()
.expect("Initializing tokio runtime failed");
let _guard = runtime.enter();
let log_level = if cli.config.verbose {
Level::DEBUG
} else {
Level::INFO
};
let sub_filter = tracing_subscriber::filter::Targets::new().with_target("oranda", log_level);
let sub = tracing_subscriber::registry()
.with(formatter::CaptureFieldsLayer)
.with(tracing_subscriber::fmt::layer().event_format(formatter::OrandaFormatter))
.with(sub_filter);
let _sub_guard = set_default(sub);
match &cli.config.command {
Command::Build(cmd) => cmd.run()?,
Command::Dev(cmd) => cmd.clone().run()?,
Command::Serve(cmd) => cmd.run()?,
Command::ConfigSchema(cmd) => cmd.run()?,
Command::GenerateCss(cmd) => cmd.run()?,
Command::Generate(cmd) => cmd.run()?,
};
Ok(())
}