Clean up the code a bit
This commit is contained in:
parent
2cd8878490
commit
bd04489dc3
2 changed files with 36 additions and 28 deletions
|
|
@ -8,11 +8,16 @@ fn main() -> Result<()> {
|
|||
match args.command {
|
||||
CommandArgs::Template {
|
||||
recipe,
|
||||
containerfile: _,
|
||||
containerfile,
|
||||
output,
|
||||
} => {
|
||||
let (tera, context) = setup_tera(recipe)?;
|
||||
let output = tera.render("Containerfile", &context)?;
|
||||
println!("{output}");
|
||||
let (tera, context) = setup_tera(recipe, containerfile)?;
|
||||
let output_str = tera.render("Containerfile", &context)?;
|
||||
if let Some(output) = output {
|
||||
std::fs::write(output, output_str)?;
|
||||
} else {
|
||||
println!("{output_str}");
|
||||
}
|
||||
}
|
||||
CommandArgs::Build { containerfile: _ } => {
|
||||
println!("Not yet implemented!");
|
||||
|
|
|
|||
51
src/lib.rs
51
src/lib.rs
|
|
@ -7,7 +7,7 @@ use std::{
|
|||
use anyhow::Result;
|
||||
use clap::{Parser, Subcommand};
|
||||
use recipe::Recipe;
|
||||
use tera::{from_value, Context, Function, Tera};
|
||||
use tera::{from_value, Context, Tera};
|
||||
|
||||
pub const DEFAULT_CONTAINERFILE: &'static str =
|
||||
include_str!("../templates/starting_point.template");
|
||||
|
|
@ -31,7 +31,11 @@ pub enum CommandArgs {
|
|||
|
||||
/// Optional Containerfile to use as a template
|
||||
#[arg(short, long)]
|
||||
containerfile: Option<String>,
|
||||
containerfile: Option<PathBuf>,
|
||||
|
||||
/// File to output to instead of STDOUT
|
||||
#[arg(short, long)]
|
||||
output: Option<PathBuf>,
|
||||
},
|
||||
|
||||
/// Build an image from a Containerfile
|
||||
|
|
@ -41,8 +45,23 @@ pub enum CommandArgs {
|
|||
},
|
||||
}
|
||||
|
||||
fn print_containerfile() -> impl Function {
|
||||
Box::new(
|
||||
pub fn setup_tera(recipe: String, containerfile: Option<PathBuf>) -> Result<(Tera, Context)> {
|
||||
let recipe_de =
|
||||
serde_yaml::from_str::<Recipe>(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?
|
||||
.process_repos();
|
||||
|
||||
let mut context = Context::from_serialize(recipe_de)?;
|
||||
context.insert("recipe", &recipe);
|
||||
|
||||
let mut tera = Tera::default();
|
||||
match containerfile {
|
||||
Some(containerfile) => {
|
||||
tera.add_raw_template("Containerfile", &read_to_string(containerfile)?)?
|
||||
}
|
||||
None => tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?,
|
||||
}
|
||||
tera.register_function(
|
||||
"print_containerfile",
|
||||
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
|
||||
match args.get("containerfile") {
|
||||
Some(v) => match from_value::<String>(v.clone()) {
|
||||
|
|
@ -54,11 +73,9 @@ fn print_containerfile() -> impl Function {
|
|||
None => Err("Needs the argument 'containerfile'".into()),
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn print_autorun_scripts() -> impl Function {
|
||||
Box::new(
|
||||
);
|
||||
tera.register_function(
|
||||
"print_autorun_scripts",
|
||||
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
|
||||
match args.get("mode") {
|
||||
Some(v) => match from_value::<String>(v.clone()) {
|
||||
|
|
@ -86,21 +103,7 @@ fn print_autorun_scripts() -> impl Function {
|
|||
None => Err("Need arg 'mode' set with 'pre' or 'post'".into()),
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn setup_tera(recipe: String) -> Result<(Tera, Context)> {
|
||||
let recipe_de =
|
||||
serde_yaml::from_str::<Recipe>(fs::read_to_string(PathBuf::from(&recipe))?.as_str())?
|
||||
.process_repos();
|
||||
|
||||
let mut context = Context::from_serialize(recipe_de)?;
|
||||
context.insert("recipe", &recipe);
|
||||
|
||||
let mut tera = Tera::default();
|
||||
tera.add_raw_template("Containerfile", DEFAULT_CONTAINERFILE)?;
|
||||
tera.register_function("print_containerfile", print_containerfile());
|
||||
tera.register_function("print_autorun_scripts", print_autorun_scripts());
|
||||
);
|
||||
|
||||
Ok((tera, context))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue