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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Copyright (c) 2023, MaidSafe.
// All rights reserved.
//
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use crate::{
    ansible::{
        generate_environment_inventory, provisioning::AnsibleProvisioner, AnsibleInventoryType,
        AnsibleRunner,
    },
    get_environment_type, get_genesis_multiaddr,
    s3::S3Repository,
    ssh::SshClient,
    terraform::TerraformRunner,
    BinaryOption, CloudProvider, EnvironmentType, TestnetDeployer,
};
use color_eyre::{eyre::eyre, Result};
use rand::seq::IteratorRandom;
use serde::{Deserialize, Serialize};
use sn_service_management::{NodeRegistry, ServiceStatus};
use std::{
    collections::{BTreeMap, BTreeSet},
    convert::From,
    fs::File,
    io::Write,
    net::{IpAddr, SocketAddr},
    path::PathBuf,
};

const DEFAULT_CONTACTS_COUNT: usize = 25;
const STOPPED_PEER_ID: &str = "-";
const TESTNET_BUCKET_NAME: &str = "sn-testnet";

pub struct DeploymentInventoryService {
    pub ansible_runner: AnsibleRunner,
    // It may seem strange to have both the runner and the provisioner, because the provisioner is
    // a wrapper around the runner, but it's for the purpose of sharing some code. More things
    // could go into the provisioner later, which may eliminate the need to have the runner.
    pub ansible_provisioner: AnsibleProvisioner,
    pub cloud_provider: CloudProvider,
    pub inventory_file_path: PathBuf,
    pub s3_repository: S3Repository,
    pub ssh_client: SshClient,
    pub terraform_runner: TerraformRunner,
    pub working_directory_path: PathBuf,
}

impl From<TestnetDeployer> for DeploymentInventoryService {
    fn from(item: TestnetDeployer) -> Self {
        let provider = match item.cloud_provider {
            CloudProvider::Aws => "aws",
            CloudProvider::DigitalOcean => "digital_ocean",
        };
        DeploymentInventoryService {
            ansible_runner: item.ansible_provisioner.ansible_runner.clone(),
            ansible_provisioner: item.ansible_provisioner.clone(),
            cloud_provider: item.cloud_provider,
            inventory_file_path: item
                .working_directory_path
                .join("ansible")
                .join("inventory")
                .join(format!("dev_inventory_{}.yml", provider)),
            s3_repository: item.s3_repository.clone(),
            ssh_client: item.ssh_client.clone(),
            terraform_runner: item.terraform_runner.clone(),
            working_directory_path: item.working_directory_path.clone(),
        }
    }
}

impl DeploymentInventoryService {
    /// Generate or retrieve the inventory for the deployment.
    ///
    /// If we're creating a new environment and there is no inventory yet, a empty inventory will
    /// be returned; otherwise the inventory will represent what is deployed currently.
    ///
    /// The `force` flag is used when the `deploy` command runs, to make sure that a new inventory
    /// is generated, because it's possible that an old one with the same environment name has been
    /// cached.
    ///
    /// The binary option will only be present on the first generation of the inventory, when the
    /// testnet is initially deployed. On any subsequent runs, we don't have access to the initial
    /// launch arguments. This means any branch specification is lost. In this case, we'll just
    /// retrieve the version numbers from the genesis node in the node registry. Most of the time
    /// it is the version numbers that will be of interest.
    pub async fn generate_or_retrieve_inventory(
        &self,
        name: &str,
        force: bool,
        binary_option: Option<BinaryOption>,
    ) -> Result<DeploymentInventory> {
        println!("======================================");
        println!("  Generating or Retrieving Inventory  ");
        println!("======================================");
        let inventory_path = get_data_directory()?.join(format!("{name}-inventory.json"));
        if inventory_path.exists() && !force {
            let inventory = DeploymentInventory::read(&inventory_path)?;
            return Ok(inventory);
        }

        // This allows for the inventory to be generated without a Terraform workspace to be
        // initialised, which is the case in the workflow for printing an inventory.
        if !force {
            let environments = self.terraform_runner.workspace_list()?;
            if !environments.contains(&name.to_string()) {
                return Err(eyre!("The '{}' environment does not exist", name));
            }
        }

        // The following operation is idempotent.
        generate_environment_inventory(
            name,
            &self.inventory_file_path,
            &self
                .working_directory_path
                .join("ansible")
                .join("inventory"),
        )
        .await?;

        let mut misc_vm_list = Vec::new();
        let genesis_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::Genesis, false)
            .await?;
        if genesis_inventory.is_empty() {
            println!("Genesis node does not exist: we are treating this as a new deployment");
            return Ok(DeploymentInventory::empty(
                name,
                binary_option
                    .ok_or_else(|| eyre!("For a new deployment the binary option must be set"))?,
            ));
        }
        misc_vm_list.push((genesis_inventory[0].0.clone(), genesis_inventory[0].1));

