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

getdozer / dozer / 5829357348

pending completion
5829357348

Pull #1839

github

aaryaattrey
typo
Pull Request #1839: load config from stdin

37 of 37 new or added lines in 4 files covered. (100.0%)

45551 of 59108 relevant lines covered (77.06%)

39686.65 hits per line

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

32.43
/dozer-cli/src/config_helper.rs
1
use crate::errors::ConfigCombineError;
2
use crate::errors::ConfigCombineError::{
3
    CannotReadConfig, CannotReadFile, CannotSerializeToString, SqlIsNotStringType,
4
    WrongPatternOfConfigFilesGlob,
5
};
6
use dozer_types::log::warn;
7
use dozer_types::serde_yaml;
8
use dozer_types::serde_yaml::mapping::Entry;
9
use dozer_types::serde_yaml::{Mapping, Value};
10
use glob::glob;
11
use std::fs;
12

13
pub fn combine_config(
×
14
    config_paths: Vec<String>,
×
15
    stdin_yaml: Option<String>,
×
16
) -> Result<Option<String>, ConfigCombineError> {
×
17
    let mut combined_yaml = serde_yaml::Value::Mapping(Mapping::new());
×
18

×
19
    let mut config_found = false;
×
20
    for pattern in config_paths {
×
21
        let files_glob = glob(&pattern).map_err(WrongPatternOfConfigFilesGlob)?;
×
22

×
23
        for entry in files_glob {
×
24
            let path = entry.map_err(CannotReadFile)?;
×
25
            match path.clone().to_str() {
×
26
                None => {
×
27
                    warn!("[Config] Path {:?} is not valid", path)
×
28
                }
×
29
                Some(name) => {
×
30
                    let content =
×
31
                        fs::read_to_string(path.clone()).map_err(|e| CannotReadConfig(path, e))?;
×
32

×
33
                    if name.contains(".yml") || name.contains(".yaml") {
×
34
                        config_found = true;
×
35
                    }
×
36

37
                    add_file_content_to_config(&mut combined_yaml, name, content)?;
×
38
                }
39
            }
40
        }
×
41
    }
42
    let stdin_name = "Stdin";
×
43
    // Merge stdin_yaml content into combined_yaml if provided
×
44
    if let Some(stdin_content) = stdin_yaml {
×
45
        let stdin_yaml: serde_yaml::Value = serde_yaml::from_str(&stdin_content)
×
46
            .map_err(|e| ConfigCombineError::ParseYaml(stdin_name.to_string(), e))?; //deserialise yaml content from stdin
×
47
        merge_yaml(stdin_yaml, &mut combined_yaml)?; //merge with yaml from config-paths
×
48
    }
×
49

50
    if config_found {
×
51
        // `serde_yaml::from_value` will return deserialization error, not sure why.
×
52
        serde_yaml::to_string(&combined_yaml)
×
53
            .map_err(CannotSerializeToString)
×
54
            .map(Some)
×
55
    } else {
×
56
        Ok(None)
×
57
    }
×
58
}
×
59

×
60
pub fn add_file_content_to_config(
5✔
61
    combined_yaml: &mut serde_yaml::Value,
5✔
62
    name: &str,
5✔
63
    content: String,
5✔
64
) -> Result<(), ConfigCombineError> {
5✔
65
    if name.contains(".yml") || name.contains(".yaml") {
5✔
66
        let yaml: serde_yaml::Value = serde_yaml::from_str(&content)
3✔
67
            .map_err(|e| ConfigCombineError::ParseYaml(name.to_string(), e))?;
3✔
68
        merge_yaml(yaml, combined_yaml)?;
3✔
69
    } else if name.contains(".sql") {
2✔
70
        let mapping = combined_yaml.as_mapping_mut().expect("Should be mapping");
2✔
71
        let sql = mapping.get_mut(serde_yaml::Value::String("sql".into()));
2✔
72

2✔
73
        match sql {
2✔
74
            None => {
1✔
75
                mapping.insert(
1✔
76
                    serde_yaml::Value::String("sql".into()),
1✔
77
                    serde_yaml::Value::String(content),
1✔
78
                );
1✔
79
            }
1✔
80
            Some(s) => {
1✔
81
                let query = s.as_str();
1✔
82
                *s = match query {
1✔
83
                    None => {
×
84
                        return Err(SqlIsNotStringType);
×
85
                    }
86
                    Some(current_query) => {
1✔
87
                        Value::String(format!("{};{}", current_query, content.as_str()))
1✔
88
                    }
89
                }
90
            }
91
        }
92
    } else {
93
        warn!("Config file \"{name}\" extension not supported");
×
94
    }
×
95

×
96
    Ok(())
5✔
97
}
5✔
98

×
99
pub fn merge_yaml(
100
    from: serde_yaml::Value,
×
101
    to: &mut serde_yaml::Value,
×
102
) -> Result<(), ConfigCombineError> {
×
103
    match (from, to) {
3✔
104
        (serde_yaml::Value::Mapping(from), serde_yaml::Value::Mapping(to)) => {
3✔
105
            for (key, value) in from {
8✔
106
                match to.entry(key) {
5✔
107
                    Entry::Occupied(mut entry) => {
×
108
                        merge_yaml(value, entry.get_mut())?;
×
109
                    }
×
110
                    Entry::Vacant(entry) => {
5✔
111
                        entry.insert(value);
5✔
112
                    }
5✔
113
                }
×
114
            }
×
115
            Ok(())
3✔
116
        }
×
117
        (serde_yaml::Value::Sequence(from), serde_yaml::Value::Sequence(to)) => {
×
118
            for value in from {
×
119
                to.push(value);
×
120
            }
×
121
            Ok(())
×
122
        }
×
123
        (serde_yaml::Value::Tagged(from), serde_yaml::Value::Tagged(to)) => {
×
124
            if from.tag != to.tag {
×
125
                return Err(ConfigCombineError::CannotMerge {
×
126
                    from: serde_yaml::Value::Tagged(from),
×
127
                    to: serde_yaml::Value::Tagged(to.clone()),
×
128
                });
×
129
            }
×
130
            merge_yaml(from.value, &mut to.value)
×
131
        }
132
        (from, to) => Err(ConfigCombineError::CannotMerge {
×
133
            from,
×
134
            to: to.clone(),
×
135
        }),
×
136
    }
137
}
3✔
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

© 2025 Coveralls, Inc