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
use crate::common::startup::get_exe;
use std::path::Path;
use std::{path::PathBuf, process::Command};

/// In order to test robustness of server bootstrapper we need to be able
/// to provide some
pub struct BootstrapCommandBuilder {
    exe: PathBuf,
    address: Option<String>,
    allowed_origins: Option<String>,
    block0_path: Option<String>,
    block0_paths: Option<String>,
    cert_file: Option<PathBuf>,
    db_url: Option<String>,
    enable_api_tokens: bool,
    in_settings_file: Option<PathBuf>,
    max_age_secs: Option<u32>,
    out_settings_file: Option<PathBuf>,
    priv_key_file: Option<PathBuf>,
    log_file: Option<PathBuf>,
    log_level: Option<String>,
    service_version: Option<String>,
}

impl Default for BootstrapCommandBuilder {
    fn default() -> Self {
        Self::new(get_exe())
    }
}

impl BootstrapCommandBuilder {
    pub fn new(exe: PathBuf) -> Self {
        Self {
            exe,
            address: None,
            allowed_origins: None,
            block0_path: None,
            block0_paths: None,
            cert_file: None,
            db_url: None,
            enable_api_tokens: false,
            in_settings_file: None,
            max_age_secs: None,
            out_settings_file: None,
            priv_key_file: None,
            log_file: None,
            log_level: None,
            service_version: None,
        }
    }

    pub fn address<S: Into<String>>(&mut self, address: S) -> &mut Self {
        self.address = Some(address.into());
        self
    }

    pub fn allowed_origins<S: Into<String>>(&mut self, allowed_origins: S) -> &mut Self {
        self.allowed_origins = Some(allowed_origins.into());
        self
    }

    pub fn block0_path(&mut self, block0_path: Option<String>) -> &mut Self {
        self.block0_path = block0_path;
        self
    }

    pub fn block0_paths(&mut self, block0_paths: Option<String>) -> &mut Self {
        self.block0_paths = block0_paths;
        self
    }

    pub fn cert_file(&mut self, cert_file: &Path) -> &mut Self {
        self.cert_file = Some(cert_file.to_path_buf());
        self
    }

    pub fn db_url<S: Into<String>>(&mut self, db_url: S) -> &mut Self {
        self.db_url = Some(db_url.into());
        self
    }

    pub fn enable_api_tokens(&mut self, enabled: bool) -> &mut Self {
        self.enable_api_tokens = enabled;
        self
    }

    pub fn in_settings_file(&mut self, in_settings_file: &Path) -> &mut Self {
        self.in_settings_file = Some(in_settings_file.to_path_buf());
        self
    }
    pub fn max_age_secs(&mut self, max_age_secs: u32) -> &mut Self {
        self.max_age_secs = Some(max_age_secs);
        self
    }
    pub fn out_settings_file(&mut self, out_settings_file: &Path) -> &mut Self {
        self.out_settings_file = Some(out_settings_file.to_path_buf());
        self
    }

    pub fn priv_key_file(&mut self, priv_key_file: &Path) -> &mut Self {
        self.priv_key_file = Some(priv_key_file.to_path_buf());
        self
    }

    pub fn log_file(&mut self, log_file: &Path) -> &mut Self {
        self.log_file = Some(log_file.to_path_buf());
        self
    }

    pub fn log_level(&mut self, log_level: &str) -> &mut Self {
        self.log_level = Some(log_level.to_string());
        self
    }

    pub fn service_version<S: Into<String>>(&mut self, service_version: S) -> &mut Self {
        self.service_version = Some(service_version.into());
        self
    }

    pub fn build(&self) -> Command {
        let mut command = Command::new(self.exe.clone());

        let service_version = if let Some(service_version) = &self.service_version {
            service_version.clone()
        } else {
            Default::default()
        };
        command.arg("--service-version").arg(service_version);

        if let Some(address) = &self.address {
            command.arg("--address").arg(address);
        }

        if let Some(allowed_origins) = &self.allowed_origins {
            command.arg("--allowed-origins").arg(allowed_origins);
        }

        if let Some(block0_path) = &self.block0_path {
            command.arg("--block0-path").arg(block0_path);
        }

        if let Some(block0_paths) = &self.block0_paths {
            command.arg("--block0-paths").arg(block0_paths);
        }

        if let Some(cert_file) = &self.cert_file {
            command.arg("--cert-file").arg(cert_file.to_str().unwrap());
        }

        if let Some(db_url) = &self.db_url {
            command.arg("--db-url").arg(db_url);
        }

        if let Some(in_settings_file) = &self.in_settings_file {
            command
                .arg("--in-settings-file")
                .arg(in_settings_file.to_str().unwrap());
        }

        if let Some(max_age_secs) = &self.max_age_secs {
            command.arg("--max-age-secs").arg(max_age_secs.to_string());
        }

        if let Some(out_settings_file) = &self.out_settings_file {
            command
                .arg("--out-settings-file")
                .arg(out_settings_file.to_str().unwrap());
        }

        if let Some(priv_key_file) = &self.priv_key_file {
            command
                .arg("--priv-key-file")
                .arg(priv_key_file.to_str().unwrap());
        }

        if self.enable_api_tokens {
            command.arg("--enable-api-tokens");
        }

        if let Some(log_file) = &self.log_file {
            command
                .arg("--log-output-path")
                .arg(log_file.to_str().unwrap());
        }

        if let Some(log_level) = &self.log_level {
            command.arg("--log-level").arg(log_level);
        }

        command
    }
}