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
use crate::config::{Configuration, JobParameters, NetworkType};
use crate::Error;
use scheduler_service_lib::JobRunner;
use std::path::PathBuf;
use std::process::Command;

pub struct SnapshotJobRunner(pub Configuration);

impl SnapshotJobRunner {
    fn print_with_password_hidden(&self, command: &Command) {
        let log_command = format!("{:?}", command);
        let pass = format!("--db-pass {}", &self.0.voting_tools.db_pass);
        println!(
            "Running command: {} ",
            log_command.replace(&pass, "--db-pass ***")
        );
    }

    pub fn crate_snapshot_output_file_name(&self, tag: &Option<String>) -> String {
        const SNAPSHOT_FILE: &str = "snapshot.json";

        if let Some(tag) = tag {
            format!("{}_{}", tag, SNAPSHOT_FILE)
        } else {
            SNAPSHOT_FILE.to_string()
        }
    }
}

impl JobRunner<JobParameters, (), Error> for SnapshotJobRunner {
    fn start(&self, request: JobParameters, output_folder: PathBuf) -> Result<Option<()>, Error> {
        let mut command = self.0.voting_tools.command()?;
        if let NetworkType::Testnet(magic) = self.0.voting_tools.network {
            command.arg("--testnet-magic").arg(magic.to_string());
        };

        let output_filename = self.crate_snapshot_output_file_name(&request.tag);

        command
            .arg("--db")
            .arg(&self.0.voting_tools.db)
            .arg("--db-user")
            .arg(&self.0.voting_tools.db_user)
            .arg("--db-pass")
            .arg(&self.0.voting_tools.db_pass)
            .arg("--db-host")
            .arg(&self.0.voting_tools.db_host)
            .arg("--out-file")
            .arg(output_folder.join(output_filename));

        if let Some(slot_no) = request.slot_no {
            command.arg("--slot-no").arg(slot_no.to_string());
        }

        if let Some(additional_params) = &self.0.voting_tools.additional_params {
            command.args(additional_params);
        }

        self.print_with_password_hidden(&command);

        command.spawn()?.wait_with_output()?;
        Ok(Some(()))
    }
}