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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::config::{format, ConfigBuilder, Format};
use clap::Parser;
use colored::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;

#[derive(Parser, Debug)]
#[command(rename_all = "kebab-case")]
pub struct Opts {
    /// The input path. It can be a single file or a directory. If this points to a directory,
    /// all files with a "toml", "yaml" or "json" extension will be converted.
    pub(crate) input_path: PathBuf,

    /// The output file or directory to be created. This command will fail if the output directory exists.
    pub(crate) output_path: PathBuf,

    /// The target format to which existing config files will be converted to.
    #[arg(long, default_value = "yaml")]
    pub(crate) output_format: Format,
}

fn check_paths(opts: &Opts) -> Result<(), String> {
    let in_metadata = fs::metadata(&opts.input_path)
        .unwrap_or_else(|_| panic!("Failed to get metadata for: {:?}", &opts.input_path));

    if opts.output_path.exists() {
        return Err(format!(
            "Output path {:?} already exists. Please provide a non-existing output path.",
            opts.output_path
        ));
    }

    if opts.output_path.extension().is_none() {
        if in_metadata.is_file() {
            return Err(format!(
                "{:?} points to a file but {:?} points to a directory.",
                opts.input_path, opts.output_path
            ));
        }
    } else if in_metadata.is_dir() {
        return Err(format!(
            "{:?} points to a directory but {:?} points to a file.",
            opts.input_path, opts.output_path
        ));
    }

    Ok(())
}

pub(crate) fn cmd(opts: &Opts) -> exitcode::ExitCode {
    if let Err(e) = check_paths(opts) {
        #[allow(clippy::print_stderr)]
        {
            eprintln!("{}", e.red());
        }
        return exitcode::SOFTWARE;
    }

    return if opts.input_path.is_file() && opts.output_path.extension().is_some() {
        if let Some(base_dir) = opts.output_path.parent() {
            if !base_dir.exists() {
                fs::create_dir_all(base_dir).unwrap_or_else(|_| {
                    panic!("Failed to create output dir(s): {:?}", &opts.output_path)
                });
            }
        }

        match convert_config(&opts.input_path, &opts.output_path, opts.output_format) {
            Ok(_) => exitcode::OK,
            Err(errors) => {
                #[allow(clippy::print_stderr)]
                {
                    errors.iter().for_each(|e| eprintln!("{}", e.red()));
                }
                exitcode::SOFTWARE
            }
        }
    } else {
        match walk_dir_and_convert(&opts.input_path, &opts.output_path, opts.output_format) {
            Ok(()) => {
                #[allow(clippy::print_stdout)]
                {
                    println!(
                        "Finished conversion(s). Results are in {:?}",
                        opts.output_path
                    );
                }
                exitcode::OK
            }
            Err(errors) => {
                #[allow(clippy::print_stderr)]
                {
                    errors.iter().for_each(|e| eprintln!("{}", e.red()));
                }
                exitcode::SOFTWARE
            }
        }
    };
}

fn convert_config(
    input_path: &Path,
    output_path: &Path,
    output_format: Format,
) -> Result<(), Vec<String>> {
    if output_path.exists() {
        return Err(vec![format!("Output path {output_path:?} exists")]);
    }
    let input_format = match Format::from_str(
        input_path
            .extension()
            .unwrap_or_else(|| panic!("Failed to get extension for: {input_path:?}"))
            .to_str()
            .unwrap_or_else(|| panic!("Failed to convert OsStr to &str for: {input_path:?}")),
    ) {
        Ok(format) => format,
        Err(_) => return Ok(()), // skip irrelevant files
    };

    if input_format == output_format {
        return Ok(());
    }

    #[allow(clippy::print_stdout)]
    {
        println!("Converting {input_path:?} config to {output_format:?}.");
    }
    let file_contents = fs::read_to_string(input_path).map_err(|e| vec![e.to_string()])?;
    let builder: ConfigBuilder = format::deserialize(&file_contents, input_format)?;
    let config = builder.build()?;
    let output_string =
        format::serialize(&config, output_format).map_err(|e| vec![e.to_string()])?;
    fs::write(output_path, output_string).map_err(|e| vec![e.to_string()])?;

    #[allow(clippy::print_stdout)]
    {
        println!("Wrote result to {output_path:?}.");
    }
    Ok(())
}

