config-forge 0.6.0

A CLI tool for converting, inspecting, and validating configuration files.
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::path::PathBuf;

use anyhow::{Result, bail};
use clap::{Parser, Subcommand};
use config_forge::{
    Format, ValueFormat, check_convert_path, convert_path, delete_path_value, diff_paths,
    get_path_value, inspect_path, merge_paths, render_diff, render_query_value, set_path_value,
    validate_path,
};

#[derive(Debug, Parser)]
#[command(name = "config-forge")]
#[command(version, about = config_forge::describe())]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Convert between JSON, JSON5 input, TOML, YAML, env, INI, and properties.
    Convert {
        /// Input configuration file.
        input: PathBuf,

        /// Output file. If omitted, converted content is printed to stdout.
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,

        /// Output format. Defaults to detection from output extension.
        #[arg(long, value_parser = parse_format)]
        to: Option<Format>,

        /// Validate that conversion would succeed without writing output.
        #[arg(long)]
        check: bool,

        /// Replace the output file if it already exists.
        #[arg(long)]
        overwrite: bool,
    },

    /// Print basic information about a configuration file.
    Inspect {
        /// Input configuration file.
        input: PathBuf,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,
    },

    /// Validate that a configuration file can be parsed, optionally against JSON Schema.
    Validate {
        /// Input configuration file.
        input: PathBuf,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,

        /// JSON Schema file used to validate the parsed configuration value.
        #[arg(long)]
        schema: Option<PathBuf>,
    },

    /// Read a value by dot path.
    Get {
        /// Input configuration file.
        input: PathBuf,

        /// Dot path to read, such as server.port or items.0.name.
        path: String,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,

        /// Output format for the selected value.
        #[arg(long, value_parser = parse_format)]
        to: Option<Format>,
    },

    /// Update an existing value by dot path and write a new file.
    Set {
        /// Input configuration file.
        input: PathBuf,

        /// Dot path to update, such as server.port or items.0.name.
        path: String,

        /// Replacement value. Defaults to a string literal.
        value: String,

        /// Output file.
        #[arg(short, long)]
        output: PathBuf,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,

        /// Output format. Defaults to detection from output extension.
        #[arg(long, value_parser = parse_format)]
        to: Option<Format>,

        /// How to parse the replacement value.
        #[arg(long, default_value = "string", value_parser = parse_value_format)]
        value_format: ValueFormat,

        /// Replace the output file if it already exists.
        #[arg(long)]
        overwrite: bool,
    },

    /// Delete an existing value by dot path and write a new file.
    Delete {
        /// Input configuration file.
        input: PathBuf,

        /// Dot path to delete, such as server.debug or items.0.
        path: String,

        /// Output file.
        #[arg(short, long)]
        output: PathBuf,

        /// Input format. Defaults to detection from input extension.
        #[arg(long, value_parser = parse_format)]
        from: Option<Format>,

        /// Output format. Defaults to detection from output extension.
        #[arg(long, value_parser = parse_format)]
        to: Option<Format>,

        /// Replace the output file if it already exists.
        #[arg(long)]
        overwrite: bool,
    },

    /// Recursively merge two configuration files and write a new file.
    Merge {
        /// Base configuration file.
        base: PathBuf,

        /// Override configuration file.
        override_file: PathBuf,

        /// Output file.
        #[arg(short, long)]
        output: PathBuf,

        /// Base input format. Defaults to detection from base extension.
        #[arg(long, value_parser = parse_format)]
        base_format: Option<Format>,

        /// Override input format. Defaults to detection from override extension.
        #[arg(long, value_parser = parse_format)]
        override_format: Option<Format>,

        /// Output format. Defaults to detection from output extension.
        #[arg(long, value_parser = parse_format)]
        to: Option<Format>,

        /// Replace the output file if it already exists.
        #[arg(long)]
        overwrite: bool,
    },

    /// Print a path-oriented diff between two configuration files.
    Diff {
        /// Old configuration file.
        old: PathBuf,

        /// New configuration file.
        new: PathBuf,

        /// Old input format. Defaults to detection from old extension.
        #[arg(long, value_parser = parse_format)]
        old_format: Option<Format>,

        /// New input format. Defaults to detection from new extension.
        #[arg(long, value_parser = parse_format)]
        new_format: Option<Format>,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Some(Command::Convert {
            input,
            output,
            from,
            to,
            check,
            overwrite,
        }) => convert(input, output, from, to, check, overwrite),
        Some(Command::Inspect { input, from }) => inspect(input, from),
        Some(Command::Validate {
            input,
            from,
            schema,
        }) => validate(input, from, schema),
        Some(Command::Get {
            input,
            path,
            from,
            to,
        }) => get(input, &path, from, to),
        Some(Command::Set {
            input,
            path,
            value,
            output,
            from,
            to,
            value_format,
            overwrite,
        }) => set(
            input,
            &path,
            &value,
            output,
            from,
            to,
            value_format,
            overwrite,
        ),
        Some(Command::Delete {
            input,
            path,
            output,
            from,
            to,
            overwrite,
        }) => delete(input, &path, output, from, to, overwrite),
        Some(Command::Merge {
            base,
            override_file,
            output,
            base_format,
            override_format,
            to,
            overwrite,
        }) => merge(
            base,
            override_file,
            output,
            base_format,
            override_format,
            to,
            overwrite,
        ),
        Some(Command::Diff {
            old,
            new,
            old_format,
            new_format,
        }) => diff(old, new, old_format, new_format),
        None => {
            println!("{} {}", config_forge::NAME, config_forge::VERSION);
            println!("{}", config_forge::describe());
            Ok(())
        }
    }
}

