use clap::{arg, Command};
use anyhow::Result;
mod sites;
mod accounts;
fn cli() -> Command {
Command::new("wpe")
.about("WPEngine CLI")
.arg(arg!(-H --headless "Enables headless mode").required(false))
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.subcommand(
Command::new("sites")
.about("Display list of sites as selection.")
.arg(arg!(<PAGE> "The page number").required(false))
.after_help("Selecting one will fetch the site and display more options.")
.subcommand(
Command::new("list")
.about("List sites.")
.arg(arg!(<ID> "Account ID").required(false))
)
.subcommand(
Command::new("add")
.about("Add a site using headless mode")
.arg(arg!(<NAME> "Site name").required(true))
.arg(arg!(<ID> "Account ID").required(true))
)
.subcommand(
Command::new("update")
.about("Update the name of a site.")
.arg(arg!(<NAME> "Site name").required(true))
.arg(arg!(<ID> "Site ID").required(true))
)
.subcommand(
Command::new("delete")
.about("Delete a site.")
.arg(arg!(<ID> "Site ID").required(true))
)
)
.subcommand(
Command::new("accounts")
.about("Fetch all sites from your wpengine account")
.arg(arg!(<PAGE> "The page number").required(false))
)
.subcommand(
Command::new("account")
.about("Fetch an account by its ID")
.arg(arg!(<ID> "The account ID"))
.arg_required_else_help(true),
)
.subcommand(
Command::new("auth")
.about("Authenticate with WP Engine API")
.subcommand(
Command::new("login")
.about("Login to WP Engine API")
)
.subcommand(
Command::new("reset")
.about("Reset authentication")
)
.subcommand_required(true)
)
.subcommand(
Command::new("status")
.about("Get API status")
)
.subcommand(
Command::new("swagger")
.about("Get API swagger")
)
}
fn main() -> Result<()> {
wpe::init()?;
let matches = cli().get_matches();
let command = wpe::API::new();
let headless = matches.get_one::<bool>("headless");
match matches.subcommand() {
Some(("sites", sub_n)) => {
sites::init(sub_n, command, headless)?;
},
Some(("accounts", sub_n)) => {
accounts::init(sub_n, command, headless)?;
},
Some(("account", sub_m)) => {
let id = sub_m.get_one::<String>("ID").unwrap();
let res = command.get_account_by_id(id)?;
println!("{}", serde_json::to_string_pretty(&res)?);
},
Some(("auth", sub_m)) => {
match sub_m.subcommand() {
Some(("login", _)) => {
wpe::set_auth()?;
},
Some(("reset", _)) => {
wpe::reset()?;
},
_ => println!("Error with auth command.")
}
},
Some(("status", _)) => {
let status = command.status()?;
println!("{}", serde_json::to_string_pretty(&status)?)
},
Some(("swagger", _)) => {
let swagger = command.swagger()?;
println!("{}", serde_json::to_string_pretty(&swagger)?)
}
_ => println!("Invalid command. Please use <help> to a see full list of commands.")
}
Ok(())
}