kanidm_cli/schema/
attr.rs

1use crate::{handle_client_error, KanidmClientParser, OpType, OutputMode, SchemaAttrOpt};
2use kanidm_proto::scim_v1::{ScimEntryGetQuery, ScimFilter};
3use std::str::FromStr;
4
5impl SchemaAttrOpt {
6    pub async fn exec(&self, opt: KanidmClientParser) {
7        match self {
8            Self::List => {
9                let client = opt.to_client(OpType::Read).await;
10
11                let attrs = match client.scim_schema_attribute_list(None).await {
12                    Ok(attrs) => attrs,
13                    Err(e) => {
14                        handle_client_error(e, opt.output_mode);
15                        return;
16                    }
17                };
18
19                for attr in attrs.resources {
20                    println!("---");
21                    println!("uuid: {}", attr.header.id);
22                    println!("attribute_name: {}", attr.attributename);
23                    println!("description: {}", attr.description);
24                    println!("multivalue: {}", attr.multivalue);
25                    println!("unique: {}", attr.unique);
26                    println!("syntax: {}", attr.syntax);
27                }
28            }
29            Self::Search { query } => {
30                let query = match ScimFilter::from_str(query) {
31                    Ok(query) => query,
32                    Err(err) => {
33                        error!("Invalid search query");
34                        error!(?err);
35                        return;
36                    }
37                };
38
39                let get_query = ScimEntryGetQuery {
40                    filter: Some(query),
41                    ..Default::default()
42                };
43
44                let client = opt.to_client(OpType::Read).await;
45
46                let attrs = match client.scim_schema_attribute_list(Some(get_query)).await {
47                    Ok(attrs) => attrs,
48                    Err(e) => {
49                        handle_client_error(e, opt.output_mode);
50                        return;
51                    }
52                };
53                match opt.output_mode {
54                    OutputMode::Json => {
55                        println!(
56                            "{}",
57                            serde_json::to_string(&attrs.resources)
58                                .expect("Failed to serialise attributes to JSON")
59                        );
60                    }
61                    OutputMode::Text => {
62                        let total = attrs.resources.len();
63                        for (index, attr) in attrs.resources.iter().enumerate() {
64                            println!("uuid: {}", attr.header.id);
65                            println!("attribute_name: {}", attr.attributename);
66                            println!("description: {}", attr.description);
67                            println!("multivalue: {}", attr.multivalue);
68                            println!("unique: {}", attr.unique);
69                            println!("syntax: {}", attr.syntax);
70                            if index < total - 1 {
71                                println!("---");
72                            }
73                        }
74                    }
75                }
76            }
77        }
78    }
79}