Wave 2: CLI skeleton, cache write, config system, spec indexer (bd-3d2, bd-1ie, bd-1sb, bd-189)
This commit is contained in:
85
src/main.rs
85
src/main.rs
@@ -1,8 +1,91 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use std::process::ExitCode;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use swagger_cli::cli::{Cli, Commands};
|
||||
use swagger_cli::errors::SwaggerCliError;
|
||||
use swagger_cli::output::robot;
|
||||
|
||||
fn pre_scan_robot() -> bool {
|
||||
std::env::args().any(|a| a == "--robot")
|
||||
}
|
||||
|
||||
fn command_name(cli: &Cli) -> &'static str {
|
||||
match &cli.command {
|
||||
Commands::Fetch(_) => "fetch",
|
||||
Commands::List(_) => "list",
|
||||
Commands::Show(_) => "show",
|
||||
Commands::Search(_) => "search",
|
||||
Commands::Schemas(_) => "schemas",
|
||||
Commands::Tags(_) => "tags",
|
||||
Commands::Aliases(_) => "aliases",
|
||||
Commands::Sync(_) => "sync",
|
||||
Commands::Doctor(_) => "doctor",
|
||||
Commands::Cache(_) => "cache",
|
||||
Commands::Diff(_) => "diff",
|
||||
}
|
||||
}
|
||||
|
||||
fn output_robot_error(err: &SwaggerCliError, command: &str, duration: Duration) {
|
||||
robot::robot_error(
|
||||
err.code(),
|
||||
&err.to_string(),
|
||||
err.suggestion(),
|
||||
command,
|
||||
duration,
|
||||
);
|
||||
}
|
||||
|
||||
fn output_human_error(err: &SwaggerCliError) {
|
||||
swagger_cli::output::human::print_error(err);
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ExitCode {
|
||||
ExitCode::SUCCESS
|
||||
let is_robot = pre_scan_robot();
|
||||
let start = Instant::now();
|
||||
|
||||
let cli = match Cli::try_parse() {
|
||||
Ok(cli) => cli,
|
||||
Err(err) => {
|
||||
if is_robot {
|
||||
let parse_err = SwaggerCliError::Usage(err.to_string());
|
||||
output_robot_error(&parse_err, "unknown", start.elapsed());
|
||||
return parse_err.to_exit_code();
|
||||
}
|
||||
err.exit();
|
||||
}
|
||||
};
|
||||
|
||||
let cmd = command_name(&cli);
|
||||
let robot = cli.robot;
|
||||
|
||||
let result = match &cli.command {
|
||||
Commands::Fetch(args) => swagger_cli::cli::fetch::execute(args, robot).await,
|
||||
Commands::List(args) => swagger_cli::cli::list::execute(args, robot).await,
|
||||
Commands::Show(args) => swagger_cli::cli::show::execute(args, robot).await,
|
||||
Commands::Search(args) => swagger_cli::cli::search::execute(args, robot).await,
|
||||
Commands::Schemas(args) => swagger_cli::cli::schemas::execute(args, robot).await,
|
||||
Commands::Tags(args) => swagger_cli::cli::tags::execute(args, robot).await,
|
||||
Commands::Aliases(args) => swagger_cli::cli::aliases::execute(args, robot).await,
|
||||
Commands::Sync(args) => swagger_cli::cli::sync_cmd::execute(args, robot).await,
|
||||
Commands::Doctor(args) => swagger_cli::cli::doctor::execute(args, robot).await,
|
||||
Commands::Cache(args) => swagger_cli::cli::cache_cmd::execute(args, robot).await,
|
||||
Commands::Diff(args) => swagger_cli::cli::diff::execute(args, robot).await,
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(err) => {
|
||||
if robot {
|
||||
output_robot_error(&err, cmd, start.elapsed());
|
||||
} else {
|
||||
output_human_error(&err);
|
||||
}
|
||||
err.to_exit_code()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user