        let mut auditor_vm_list = Vec::new();
        let auditor_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::Auditor, true)
            .await?;
        for entry in auditor_inventory.iter() {
            auditor_vm_list.push((entry.0.clone(), entry.1));
        }

        let build_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::Build, false)
            .await?;
        if !build_inventory.is_empty() {
            misc_vm_list.push((build_inventory[0].0.clone(), build_inventory[0].1));
        }

        let mut node_vm_list = Vec::new();
        let nodes_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::Nodes, false)
            .await?;
        for entry in nodes_inventory.iter() {
            node_vm_list.push((entry.0.clone(), entry.1));
        }

        let mut bootstrap_vm_list = Vec::new();
        let bootstrap_nodes_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::BootstrapNodes, false)
            .await?;
        for entry in bootstrap_nodes_inventory.iter() {
            bootstrap_vm_list.push((entry.0.clone(), entry.1));
        }

        let mut uploader_vm_list = Vec::new();
        let uploader_inventory = self
            .ansible_runner
            .get_inventory(AnsibleInventoryType::Uploaders, false)
            .await?;
        for entry in uploader_inventory.iter() {
            uploader_vm_list.push((entry.0.clone(), entry.1));
        }

        println!("Retrieving node registries from all VMs...");
        let mut node_registries = Vec::new();
        let mut failed_node_registry_vms = Vec::new();

        let bootstrap_node_registries = self
            .ansible_provisioner
            .get_node_registries(&AnsibleInventoryType::BootstrapNodes)?;

        let generic_node_registries = self
            .ansible_provisioner
            .get_node_registries(&AnsibleInventoryType::Nodes)?;

        let genesis_node_registry = self
            .ansible_provisioner
            .get_node_registries(&AnsibleInventoryType::Genesis)?;

        let auditor_node_registry = self
            .ansible_provisioner
            .get_node_registries(&AnsibleInventoryType::Auditor)?;

        node_registries.extend(bootstrap_node_registries.retrieved_registries.clone());
        node_registries.extend(generic_node_registries.retrieved_registries.clone());
        node_registries.extend(genesis_node_registry.retrieved_registries);
        node_registries.extend(auditor_node_registry.retrieved_registries);

        failed_node_registry_vms.extend(bootstrap_node_registries.failed_vms);
        failed_node_registry_vms.extend(generic_node_registries.failed_vms);
        failed_node_registry_vms.extend(genesis_node_registry.failed_vms);
        failed_node_registry_vms.extend(auditor_node_registry.failed_vms);

        let safenode_rpc_endpoints: BTreeMap<String, SocketAddr> = node_registries
            .iter()
            .flat_map(|(_, inv)| {
                inv.nodes.iter().map(|node| {
                    let id = if let Some(peer_id) = node.peer_id {
                        peer_id.to_string().clone()
                    } else {
                        "-".to_string()
                    };
                    (id, node.rpc_socket_addr)
                })
            })
            .collect();

        let safenodemand_endpoints: Vec<SocketAddr> = node_registries
            .iter()
            .filter_map(|(_, reg)| reg.daemon.clone())
            .filter_map(|daemon| daemon.endpoint)
            .collect();

        let bootstrap_peers = bootstrap_node_registries
            .retrieved_registries
            .iter()
            .flat_map(|(_, reg)| {
                reg.nodes.iter().map(|node| {
                    if let Some(listen_addresses) = &node.listen_addr {
                        // It seems to be the case that the listening address with the public IP is
                        // always in the second position. If this ever changes, we could do some
                        // filtering to find the address that does not start with "127." or "10.".
                        listen_addresses[1].to_string()
                    } else {
                        "-".to_string()
                    }
                })
            })
            .collect::<Vec<String>>();
        let node_peers = generic_node_registries
            .retrieved_registries
            .iter()
            .flat_map(|(_, reg)| {
                reg.nodes.iter().map(|node| {
                    if let Some(listen_addresses) = &node.listen_addr {
                        // It seems to be the case that the listening address with the public IP is
                        // always in the second position. If this ever changes, we could do some
                        // filtering to find the address that does not start with "127." or "10.".
                        listen_addresses[1].to_string()
                    } else {
                        "-".to_string()
                    }
                })
            })
            .collect::<Vec<String>>();

        let binary_option = if let Some(binary_option) = binary_option {
            binary_option
        } else {
            let (_, genesis_node_registry) = node_registries
                .iter()
                .find(|(_, reg)| reg.faucet.is_some())
                .ok_or_else(|| eyre!("Unable to retrieve genesis node registry"))?;
            let faucet_version = &genesis_node_registry.faucet.as_ref().unwrap().version;
            let safenode_version = genesis_node_registry
                .nodes
                .first()
                .ok_or_else(|| eyre!("Unable to obtain the genesis node"))?
                .version
                .clone();
            let safenode_manager_version = genesis_node_registry
                .daemon
                .as_ref()
                .ok_or_else(|| eyre!("Unable to obtain the daemon"))?
                .version
                .clone();
            let (_, auditor_node_registry) = node_registries
                .iter()
                .find(|(_, reg)| reg.auditor.is_some())
                .ok_or_else(|| eyre!("Unable to retrieve auditor node registry"))?;
            let sn_auditor_version = &auditor_node_registry.auditor.as_ref().unwrap().version;

            BinaryOption::Versioned {
                safe_version: "0.0.1".parse()?, // todo: store safe version in the safenodeman registry?
                faucet_version: faucet_version.parse()?,
                safenode_version: safenode_version.parse()?,
                safenode_manager_version: safenode_manager_version.parse()?,
                sn_auditor_version: sn_auditor_version.parse()?,
            }
        };

        let (genesis_multiaddr, genesis_ip) =
            get_genesis_multiaddr(&self.ansible_runner, &self.ssh_client).await?;
        let environment_type = get_environment_type(name, &self.s3_repository).await?;
        let inventory = DeploymentInventory {
            auditor_vms: auditor_vm_list,
            binary_option,
            bootstrap_node_vms: bootstrap_vm_list,
            bootstrap_peers,
            environment_type,
            failed_node_registry_vms,
            faucet_address: format!("{genesis_ip}:8000"),
            genesis_multiaddr,
            name: name.to_string(),
            misc_vms: misc_vm_list,
            node_vms: node_vm_list,
            node_peers,
            rpc_endpoints: safenode_rpc_endpoints,
            safenodemand_endpoints,
            ssh_user: self.cloud_provider.get_ssh_user(),
            uploaded_files: Vec::new(),
            uploader_vms: uploader_vm_list,
        };
        Ok(inventory)
    }

    pub async fn upload_network_contacts(
        &self,
        inventory: &DeploymentInventory,
        contacts_file_name: Option<String>,
    ) -> Result<()> {
        let temp_dir_path = tempfile::tempdir()?.into_path();
        let temp_file_path = if let Some(file_name) = contacts_file_name {
            temp_dir_path.join(file_name)
        } else {
            temp_dir_path.join(inventory.name.clone())
        };

        let mut file = std::fs::File::create(&temp_file_path)?;
        let mut rng = rand::thread_rng();

        let bootstrap_peers_len = inventory.bootstrap_peers.len();
        for peer in inventory
            .bootstrap_peers
            .iter()
            .filter(|&peer| peer != STOPPED_PEER_ID)
            .cloned()
            .choose_multiple(&mut rng, DEFAULT_CONTACTS_COUNT)
        {
            writeln!(file, "{peer}",)?;
        }

        if DEFAULT_CONTACTS_COUNT > bootstrap_peers_len {
            for peer in inventory
                .node_peers
                .iter()
                .filter(|&peer| peer != STOPPED_PEER_ID)
                .cloned()
                .choose_multiple(&mut rng, DEFAULT_CONTACTS_COUNT - bootstrap_peers_len)
            {
                writeln!(file, "{peer}",)?;
            }
        }

        self.s3_repository
            .upload_file(TESTNET_BUCKET_NAME, &temp_file_path, true)
            .await?;

        Ok(())
    }
}