fn walk_dir_and_convert(
    input_path: &Path,
    output_dir: &Path,
    output_format: Format,
) -> Result<(), Vec<String>> {
    let mut errors = Vec::new();

    if input_path.is_dir() {
        for entry in fs::read_dir(input_path)
            .unwrap_or_else(|_| panic!("Failed to read dir: {input_path:?}"))
        {
            let entry_path = entry
                .unwrap_or_else(|_| panic!("Failed to get entry for dir: {input_path:?}"))
                .path();
            let new_output_dir = if entry_path.is_dir() {
                let last_component = entry_path
                    .file_name()
                    .unwrap_or_else(|| panic!("Failed to get file_name for {entry_path:?}"));
                let new_dir = output_dir.join(last_component);

                if !new_dir.exists() {
                    fs::create_dir_all(&new_dir)
                        .unwrap_or_else(|_| panic!("Failed to create output dir: {new_dir:?}"));
                }
                new_dir
            } else {
                output_dir.to_path_buf()
            };

            if let Err(new_errors) = walk_dir_and_convert(
                &input_path.join(&entry_path),
                &new_output_dir,
                output_format,
            ) {
                errors.extend(new_errors);
            }
        }
    } else {
        let output_path = output_dir.join(
            input_path
                .with_extension(output_format.to_string().as_str())
                .file_name()
                .ok_or_else(|| {
                    vec![format!(
                        "Cannot create output path for input: {input_path:?}"
                    )]
                })?,
        );
        if let Err(new_errors) = convert_config(input_path, &output_path, output_format) {
            errors.extend(new_errors);
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

#[cfg(all(
    test,
    feature = "sources-demo_logs",
    feature = "transforms-remap",
    feature = "sinks-console"
))]
mod tests {
    use crate::config::{format, ConfigBuilder, Format};
    use crate::convert_config::{check_paths, walk_dir_and_convert, Opts};
    use std::path::{Path, PathBuf};
    use std::str::FromStr;
    use std::{env, fs};
    use tempfile::tempdir;

    fn test_data_dir() -> PathBuf {
        PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("tests/data/cmd/config")
    }

    // Read the contents of the specified `path` and deserialize them into a `ConfigBuilder`.
    // Finally serialize them a string again. Configs do not implement equality,
    // so for these tests we will rely on strings for comparisons.
    fn convert_file_to_config_string(path: &Path) -> String {
        let files_contents = fs::read_to_string(path).unwrap();
        let extension = path.extension().unwrap().to_str().unwrap();
        let file_format = Format::from_str(extension).unwrap();
        let builder: ConfigBuilder = format::deserialize(&files_contents, file_format).unwrap();
        let config = builder.build().unwrap();

        format::serialize(&config, file_format).unwrap()
    }

    #[test]
    fn invalid_path_opts() {
        let check_error = |opts, pattern| {
            let error = check_paths(&opts).unwrap_err();
            assert!(error.contains(pattern));
        };

        check_error(
            Opts {
                input_path: ["./"].iter().collect(),
                output_path: ["./"].iter().collect(),
                output_format: Format::Yaml,
            },
            "already exists",
        );

        check_error(
            Opts {
                input_path: ["./"].iter().collect(),
                output_path: ["./out.yaml"].iter().collect(),
                output_format: Format::Yaml,
            },
            "points to a file.",
        );

        check_error(
            Opts {
                input_path: [test_data_dir(), "config_2.toml".into()].iter().collect(),
                output_path: ["./another_dir"].iter().collect(),
                output_format: Format::Yaml,
            },
            "points to a directory.",
        );
    }

    #[test]
    fn convert_all_from_dir() {
        let input_path = test_data_dir();
        let output_dir = tempdir()
            .expect("Unable to create tempdir for config")
            .into_path();
        walk_dir_and_convert(&input_path, &output_dir, Format::Yaml).unwrap();

        let mut count: usize = 0;
        let original_config = convert_file_to_config_string(&test_data_dir().join("config_1.yaml"));
        for entry in fs::read_dir(&output_dir).unwrap() {
            let entry = entry.unwrap();
            let path = entry.path();
            if path.is_file() {
                let extension = path.extension().unwrap().to_str().unwrap();
                if extension == Format::Yaml.to_string() {
                    // Note that here we read the converted string directly.
                    let converted_config = fs::read_to_string(output_dir.join(&path)).unwrap();
                    assert_eq!(converted_config, original_config);
                    count += 1;
                }
            }
        }
        // There two non-yaml configs in the input directory.
        assert_eq!(count, 2);
    }
}