mod auth;
mod cache;
mod food;
mod local;
mod mart;
mod output;
mod paths;
mod places;
use bpaf::Bpaf;
use grabcli_core::cache::CacheMode;
use food::{FoodCmd, food_cmd};
use mart::{MartCmd, mart_cmd};
use output::Output;
use auth::{AuthCmd, auth_cmd};
use cache::{CacheCmd, cache_cmd};
use places::{PlacesCmd, places_cmd};
#[derive(Debug, Clone, Bpaf)]
enum Service {
#[bpaf(command("auth"))]
Auth {
#[bpaf(external(auth_cmd))]
cmd: AuthCmd,
},
#[bpaf(command("food"))]
Food {
#[bpaf(long("json"), switch)]
json: bool,
#[bpaf(external(food_cmd))]
cmd: FoodCmd,
},
#[bpaf(command("mart"))]
Mart {
#[bpaf(long("json"), switch)]
json: bool,
#[bpaf(external(mart_cmd))]
cmd: MartCmd,
},
#[bpaf(command("saved-places"))]
SavedPlaces {
#[bpaf(external(places_cmd))]
cmd: PlacesCmd,
},
#[bpaf(command("cache"))]
Cache {
#[bpaf(external(cache_cmd))]
cmd: CacheCmd,
},
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options, version)]
struct Cli {
#[bpaf(long("json"), switch)]
json: bool,
#[bpaf(long("no-cache"), switch)]
no_cache: bool,
#[bpaf(long("refresh"), switch)]
refresh: bool,
#[bpaf(external(service))]
service: Service,
}
#[tokio::main]
async fn main() {
let cli = cli().run();
let cache_mode = CacheMode::from_flags(cli.no_cache, cli.refresh);
let (out, result) = match cli.service {
Service::Auth { cmd } => {
let out = Output::new(cli.json || auth::wants_json(&cmd));
(out, auth::run(out, cmd).await)
}
Service::Food { json, cmd } => {
let out = Output::new(cli.json || json || food::cmd_wants_json(&cmd));
(out, food::run(out, cache_mode, cmd).await)
}
Service::Mart { json, cmd } => {
let out = Output::new(cli.json || json || mart::cmd_wants_json(&cmd));
(out, mart::run(out, cache_mode, cmd).await)
}
Service::SavedPlaces { cmd } => {
let out = Output::new(cli.json || places::wants_json(&cmd));
(out, places::run(out, cmd).await)
}
Service::Cache { cmd } => {
let out = Output::new(cli.json || cache::wants_json(&cmd));
(out, cache::run(out, cmd).await)
}
};
if let Err(err) = result {
std::process::exit(out.emit_error(&err));
}
}