• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 5888798292

17 Aug 2023 08:51AM UTC coverage: 76.025% (-1.4%) from 77.415%
5888798292

push

github

web-flow
feat: implement graph on live ui (#1847)

* feat: implement progress

* feat: implement enable progress flag

* feat: implement progress in live

* chore: fix clippy

* chore: always use telemetry metrics

* fix: Only run build once

---------

Co-authored-by: sagar <sagar@getdozer.io>
Co-authored-by: chubei <914745487@qq.com>

536 of 536 new or added lines in 21 files covered. (100.0%)

46101 of 60639 relevant lines covered (76.03%)

40410.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

24.64
/dozer-cli/src/cli/helper.rs
1
use crate::config_helper::combine_config;
2
use crate::errors::CliError;
3
use crate::errors::CliError::{ConfigurationFilePathNotProvided, FailedToFindConfigurationFiles};
4
use crate::errors::ConfigCombineError::CannotReadConfig;
5
use crate::errors::OrchestrationError;
6
use crate::simple::SimpleOrchestrator as Dozer;
7
use atty::Stream;
8
use dozer_types::models::config::default_cache_max_map_size;
9
use dozer_types::prettytable::{row, Table};
10
use dozer_types::{models::config::Config, serde_yaml};
11
use handlebars::Handlebars;
12
use std::collections::BTreeMap;
13
use std::io::{self, Read};
14
use std::path::PathBuf;
15
use std::sync::Arc;
16
use tokio::runtime::Runtime;
17

18
pub async fn init_dozer(
×
19
    runtime: Arc<Runtime>,
×
20
    config_paths: Vec<String>,
×
21
    config_token: Option<String>,
×
22
    config_overrides: Vec<(String, serde_json::Value)>,
×
23
    ignore_pipe: bool,
×
24
    enable_progress: bool,
×
25
) -> Result<Dozer, CliError> {
×
26
    let mut config = load_config(config_paths, config_token, ignore_pipe).await?;
×
27

×
28
    config = apply_overrides(&config, config_overrides)?;
×
29

×
30
    let cache_max_map_size = config
×
31
        .cache_max_map_size
×
32
        .unwrap_or_else(default_cache_max_map_size);
×
33
    let page_size = page_size::get() as u64;
×
34
    config.cache_max_map_size = Some(cache_max_map_size / page_size * page_size);
×
35

×
36
    Ok(Dozer::new(config, runtime, enable_progress))
×
37
}
×
38

×
39
pub async fn list_sources(
×
40
    runtime: Arc<Runtime>,
×
41
    config_paths: Vec<String>,
×
42
    config_token: Option<String>,
×
43
    config_overrides: Vec<(String, serde_json::Value)>,
×
44
    ignore_pipe: bool,
×
45
    filter: Option<String>,
×
46
) -> Result<(), OrchestrationError> {
×
47
    let dozer = init_dozer(
×
48
        runtime,
×
49
        config_paths,
×
50
        config_token,
×
51
        config_overrides,
×
52
        ignore_pipe,
×
53
        false,
×
54
    )
×
55
    .await?;
×
56
    let connection_map = dozer.list_connectors()?;
×
57
    let mut table_parent = Table::new();
×
58
    for (connection_name, (tables, schemas)) in connection_map {
×
59
        let mut first_table_found = false;
×
60

×
61
        for (table, schema) in tables.into_iter().zip(schemas) {
×
62
            let name = table.schema.map_or(table.name.clone(), |schema_name| {
×
63
                format!("{schema_name}.{}", table.name)
×
64
            });
×
65

×
66
            if filter
×
67
                .as_ref()
×
68
                .map_or(true, |name_part| name.contains(name_part))
×
69
            {
70
                if !first_table_found {
×
71
                    table_parent.add_row(row!["Connection", "Table", "Columns"]);
×
72
                    first_table_found = true;
×
73
                }
×
74
                let schema_table = schema.schema.print();
×
75

×
76
                table_parent.add_row(row![connection_name, name, schema_table]);
×
77
            }
×
78
        }
×
79

×
80
        if first_table_found {
×
81
            table_parent.add_empty_row();
×
82
        }
×
83
    }
×
84
    table_parent.printstd();
×
85
    Ok(())
×
86
}
×
87

×
88
async fn load_config(
×
89
    config_url_or_paths: Vec<String>,
×
90
    config_token: Option<String>,
×
91
    ignore_pipe: bool,
×
92
) -> Result<Config, CliError> {
×
93
    let read_stdin = atty::isnt(Stream::Stdin) && !ignore_pipe;
×
94
    let first_config_path = config_url_or_paths.get(0);
×
95
    match first_config_path {
×
96
        None => Err(ConfigurationFilePathNotProvided),
×
97
        Some(path) => {
×
98
            if path.starts_with("https://") || path.starts_with("http://") {
×
99
                load_config_from_http_url(path, config_token).await
×
100
            } else {
×
101
                load_config_from_file(config_url_or_paths, read_stdin)
×
102
            }
×
103
        }
×
104
    }
×
105
}
×
106

×
107
async fn load_config_from_http_url(
×
108
    config_url: &str,
×
109
    config_token: Option<String>,
×
110
) -> Result<Config, CliError> {
×
111
    let client = reqwest::Client::new();
×
112
    let mut get_request = client.get(config_url);
×
113
    if let Some(token) = config_token {
×
114
        get_request = get_request.bearer_auth(token);
×
115
    }
×
116
    let response: reqwest::Response = get_request.send().await?.error_for_status()?;
×
117
    let contents = response.text().await?;
×
118
    parse_config(&contents)
×
119
}
×
120

×
121
pub fn load_config_from_file(
×
122
    config_path: Vec<String>,
×
123
    read_stdin: bool,
×
124
) -> Result<Config, CliError> {
×
125
    let stdin_path = PathBuf::from("<stdin>");
×
126
    let input = if read_stdin {
×
127
        let mut input = String::new();
×
128
        io::stdin()
×
129
            .read_to_string(&mut input)
×
130
            .map_err(|e| CannotReadConfig(stdin_path, e))?;
×
131
        Some(input)
×
132
    } else {
133
        None
×
134
    };
×
135

×
136
    let config_template = combine_config(config_path.clone(), input)?;
×
137
    match config_template {
×
138
        Some(template) => parse_config(&template),
×
139
        None => Err(FailedToFindConfigurationFiles(config_path.join(", "))),
×
140
    }
141
}
×
142

×
143
fn parse_config(config_template: &str) -> Result<Config, CliError> {
×
144
    let mut handlebars = Handlebars::new();
×
145
    handlebars
×
146
        .register_template_string("config", config_template)
×
147
        .map_err(|e| CliError::FailedToParseYaml(Box::new(e)))?;
×
148

149
    let mut data = BTreeMap::new();
×
150

×
151
    for (key, value) in std::env::vars() {
×
152
        data.insert(key, value);
×
153
    }
×
154

155
    let config_str = handlebars
×
156
        .render("config", &data)
×
157
        .map_err(|e| CliError::FailedToParseYaml(Box::new(e)))?;
×
158

×
159
    let config: Config = serde_yaml::from_str(&config_str)
×
160
        .map_err(|e: serde_yaml::Error| CliError::FailedToParseYaml(Box::new(e)))?;
×
161

162
    Ok(config)
×
163
}
×
164

×
165
/// Convert `config` to JSON, apply JSON pointer overrides, then convert back to `Config`.
×
166
fn apply_overrides(
2✔
167
    config: &Config,
2✔
168
    config_overrides: Vec<(String, serde_json::Value)>,
2✔
169
) -> Result<Config, CliError> {
2✔
170
    let mut config_json = serde_json::to_value(config).map_err(CliError::SerializeConfigToJson)?;
2✔
171

×
172
    for (pointer, value) in config_overrides {
4✔
173
        if let Some(pointee) = config_json.pointer_mut(&pointer) {
2✔
174
            *pointee = value;
2✔
175
        } else {
2✔
176
            return Err(CliError::MissingConfigOverride(pointer));
×
177
        }
×
178
    }
179

180
    // Directly convert `config_json` to `Config` fails, not sure why.
181
    let config_json_string =
2✔
182
        serde_json::to_string(&config_json).map_err(CliError::SerializeConfigToJson)?;
2✔
183
    let config: Config =
2✔
184
        serde_json::from_str(&config_json_string).map_err(CliError::DeserializeConfigFromJson)?;
2✔
185

186
    Ok(config)
2✔
187
}
2✔
188

189
pub const LOGO: &str = r"
190
.____   ___ __________ ____
191
|  _ \ / _ \__  / ____|  _ \
192
| | | | | | |/ /|  _| | |_) |
193
| |_| | |_| / /_| |___|  _ <
194
|____/ \___/____|_____|_| \_\
195
";
196

197
pub const DESCRIPTION: &str = r#"Open-source platform to build, publish and manage blazing-fast real-time data APIs in minutes. 
198

×
199
 If no sub commands are passed, dozer will bring up both app and api services.
×
200
"#;
×
201

×
202
#[cfg(test)]
×
203
mod tests {
×
204
    use dozer_types::models::{api_config::ApiConfig, api_security::ApiSecurity};
×
205

×
206
    use super::*;
×
207

×
208
    #[test]
1✔
209
    fn test_override_top_level() {
1✔
210
        let mut config = Config {
1✔
211
            app_name: "test_override_top_level".to_string(),
1✔
212
            ..Default::default()
1✔
213
        };
1✔
214
        config.sql = Some("sql1".to_string());
1✔
215
        let sql = "sql2".to_string();
1✔
216
        let config = apply_overrides(
1✔
217
            &config,
1✔
218
            vec![("/sql".to_string(), serde_json::to_value(&sql).unwrap())],
1✔
219
        )
1✔
220
        .unwrap();
1✔
221
        assert_eq!(config.sql.unwrap(), sql);
1✔
222
    }
1✔
223

×
224
    #[test]
1✔
225
    fn test_override_nested() {
1✔
226
        let mut config = Config {
1✔
227
            app_name: "test_override_nested".to_string(),
1✔
228
            ..Default::default()
1✔
229
        };
1✔
230
        config.api = Some(ApiConfig {
1✔
231
            api_security: Some(ApiSecurity::Jwt("secret1".to_string())),
1✔
232
            ..Default::default()
1✔
233
        });
1✔
234
        let api_security = ApiSecurity::Jwt("secret2".to_string());
1✔
235
        let config = apply_overrides(
1✔
236
            &config,
1✔
237
            vec![(
1✔
238
                "/api/api_security".to_string(),
1✔
239
                serde_json::to_value(&api_security).unwrap(),
1✔
240
            )],
1✔
241
        )
1✔
242
        .unwrap();
1✔
243
        assert_eq!(config.api.unwrap().api_security.unwrap(), api_security);
1✔
244
    }
1✔
245
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc