Skip to content

Commit d764c4e

Browse files
committed
Move TemplateCommand struct into template mod
1 parent 6718c4f commit d764c4e

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

src/cli_args.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::path::PathBuf;
2-
31
use clap::{Parser, Subcommand};
42

5-
use config_utils::file_types::FileType;
3+
use config_utils::template::cli_args::TemplateCommand;
64

75
/// Utility that helps you handling config files.
86
#[derive(Debug, Parser)]
@@ -14,20 +12,5 @@ pub struct Args {
1412

1513
#[derive(Debug, Subcommand)]
1614
pub enum Command {
17-
/// Fill out variables in config files from either env variables or files directly.
18-
Template {
19-
/// The path to the file that should be templated
20-
file: PathBuf,
21-
22-
/// The optional file type of the file to be templated. If this is not specified this utility will try to infer
23-
/// the type based on the file name.
24-
#[arg(value_enum)]
25-
file_type: Option<FileType>,
26-
27-
/// By default inserted values are automatically escaped according to the deteced file format. You can disable
28-
/// this, e.g. when you need to insert XML tags (as they otherwise would be escaped).
29-
/// NOTE: Please make sure to correctly escape the inserted text on your own!
30-
#[clap(long)]
31-
dont_escape: bool,
32-
},
15+
Template(TemplateCommand),
3316
}

src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use config_utils::template::{self, template};
2+
use config_utils::template::{self, cli_args::TemplateCommand, template};
33
use snafu::{ResultExt, Snafu};
44

55
use cli_args::{Args, Command};
@@ -19,11 +19,11 @@ fn main() -> Result<()> {
1919
let args = Args::parse();
2020

2121
match args.command {
22-
Command::Template {
22+
Command::Template(TemplateCommand {
2323
file,
2424
file_type,
2525
dont_escape,
26-
} => {
26+
}) => {
2727
template(&file, file_type.as_ref(), !dont_escape).context(TemplateFileSnafu)?;
2828
}
2929
}

src/template/cli_args.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Parser;
4+
5+
use crate::file_types::FileType;
6+
7+
/// Fill out variables in config files from either env variables or files directly.
8+
#[derive(Debug, Parser)]
9+
pub struct TemplateCommand {
10+
/// The path to the file that should be templated
11+
pub file: PathBuf,
12+
13+
/// The optional file type of the file to be templated. If this is not specified this utility will try to infer
14+
/// the type based on the file name.
15+
#[arg(value_enum)]
16+
pub file_type: Option<FileType>,
17+
18+
/// By default inserted values are automatically escaped according to the deteced file format. You can disable
19+
/// this, e.g. when you need to insert XML tags (as they otherwise would be escaped).
20+
/// NOTE: Please make sure to correctly escape the inserted text on your own!
21+
#[clap(long)]
22+
pub dont_escape: bool,
23+
}

src/template/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
ENV_VAR_PATTERN_END, ENV_VAR_PATTERN_START, FILE_PATTERN_END, FILE_PATTERN_START,
1313
};
1414

15+
pub mod cli_args;
16+
1517
#[derive(Debug, Snafu)]
1618
pub enum Error {
1719
#[snafu(display("Could not read file {file_name:?}"))]
@@ -125,7 +127,7 @@ pub fn template(file_name: &PathBuf, file_type: Option<&FileType>, escape: bool)
125127
})?;
126128
}
127129

128-
fs::rename(&tmp_file_name, &file_name).context(RenameTemporaryFileSnafu {
130+
fs::rename(&tmp_file_name, file_name).context(RenameTemporaryFileSnafu {
129131
tmp_file_name,
130132
destination_file_name: file_name,
131133
})?;

tests/templating.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
env,
33
fs::{self, File},
44
io::Write,
5-
path::PathBuf,
5+
path::{Path, PathBuf},
66
};
77

88
use rstest::rstest;
@@ -21,13 +21,13 @@ fn test_file_templating(#[files("tests/resources/**/*.in")] test_file_in: PathBu
2121
fs::copy(&test_file_in, &test_file).unwrap();
2222
template(&test_file, None, true).unwrap();
2323

24-
let actual = fs::read_to_string(&test_file).unwrap();
25-
let expected = fs::read_to_string(&test_file_expected).unwrap();
24+
let actual = fs::read_to_string(test_file).unwrap();
25+
let expected = fs::read_to_string(test_file_expected).unwrap();
2626

2727
similar_asserts::assert_eq!(actual, expected);
2828
}
2929

30-
fn set_example_envs(example_dir: &PathBuf) {
30+
fn set_example_envs(example_dir: &Path) {
3131
// SAFETY: We only use a single thread to set this env vars
3232
env::set_var("ENV_TEST", "foo");
3333
env::set_var("ENV_TEST_USERNAME", "example user");

0 commit comments

Comments
 (0)