kanidm_cli/
raw.rs

1use crate::{KanidmClientParser, OutputMode, RawOpt};
2use kanidm_proto::cli::OpType;
3use kanidm_proto::scim_v1::{
4    client::{ScimEntryPostGeneric, ScimEntryPutGeneric},
5    ScimEntryGetQuery,
6};
7use serde::de::DeserializeOwned;
8use std::error::Error;
9use std::fs::File;
10use std::io::BufReader;
11use std::path::Path;
12
13fn read_file<T: DeserializeOwned, P: AsRef<Path>>(path: P) -> Result<T, Box<dyn Error>> {
14    let f = File::open(path)?;
15    let r = BufReader::new(f);
16
17    Ok(serde_json::from_reader(r)?)
18}
19
20impl RawOpt {
21    pub async fn exec(&self, opt: KanidmClientParser) {
22        match self {
23            RawOpt::Search { filter } => {
24                let client = opt.to_client(OpType::Read).await;
25
26                let query = ScimEntryGetQuery {
27                    filter: Some(filter.clone()),
28                    ..Default::default()
29                };
30
31                match client.scim_v1_entry_query(query).await {
32                    Ok(rset) => match opt.output_mode {
33                        #[allow(clippy::expect_used)]
34                        OutputMode::Json => {
35                            println!(
36                                "{}",
37                                serde_json::to_string(&rset).expect("Failed to serialize entry!")
38                            )
39                        }
40                        OutputMode::Text => {
41                            println!(
42                                "{}",
43                                serde_json::to_string_pretty(&rset)
44                                    .expect("Failed to serialize entry!")
45                            )
46                        }
47                    },
48                    Err(e) => error!("Error -> {:?}", e),
49                }
50            }
51            RawOpt::Create { file } => {
52                let client = opt.to_client(OpType::Write).await;
53                // Read the file?
54                let entry: ScimEntryPostGeneric = match read_file(file) {
55                    Ok(r) => r,
56                    Err(e) => {
57                        error!("Error -> {:?}", e);
58                        return;
59                    }
60                };
61
62                if let Err(e) = client.scim_v1_entry_create(entry).await {
63                    error!("Error -> {:?}", e);
64                }
65            }
66            RawOpt::Update { file } => {
67                let client = opt.to_client(OpType::Write).await;
68
69                let entry: ScimEntryPutGeneric = match read_file(file) {
70                    Ok(r) => r,
71                    Err(e) => {
72                        error!("Error -> {:?}", e);
73                        return;
74                    }
75                };
76
77                if let Err(e) = client.scim_v1_entry_update(entry).await {
78                    error!("Error -> {:?}", e);
79                }
80            }
81            RawOpt::Delete { id } => {
82                let client = opt.to_client(OpType::Write).await;
83
84                if let Err(e) = client.scim_v1_entry_delete(id).await {
85                    error!("Error -> {:?}", e);
86                }
87            }
88        }
89    }
90}