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 {
|
match args.command {
|
||||||
CommandArgs::Template {
|
CommandArgs::Template {
|
||||||
recipe,
|
recipe,
|
||||||
containerfile: _,
|
containerfile,
|
||||||
|
output,
|
||||||
} => {
|
} => {
|
||||||
let (tera, context) = setup_tera(recipe)?;
|
let (tera, context) = setup_tera(recipe, containerfile)?;
|
||||||
let output = tera.render("Containerfile", &context)?;
|
let output_str = tera.render("Containerfile", &context)?;
|
||||||
println!("{output}");
|
if let Some(output) = output {
|
||||||
|
std::fs::write(output, output_str)?;
|
||||||
|
} else {
|
||||||
|
println!("{output_str}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
CommandArgs::Build { containerfile: _ } => {
|
CommandArgs::Build { containerfile: _ } => {
|
||||||
println!("Not yet implemented!");
|
println!("Not yet implemented!");
|
||||||
|
|
|
||||||
51
src/lib.rs
51
src/lib.rs
|
|
@ -7,7 +7,7 @@ use std::{
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use recipe::Recipe;
|
use recipe::Recipe;
|
||||||
use tera::{from_value, Context, Function, Tera};
|
use tera::{from_value, Context, Tera};
|
||||||
|
|
||||||
pub const DEFAULT_CONTAINERFILE: &'static str =
|
pub const DEFAULT_CONTAINERFILE: &'static str =
|
||||||
include_str!("../templates/starting_point.template");
|
include_str!("../templates/starting_point.template");
|
||||||
|
|
@ -31,7 +31,11 @@ pub enum CommandArgs {
|
||||||
|
|
||||||
/// Optional Containerfile to use as a template
|
/// Optional Containerfile to use as a template
|
||||||
#[arg(short, long)]
|
#[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
|
/// Build an image from a Containerfile
|
||||||
|
|
@ -41,8 +45,23 @@ pub enum CommandArgs {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_containerfile() -> impl Function {
|
pub fn setup_tera(recipe: String, containerfile: Option<PathBuf>) -> Result<(Tera, Context)> {
|
||||||
Box::new(
|
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> {
|
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
|
||||||
match args.get("containerfile") {
|
match args.get("containerfile") {
|
||||||
Some(v) => match from_value::<String>(v.clone()) {
|
Some(v) => match from_value::<String>(v.clone()) {
|
||||||
|
|
@ -54,11 +73,9 @@ fn print_containerfile() -> impl Function {
|
||||||
None => Err("Needs the argument 'containerfile'".into()),
|
None => Err("Needs the argument 'containerfile'".into()),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
);
|
||||||
}
|
tera.register_function(
|
||||||
|
"print_autorun_scripts",
|
||||||
fn print_autorun_scripts() -> impl Function {
|
|
||||||
Box::new(
|
|
||||||
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
|
|args: &HashMap<String, tera::Value>| -> tera::Result<tera::Value> {
|
||||||
match args.get("mode") {
|
match args.get("mode") {
|
||||||
Some(v) => match from_value::<String>(v.clone()) {
|
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()),
|
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))
|
Ok((tera, context))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue