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
use anyhow::{Context, Result};
use std::fs;

#[cfg(test)]
mod tests {
    use super::merge_yaml_strings;

    fn trim_start_lines(s: &str) -> String {
        s.lines()
            .map(|l| l.trim_start())
            .collect::<Vec<&str>>()
            .join("\n")
    }

    #[test]
    fn yaml_merges_arrays() {
        let yaml_a = r#"
          list:
            - a
        "#;

        let yaml_b = r#"
          list:
            - b
        "#;

        let result = merge_yaml_strings(&vec![yaml_a.to_string(), yaml_b.to_string()]).unwrap();
        let expected = r#"{
          "list": [
            "a",
            "b"
          ]
        }"#;
        assert_eq!(trim_start_lines(&expected), trim_start_lines(&result));
    }

    #[test]
    fn yaml_merges_dictionaries() {
        let yaml_a = r#"
          dict:
            a: true
        "#;

        let yaml_b = r#"
          dict:
            b: true
        "#;

        let result = merge_yaml_strings(&vec![yaml_a.to_string(), yaml_b.to_string()]).unwrap();
        let expected = r#"{
            "dict": {
                "a": true,
                "b": true
            }
        }"#;
        assert_eq!(trim_start_lines(&expected), trim_start_lines(&result));
    }

    #[test]
    fn yaml_merges_deeply() {
        let yaml_a = r#"
          dict:
            list:
                - a
        "#;

        let yaml_b = r#"
          dict:
            list:
            - b 
        "#;

        let result = merge_yaml_strings(&vec![yaml_a.to_string(), yaml_b.to_string()]).unwrap();
        let expected = r#"{
            "dict": {
                "list": [
                    "a",
                    "b"
                ]
            }
        }"#;
        assert_eq!(trim_start_lines(&expected), trim_start_lines(&result));
    }
}

pub fn merge_yaml_strings(inputs: &Vec<String>) -> Result<String> {
    let mut result: serde_json::Value = serde_json::from_str("{}")?;

    for i in inputs {
        let i: serde_json::Value =
            serde_yaml::from_str(&i).with_context(|| format!("Failed to parse file {}", i))?;
        result = merge_struct::merge(&result, &i)?;
    }

    return serde_json::to_string_pretty(&result)
        .with_context(|| "converting json to pretty string");
}

pub fn merge_yaml_files(inputs: &Vec<String>, output: &str) -> Result<()> {
    eprintln!("Will read from:");
    for i in inputs {
        eprintln!("- {}", i);
    }
    eprintln!("And write to: {}", output);

    let mut input_contents = Vec::new();

    for i in inputs {
        let contents =
            fs::read_to_string(i).with_context(|| format!("Failed to read file {}", i))?;
        input_contents.push(contents);
    }

    let result = merge_yaml_strings(inputs).with_context(|| format!("Failed to merge yaml"))?;
    fs::write(output, result).with_context(|| format!("Failed to write to {}", output))?;

    return Ok(());
}