pub type VirtualMachine = (String, IpAddr);

#[derive(Clone)]
pub struct DeploymentNodeRegistries {
    pub inventory_type: AnsibleInventoryType,
    pub retrieved_registries: Vec<(String, NodeRegistry)>,
    pub failed_vms: Vec<String>,
}

impl DeploymentNodeRegistries {
    pub fn print(&self) {
        Self::print_banner(&self.inventory_type.to_string());
        for (vm_name, registry) in self.retrieved_registries.iter() {
            println!("{vm_name}:");
            for node in registry.nodes.iter() {
                println!(
                    "  {}: {} {}",
                    node.service_name,
                    node.version,
                    Self::format_status(&node.status)
                );
            }
        }
        if !self.failed_vms.is_empty() {
            println!(
                "Failed to retrieve node registries for {}:",
                self.inventory_type
            );
            for vm_name in self.failed_vms.iter() {
                println!("- {}", vm_name);
            }
        }
    }

    fn format_status(status: &ServiceStatus) -> String {
        match status {
            ServiceStatus::Running => "RUNNING".to_string(),
            ServiceStatus::Stopped => "STOPPED".to_string(),
            ServiceStatus::Added => "ADDED".to_string(),
            ServiceStatus::Removed => "REMOVED".to_string(),
        }
    }

    fn print_banner(text: &str) {
        let padding = 2;
        let text_width = text.len() + padding * 2;
        let border_chars = 2;
        let total_width = text_width + border_chars;
        let top_bottom = "═".repeat(total_width);

        println!("╔{}╗", top_bottom);
        println!("║ {:^width$} ║", text, width = text_width);
        println!("╚{}╝", top_bottom);
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeploymentInventory {
    pub auditor_vms: Vec<VirtualMachine>,
    pub binary_option: BinaryOption,
    pub bootstrap_node_vms: Vec<VirtualMachine>,
    pub bootstrap_peers: Vec<String>,
    pub environment_type: EnvironmentType,
    pub failed_node_registry_vms: Vec<String>,
    pub faucet_address: String,
    pub genesis_multiaddr: String,
    pub misc_vms: Vec<VirtualMachine>,
    pub name: String,
    pub node_vms: Vec<VirtualMachine>,
    pub node_peers: Vec<String>,
    pub rpc_endpoints: BTreeMap<String, SocketAddr>,
    pub safenodemand_endpoints: Vec<SocketAddr>,
    pub ssh_user: String,
    pub uploaded_files: Vec<(String, String)>,
    pub uploader_vms: Vec<VirtualMachine>,
}

impl DeploymentInventory {
    /// Create an inventory for a new deployment which is initially empty, other than the name and
    /// binary option, which will have been selected.
    pub fn empty(name: &str, binary_option: BinaryOption) -> DeploymentInventory {
        Self {
            binary_option,
            name: name.to_string(),
            auditor_vms: Vec::new(),
            bootstrap_node_vms: Vec::new(),
            bootstrap_peers: Vec::new(),
            environment_type: EnvironmentType::Development,
            genesis_multiaddr: String::new(),
            failed_node_registry_vms: Vec::new(),
            faucet_address: String::new(),
            misc_vms: Vec::new(),
            node_vms: Vec::new(),
            node_peers: Vec::new(),
            rpc_endpoints: BTreeMap::new(),
            safenodemand_endpoints: Vec::new(),
            ssh_user: "root".to_string(),
            uploaded_files: Vec::new(),
            uploader_vms: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.bootstrap_node_vms.is_empty() && self.node_vms.is_empty()
    }

    pub fn vm_list(&self) -> Vec<VirtualMachine> {
        let mut list = Vec::new();
        list.extend(self.bootstrap_node_vms.clone());
        list.extend(self.misc_vms.clone());
        list.extend(self.node_vms.clone());
        list
    }

    pub fn peers(&self) -> BTreeSet<String> {
        let mut list = BTreeSet::new();
        list.extend(self.bootstrap_peers.clone());
        list.extend(self.node_peers.clone());
        list
    }

    pub fn save(&self) -> Result<()> {
        let path = get_data_directory()?.join(format!("{}-inventory.json", self.name));
        let serialized_data = serde_json::to_string_pretty(self)?;
        let mut file = File::create(path)?;
        file.write_all(serialized_data.as_bytes())?;
        Ok(())
    }

    pub fn read(file_path: &PathBuf) -> Result<Self> {
        let data = std::fs::read_to_string(file_path)?;
        let deserialized_data: DeploymentInventory = serde_json::from_str(&data)?;
        Ok(deserialized_data)
    }

    pub fn add_uploaded_files(&mut self, uploaded_files: Vec<(String, String)>) {
        self.uploaded_files.extend_from_slice(&uploaded_files);
    }

    pub fn get_random_peer(&self) -> Option<String> {
        let mut rng = rand::thread_rng();
        self.peers().into_iter().choose(&mut rng)
    }

    pub fn bootstrap_node_count(&self) -> usize {
        self.bootstrap_peers.len() / self.bootstrap_node_vms.len()
    }

    pub fn node_count(&self) -> usize {
        self.node_peers.len() / self.node_vms.len()
    }

    pub fn print_report(&self) -> Result<()> {
        println!("**************************************");
        println!("*                                    *");
        println!("*          Inventory Report          *");
        println!("*                                    *");
        println!("**************************************");

        println!("Environment Name: {}", self.name);
        println!();
        match &self.binary_option {
            BinaryOption::BuildFromSource {
                repo_owner, branch, ..
            } => {
                println!("==============");
                println!("Branch Details");
                println!("==============");
                println!("Repo owner: {repo_owner}");
                println!("Branch name: {branch}");
                println!();
            }
            BinaryOption::Versioned {
                faucet_version,
                safe_version,
                safenode_version,
                safenode_manager_version,
                sn_auditor_version,
            } => {
                println!("===============");
                println!("Version Details");
                println!("===============");
                println!("faucet version: {faucet_version}");
                println!("safe version: {safe_version}");
                println!("safenode version: {safenode_version}");
                println!("safenode-manager version: {safenode_manager_version}");
                println!("sn_auditor version: {sn_auditor_version}");
                println!();
            }
        }

        println!("=============");
        println!("Bootstrap VMs");
        println!("=============");
        for vm in self.bootstrap_node_vms.iter() {
            println!("{}: {}", vm.0, vm.1);
        }
        println!("Nodes per VM: {}", self.bootstrap_node_count());
        println!("SSH user: {}", self.ssh_user);
        println!();

        println!("========");
        println!("Node VMs");
        println!("========");
        for vm in self.node_vms.iter() {
            println!("{}: {}", vm.0, vm.1);
        }
        println!("Nodes per VM: {}", self.node_count());
        println!("SSH user: {}", self.ssh_user);
        println!();

        println!("============");
        println!("Uploader VMs");
        println!("============");
        for vm in self.uploader_vms.iter() {
            println!("{}: {}", vm.0, vm.1);
        }
        println!("SSH user: {}", self.ssh_user);
        println!();

        println!("=========");
        println!("Other VMs");
        println!("=========");
        for vm in self.misc_vms.iter() {
            println!("{}: {}", vm.0, vm.1);
        }
        println!("SSH user: {}", self.ssh_user);
        println!();

        // Take the first peer from each VM. If you just take, say, the first 10 on the peer list,
        // they will all be from the same machine. They will be unique peers, but they won't look
        // very random.
        println!("============");
        println!("Sample Peers");
        println!("============");
        self.bootstrap_node_vms
            .iter()
            .chain(self.node_vms.iter())
            .map(|vm| vm.1.to_string())
            .for_each(|ip| {
                if let Some(peer) = self.peers().iter().find(|p| p.contains(&ip)) {
                    println!("{peer}");
                }
            });
        println!("Genesis: {}", self.genesis_multiaddr);
        let inventory_file_path =
            get_data_directory()?.join(format!("{}-inventory.json", self.name));
        println!(
            "The entire peer list can be found at {}",
            inventory_file_path.to_string_lossy()
        );
        println!();

        println!("==============");
        println!("Faucet Details");
        println!("==============");
        println!("Faucet address: {:?}", self.faucet_address);
        println!("Check the faucet:");
        println!(
            "safe --peer {} wallet get-faucet {:?}",
            self.genesis_multiaddr, self.faucet_address
        );
        println!();

        println!("===============");
        println!("Auditor Details");
        println!("===============");
        for vm in self.auditor_vms.iter() {
            println!("{}:4242", vm.1);
        }
        println!();

        if !self.uploaded_files.is_empty() {
            println!("Uploaded files:");
            for file in self.uploaded_files.iter() {
                println!("{}: {}", file.0, file.1);
            }
        }
        Ok(())
    }
}

pub fn get_data_directory() -> Result<PathBuf> {
    let path = dirs_next::data_dir()
        .ok_or_else(|| eyre!("Could not retrieve data directory"))?
        .join("safe")
        .join("testnet-deploy");
    if !path.exists() {
        std::fs::create_dir_all(path.clone())?;
    }
    Ok(path)
}