1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Code related to CLI things
//!

use clap::{arg, command, value_parser, Arg, ArgMatches};
use dialoguer::theme::ColorfulTheme;
use dialoguer::{Confirm, Input};
use tokio::io::AsyncWriteExt;
use tokio::sync::{mpsc, oneshot};
use tokio::time::sleep;

use crate::config::ConfigFile;
use crate::datastore::Command;
use crate::enums::SystemState;
use crate::zones::FileZone;

/// Handles the command-line arguments.
pub fn clap_parser() -> ArgMatches {
    command!()
        .arg(
            arg!(
                -c --config <FILE> "Sets a custom config file"
            )
            .required(false)
            .value_parser(value_parser!(String)),
        )
        .arg(
            Arg::new("configcheck")
                .short('t')
                .long("configcheck")
                .help("Check the config file, show it and then quit.")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("export_config")
                .long("export-default-config")
                .help("Export a default config file.")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("export_zone")
                .short('e')
                .long("export-zone")
                .help("Export a single zone.")
                .value_parser(value_parser!(String)),
        )
        .arg(
            Arg::new("import_zones")
                .short('i')
                .long("import-zones")
                .help("Import a single zone file.")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("import_zone")
                .long("import-zone")
                .help("Import a single zone from a file.")
                .value_parser(value_parser!(String)),
        )
        .arg(
            Arg::new("filename")
                .short('f')
                .long("filename")
                .help("Filename to save to (used in other commands).")
                .value_parser(value_parser!(String)),
        )
        .arg(
            Arg::new("add_admin")
                .long("add-admin")
                .help("Add a new admin user.")
                .action(clap::ArgAction::SetTrue),
        )
        .arg(
            Arg::new("use_zonefile")
                .long("using-zonefile")
                .help("Load the zone file into the DB on startup, typically used for testing.")
                .action(clap::ArgAction::SetTrue),
        )
        .get_matches()
}

/// Turns the clap inputs into actions.
pub async fn cli_commands(
    tx: mpsc::Sender<Command>,
    clap_results: &ArgMatches,
    zone_file: &Option<String>,
) -> Result<SystemState, String> {
    if clap_results.get_flag("add_admin") {
        let _ = add_admin_user(tx).await;
        return Ok(SystemState::ShuttingDown);
    }
    if clap_results.get_flag("export_config") {
        default_config();
        return Ok(SystemState::ShuttingDown);
    }

    // Load the specified zone file on startup
    if clap_results.get_flag("use_zonefile") {
        if let Some(zone_file) = zone_file {
            if let Err(error) = import_zones(tx.clone(), zone_file.to_owned(), None).await {
                log::error!("Failed to import zone file! {error:?}");
                return Ok(SystemState::ShuttingDown);
            }
        }
    }

    if let Some(zone_name) = clap_results.get_one::<String>("export_zone") {
        if let Some(output_filename) = clap_results.get_one::<String>("filename") {
            log::info!("Exporting zone {zone_name} to {output_filename}");
            let res = export_zone_file(tx, zone_name, output_filename).await;
            if let Err(err) = res {
                log::error!("{err}");
            }
            return Ok(SystemState::Export);
        } else {
            log::error!("You need to specify a a filename to save to.");
            return Ok(SystemState::ShuttingDown);
        }
    };

    if clap_results.get_flag("import_zones") {
        if let Some(filename) = clap_results.get_one::<String>("filename") {
            log::info!("Importing zones from {filename}");
            import_zones(tx, filename.to_owned(), None)
                .await
                .map_err(|e| format!("Error importing {filename}: {e:?}"))?;

            return Ok(SystemState::Import);
        } else {
            log::error!("You need to specify a a filename to save to.");
            return Ok(SystemState::ShuttingDown);
        }
    };
    if let Some(zone_name) = clap_results.get_one::<String>("import_zone") {
        if let Some(filename) = clap_results.get_one::<String>("filename") {
            log::info!("Importing zones from {filename}");
            import_zones(tx, filename.to_owned(), Some(zone_name.to_owned()))
                .await
                .map_err(|e| format!("Error importing {filename}: {e:?}"))?;

            return Ok(SystemState::Import);
        } else {
            log::error!("You need to specify a a filename to save to.");
            return Ok(SystemState::ShuttingDown);
        }
    };
    Ok(SystemState::Server)
}