fn convert(
    input: PathBuf,
    output: Option<PathBuf>,
    from: Option<Format>,
    to: Option<Format>,
    check: bool,
    overwrite: bool,
) -> Result<()> {
    if check {
        let output_format = check_convert_path(&input, from, to)?;
        println!("ok: conversion to {} is valid", output_format.name());
        return Ok(());
    }

    if output.is_none() && to.is_none() {
        bail!("--to is required when --output is not provided");
    }

    let rendered = convert_path(&input, output.as_ref(), from, to, overwrite)?;

    if output.is_none() {
        print!("{rendered}");
    }

    Ok(())
}

fn inspect(input: PathBuf, from: Option<Format>) -> Result<()> {
    let info = inspect_path(&input, from)?;

    println!("path: {}", input.display());
    println!("format: {}", info.format.name());
    println!("root: {}", info.root_kind);
    println!("size: {} bytes", info.size_bytes);

    Ok(())
}

fn validate(input: PathBuf, from: Option<Format>, schema: Option<PathBuf>) -> Result<()> {
    let format = validate_path(&input, from, schema.as_ref())?;
    let file_name = input
        .file_name()
        .and_then(|value| value.to_str())
        .unwrap_or_else(|| input.to_str().unwrap_or("<input>"));

    if schema.is_some() {
        println!(
            "ok: {file_name} is valid {} and matches schema",
            format.name()
        );
    } else {
        println!("ok: {file_name} is valid {}", format.name());
    }
    Ok(())
}

fn get(input: PathBuf, path: &str, from: Option<Format>, to: Option<Format>) -> Result<()> {
    let value = get_path_value(&input, path, from)?;
    let rendered = render_query_value(&value, to)?;
    print!("{rendered}");
    Ok(())
}

fn set(
    input: PathBuf,
    path: &str,
    value: &str,
    output: PathBuf,
    from: Option<Format>,
    to: Option<Format>,
    value_format: ValueFormat,
    overwrite: bool,
) -> Result<()> {
    set_path_value(
        &input,
        &output,
        path,
        value,
        value_format,
        from,
        to,
        overwrite,
    )?;
    Ok(())
}

fn delete(
    input: PathBuf,
    path: &str,
    output: PathBuf,
    from: Option<Format>,
    to: Option<Format>,
    overwrite: bool,
) -> Result<()> {
    delete_path_value(&input, &output, path, from, to, overwrite)?;
    Ok(())
}

fn merge(
    base: PathBuf,
    override_file: PathBuf,
    output: PathBuf,
    base_format: Option<Format>,
    override_format: Option<Format>,
    to: Option<Format>,
    overwrite: bool,
) -> Result<()> {
    merge_paths(
        &base,
        &override_file,
        &output,
        base_format,
        override_format,
        to,
        overwrite,
    )?;
    Ok(())
}

fn diff(
    old: PathBuf,
    new: PathBuf,
    old_format: Option<Format>,
    new_format: Option<Format>,
) -> Result<()> {
    let entries = diff_paths(&old, &new, old_format, new_format)?;
    print!("{}", render_diff(&entries));
    Ok(())
}

fn parse_format(value: &str) -> Result<Format> {
    value.parse()
}

fn parse_value_format(value: &str) -> Result<ValueFormat> {
    value.parse()
}