1use crate::{handle_client_error, DomainOpt, KanidmClientParser};
2use anyhow::{Context, Error};
3use kanidm_proto::{cli::OpType, internal::ImageValue};
4use std::fs::read;
5
6impl DomainOpt {
7 pub async fn exec(&self, opt: KanidmClientParser) {
8 match self {
9 DomainOpt::SetDisplayname(dopt) => {
10 eprintln!(
11 "Attempting to set the domain's display name to: {:?}",
12 dopt.new_display_name
13 );
14 let client = opt.to_client(OpType::Write).await;
15 match client
16 .idm_domain_set_display_name(&dopt.new_display_name)
17 .await
18 {
19 Ok(_) => println!("Success"),
20 Err(e) => handle_client_error(e, opt.output_mode),
21 }
22 }
23 DomainOpt::SetLdapMaxQueryableAttrs {
24 new_max_queryable_attrs,
25 } => {
26 eprintln!(
27 "Attempting to set the maximum number of queryable LDAP attributes to: {new_max_queryable_attrs:?}"
28 );
29 let client = opt.to_client(OpType::Write).await;
30 match client
31 .idm_domain_set_ldap_max_queryable_attrs(*new_max_queryable_attrs)
32 .await
33 {
34 Ok(_) => println!("Success"),
35 Err(e) => handle_client_error(e, opt.output_mode),
36 }
37 }
38 DomainOpt::SetLdapBasedn { new_basedn } => {
39 eprintln!("Attempting to set the domain's ldap basedn to: {new_basedn:?}");
40 let client = opt.to_client(OpType::Write).await;
41 match client.idm_domain_set_ldap_basedn(new_basedn).await {
42 Ok(_) => println!("Success"),
43 Err(e) => handle_client_error(e, opt.output_mode),
44 }
45 }
46 DomainOpt::SetLdapAllowUnixPasswordBind { enable } => {
47 let client = opt.to_client(OpType::Write).await;
48 match client.idm_set_ldap_allow_unix_password_bind(*enable).await {
49 Ok(_) => println!("Success"),
50 Err(e) => handle_client_error(e, opt.output_mode),
51 }
52 }
53 DomainOpt::SetAllowEasterEggs { enable } => {
54 let client = opt.to_client(OpType::Write).await;
55 match client.idm_set_domain_allow_easter_eggs(*enable).await {
56 Ok(_) => {
57 if *enable {
58 println!("Success 🎉 🥚 🎉")
59 } else {
60 println!("Success")
61 }
62 }
63 Err(e) => handle_client_error(e, opt.output_mode),
64 }
65 }
66 DomainOpt::Show => {
67 let client = opt.to_client(OpType::Read).await;
68 match client.idm_domain_get().await {
69 Ok(e) => println!("{e}"),
70 Err(e) => handle_client_error(e, opt.output_mode),
71 }
72 }
73 DomainOpt::RevokeKey { key_id } => {
74 let client = opt.to_client(OpType::Write).await;
75 match client.idm_domain_revoke_key(key_id).await {
76 Ok(_) => println!("Success"),
77 Err(e) => handle_client_error(e, opt.output_mode),
78 }
79 }
80 DomainOpt::SetImage { path, image_type } => {
81 let client = opt.to_client(OpType::Write).await;
82 let img_res: Result<ImageValue, Error> = (move || {
83 let file_name = path
84 .file_name()
85 .context("Please pass a file")?
86 .to_str()
87 .context("Path contains non utf-8")?
88 .to_string();
89
90 let image_type = match image_type {
91 Some(val) => val.clone(),
92 None => {
93 path
94 .extension().context("Path has no extension so we can't infer the imageType, or you could pass the optional imageType argument yourself.")?
95 .to_str().context("Path contains invalid utf-8")?
96 .try_into()
97 .map_err(Error::msg)?
98 }
99 };
100
101 let read_res = read(path);
102 match read_res {
103 Ok(data) => Ok(ImageValue::new(file_name, image_type, data)),
104 Err(err) => {
105 if opt.debug {
106 eprintln!(
107 "{}",
108 kanidm_lib_file_permissions::diagnose_path(path.as_ref())
109 );
110 }
111 Err(err).context(format!("Failed to read file at '{}'", path.display()))
112 }
113 }
114 })();
115
116 let img = match img_res {
117 Ok(img) => img,
118 Err(err) => {
119 eprintln!("{err}");
120 return;
121 }
122 };
123
124 match client.idm_domain_update_image(img).await {
125 Ok(_) => println!("Success"),
126 Err(e) => handle_client_error(e, opt.output_mode),
127 }
128 }
129 DomainOpt::RemoveImage => {
130 let client = opt.to_client(OpType::Write).await;
131
132 match client.idm_domain_delete_image().await {
133 Ok(_) => println!("Success"),
134 Err(e) => handle_client_error(e, opt.output_mode),
135 }
136 }
137 }
138 }
139}