Skip to content

Commit fd1fb83

Browse files
committed
Add default_glob option to config file.
1 parent 0227286 commit fd1fb83

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

src/main.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const CONFIG_FILE: &str = "codegen.json";
1414
struct Config {
1515
version: u32,
1616
template_root: String,
17+
#[serde(default)]
18+
default_glob: Option<String>,
1719
templates: BTreeMap<String, Template>,
1820
}
1921

@@ -170,8 +172,8 @@ fn find_config_file() -> anyhow::Result<PathBuf> {
170172
#[derive(FromArgs, PartialEq, Debug)]
171173
/// Codegen - Generate Rust code from templates
172174
struct CliArgs {
173-
/// output file paths (glob pattern)
174-
#[argh(positional, hidden_help)]
175+
/// filter output paths by glob pattern (defaults to codegen.json's "default_glob" when set)
176+
#[argh(positional)]
175177
glob: Option<String>,
176178

177179
/// force overwrite existing files
@@ -203,14 +205,24 @@ fn main() -> anyhow::Result<()> {
203205
let args: CliArgs = argh::from_env();
204206

205207
let fmt_output = !args.nofmt;
208+
209+
let config_file = if let Some(ref config) = args.config {
210+
config.clone()
211+
} else {
212+
find_config_file()?
213+
};
214+
let config_root = config_file.parent().unwrap_or(Path::new("."));
215+
let config = Config::from_file(config_file.as_path())?;
216+
206217
let output_path_glob = args.glob;
207218

208219
if args.stdout && output_path_glob.is_none() {
209220
// TODO: What if the glob matches multiple files?
210221
bail!("specify a single file to output to stdout.");
211222
}
212223

213-
let glob = if let Some(ref output_path_glob) = output_path_glob {
224+
let effective_glob = output_path_glob.as_ref().or(config.default_glob.as_ref());
225+
let glob = if let Some(output_path_glob) = effective_glob {
214226
Some(
215227
globset::Glob::new(output_path_glob.as_str())
216228
.context("failed to compile glob")?
@@ -220,14 +232,6 @@ fn main() -> anyhow::Result<()> {
220232
None
221233
};
222234

223-
let config_file = if let Some(ref config) = args.config {
224-
config.clone()
225-
} else {
226-
find_config_file()?
227-
};
228-
let config_root = config_file.parent().unwrap_or(Path::new("."));
229-
let config = Config::from_file(config_file.as_path())?;
230-
231235
let template_path = Path::new(config_root)
232236
.join(&config.template_root)
233237
.join("**/*.rs.tera");

0 commit comments

Comments
 (0)