Skip to main content

az_rs/
lib.rs

1use anyhow::Result;
2use api::ApiManager;
3use arg::CliInput;
4use clap::{ArgMatches, Command};
5use client::Client;
6use std::path::PathBuf;
7
8pub mod api;
9pub mod arg;
10pub mod azidentityext;
11pub mod client;
12pub mod cmd;
13pub mod log;
14
15#[cfg(target_arch = "wasm32")]
16pub mod wasm_exports;
17
18pub async fn run(p: PathBuf, client: &Client, raw_input: Vec<String>) -> Result<String> {
19    tracing::info!("Running CLI with input: {:?}", raw_input);
20
21    let api_manager = ApiManager::new(p)?;
22
23    let matches = get_matches(cmd::cmd(), raw_input.clone())?;
24
25    match matches.subcommand() {
26        Some(("api", matches)) => {
27            let args = if let Some(args) = matches.get_many::<String>("args") {
28                args.cloned().collect()
29            } else {
30                vec![]
31            };
32            let input = CliInput::new(args)?;
33            let mut matches = get_matches(cmd::cmd_api(&api_manager, &input), raw_input.clone())?;
34
35            // Invoke the api call
36            while let Some((_, m)) = matches.subcommand() {
37                matches = m.clone();
38            }
39            let invoker = api_manager.build_invocation(&input, &matches)?;
40            let res = invoker.invoke(&client).await?;
41            return Ok(res);
42        }
43        _ => unreachable!("Exhausted list of subcommands and subcommand_required prevents `None`"),
44    }
45}
46
47#[cfg(target_arch = "wasm32")]
48pub fn get_matches(cmd: Command, input: Vec<String>) -> Result<ArgMatches> {
49    use anyhow::anyhow;
50    use clap::builder::Styles;
51    let cmd = cmd.styles(Styles::plain());
52    cmd.try_get_matches_from(input)
53        .map_err(|e| anyhow!("{}", e.render().ansi()))
54}
55
56#[cfg(not(target_arch = "wasm32"))]
57pub fn get_matches(cmd: Command, input: Vec<String>) -> Result<ArgMatches> {
58    Ok(cmd.get_matches_from(input))
59}