eio_okta_client/command/
standalone.rs

1use crate::client::Client;
2use crate::Options;
3use clap::{Args, Parser};
4use eio_okta_api::traits::{Endpoint, Pagination, Response};
5use serde::de::DeserializeOwned;
6use serde::Serialize;
7
8#[derive(Debug, Parser)]
9#[remain::sorted]
10pub struct Standalone<T: Args + Serialize + DeserializeOwned> {
11  #[command(flatten)]
12  client_options: Options,
13  #[command(flatten)]
14  endpoint: T,
15  #[arg(long)]
16  #[arg(help_heading = "Output Options")]
17  #[arg(help = "pretty-print JSON output?")]
18  pretty: bool,
19}
20
21impl<T> Standalone<T>
22where
23  T: Endpoint + Serialize + DeserializeOwned + Args,
24{
25  pub fn resource(self) -> Result<<T as Response>::Body, crate::Error> {
26    let Self {
27      endpoint,
28      client_options,
29      ..
30    } = self;
31
32    let client = Client::from(client_options);
33    client.call(endpoint)
34  }
35
36  pub fn resource_json(self) -> Result<String, crate::Error> {
37    let pretty = self.pretty;
38    let value = self.resource()?;
39    let json = if pretty {
40      serde_json::to_string_pretty(&value)
41    } else {
42      serde_json::to_string(&value)
43    }?;
44
45    Ok(json)
46  }
47}
48
49impl<T> Standalone<T>
50where
51  T: Endpoint + Pagination + Serialize + DeserializeOwned + Args,
52{
53  pub fn resources(self) -> Result<Vec<<T as Pagination>::Item>, crate::Error> {
54    let Self {
55      endpoint,
56      client_options,
57      ..
58    } = self;
59
60    let client = Client::from(client_options);
61    client.paginate(endpoint)
62  }
63
64  pub fn resources_json(self) -> Result<String, crate::Error> {
65    let pretty = self.pretty;
66    let value = self.resources()?;
67    let json = if pretty {
68      serde_json::to_string_pretty(&value)
69    } else {
70      serde_json::to_string(&value)
71    }?;
72
73    Ok(json)
74  }
75}