/// Output a default configuration file, based on the [crate::config::ConfigFile] object.
pub fn default_config() {
    let output = match serde_json::to_string_pretty(&ConfigFile::default()) {
        Ok(value) => value,
        Err(_) => {
            log::error!("I don't know how, but we couldn't parse our own config file def.");
            "".to_string()
        }
    };
    println!("{output}");
}

/// Dump a zone to a file
pub async fn export_zone_file(
    tx: mpsc::Sender<Command>,
    zone_name: &String,
    filename: &String,
) -> Result<(), String> {
    // make a channel

    let (tx_oneshot, rx_oneshot) = oneshot::channel();
    let ds_req: Command = Command::GetZone {
        id: None,
        name: Some(zone_name.clone()),
        resp: tx_oneshot,
    };
    if let Err(error) = tx.send(ds_req).await {
        return Err(format!(
            "failed to send to datastore from export_zone_file {error:?}"
        ));
    };

    let zone: Option<FileZone> = match rx_oneshot.await {
        Ok(value) => value,
        Err(err) => return Err(format!("rx from ds failed {err:?}")),
    };
    eprintln!("Got filezone: {zone:?}");

    let zone_bytes = match zone {
        None => {
            log::warn!("Couldn't find the zone {zone_name}");
            return Ok(());
        }
        Some(zone) => serde_json::to_string_pretty(&zone).map_err(|err| {
            format!(
                "Failed to serialize zone {zone_name} to json: {err:?}",
                zone_name = zone_name,
                err = err
            )
        })?,
    };

    // open the file
    let mut file = tokio::fs::File::create(filename)
        .await
        .map_err(|e| format!("Failed to open file {e:?}"))?;
    // write the thing
    file.write_all(zone_bytes.as_bytes())
        .await
        .map_err(|e| format!("Failed to write file: {e:?}"))?;
    // make some cake

    Ok(())
}

/// Import zones from a file
pub async fn import_zones(
    tx: mpsc::Sender<Command>,
    filename: String,
    zone_name: Option<String>,
) -> Result<(), String> {
    let (tx_oneshot, mut rx_oneshot) = oneshot::channel();
    let msg = Command::ImportFile {
        filename,
        resp: tx_oneshot,
        zone_name,
    };
    if let Err(err) = tx.send(msg).await {
        log::error!("Failed to send message to datastore: {err:?}");
    }
    loop {
        let res = rx_oneshot.try_recv();
        match res {
            Err(error) => {
                if let oneshot::error::TryRecvError::Closed = error {
                    break;
                }
            }
            Ok(()) => break,
        };
        sleep(std::time::Duration::from_micros(500)).await;
    }
    Ok(())
    // rx_oneshot.await.map_err(|e| format!("Failed to receive result: {e:?}"))
}

/// Presents the CLI UI to add an admin user.
pub async fn add_admin_user(tx: mpsc::Sender<Command>) -> Result<(), ()> {
    // prompt for the username
    println!("Creating admin user, please enter their username from the identity provider");
    let username: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt("Username")
        .interact_text()
        .map_err(|e| {
            log::error!("Failed to get username from user: {e:?}");
        })?;

    println!(
        "The authentication reference is the unique user identifier in the Identity Provider."
    );
    let authref: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt("Authentication Reference:")
        .interact_text()
        .map_err(|e| {
            log::error!("Failed to get auth reference from user: {e:?}");
        })?;

    println!(
        r#"

Creating the following user:


Username: {username}
Authref:  {authref}

"#
    );
    // show the details and confirm them
    let confirm = Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Do these details look correct?")
        .interact_opt();

    match confirm {
        Ok(Some(true)) => {}
        Ok(Some(false)) | Ok(None) | Err(_) => {
            log::warn!("Cancelled user creation");
            return Err(());
        }
    }

    // create oneshot
    let (tx_oneshot, rx_oneshot) = oneshot::channel();

    let new_user = Command::CreateUser {
        username: username.clone(),
        authref: authref.clone(),
        admin: true,
        disabled: false,
        resp: tx_oneshot,
    };
    // send command
    if let Err(error) = tx.send(new_user).await {
        log::error!("Failed to send new user command for username {username:?}: {error:?}");
        return Err(());
    };
    // wait for the response
    match rx_oneshot.await {
        Ok(res) => match res {
            true => {
                log::info!("Successfully created user!");
                Ok(())
            }
            false => {
                log::error!("Failed to create user! Check datastore logs.");
                Err(())
            }
        },
        Err(error) => {
            log::debug!("Failed to rx result from datastore: {error:?}");
            Err(())
        }
    }
}