chore: Switch to using typed builders

This commit is contained in:
Gerald Pinder 2023-12-18 23:36:53 -05:00
parent e9cfc8ae06
commit aa86f48a5d
6 changed files with 39 additions and 19 deletions

21
Cargo.lock generated
View file

@ -1028,6 +1028,26 @@ dependencies = [
"serde",
]
[[package]]
name = "typed-builder"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e47c0496149861b7c95198088cbf36645016b1a0734cf350c50e2a38e070f38a"
dependencies = [
"typed-builder-macro",
]
[[package]]
name = "typed-builder-macro"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "982ee4197351b5c9782847ef5ec1fdcaf50503fb19d68f9771adae314e72b492"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]]
name = "typenum"
version = "1.17.0"
@ -1051,6 +1071,7 @@ dependencies = [
"serde_json",
"serde_yaml",
"tera",
"typed-builder",
]
[[package]]

View file

@ -23,6 +23,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
serde_yaml = "0.9.25"
tera = "1.19.1"
typed-builder = "0.18.0"
[features]
default = ["build"]

View file

@ -7,12 +7,12 @@ use std::{
use anyhow::{anyhow, bail, Result};
use chrono::Local;
use clap::Args;
use derive_builder::Builder;
use log::{debug, error, info, trace, warn};
use typed_builder::TypedBuilder;
use crate::{module_recipe::Recipe, ops, template::TemplateCommand};
#[derive(Debug, Clone, Args, Builder)]
#[derive(Debug, Clone, Args, TypedBuilder)]
pub struct BuildCommand {
/// The recipe file to build an image
#[arg()]
@ -63,8 +63,8 @@ impl BuildCommand {
if let Err(e) = TemplateCommand::builder()
.recipe(self.recipe.clone())
.containerfile(self.containerfile.clone())
.output(Some(PathBuf::from("Containerfile")))
.build()?
.output(PathBuf::from("Containerfile"))
.build()
.run()
{
error!("Failed to template file: {e}");
@ -105,10 +105,6 @@ impl BuildCommand {
Ok(())
}
pub fn builder() -> BuildCommandBuilder {
BuildCommandBuilder::default()
}
fn generate_tags(&self, recipe: &Recipe) -> Vec<String> {
debug!("Generating image tags for {}", &recipe.name);
trace!("BuildCommand::generate_tags({recipe:#?})");

View file

@ -2,13 +2,13 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use clap::Args;
use derive_builder::Builder;
use typed_builder::TypedBuilder;
const GITLAB_CI_FILE: &'static str = include_str!("../templates/init/gitlab-ci.yml.tera");
const RECIPE_FILE: &'static str = include_str!("../templates/init/recipe.yml.tera");
const LICENSE_FILE: &'static str = include_str!("../LICENSE");
#[derive(Debug, Clone, Args, Builder)]
#[derive(Debug, Clone, Args, TypedBuilder)]
pub struct InitCommand {
/// The directory to extract the files into. Defaults to the current directory
#[arg()]

View file

@ -7,15 +7,15 @@ use std::{
use anyhow::Result;
use clap::Args;
use derive_builder::Builder;
use log::{debug, error, info, trace};
use tera::{Context, Tera};
use typed_builder::TypedBuilder;
use crate::module_recipe::Recipe;
pub const DEFAULT_CONTAINERFILE: &str = include_str!("../templates/Containerfile.tera");
#[derive(Debug, Clone, Args, Builder)]
#[derive(Debug, Clone, Args, TypedBuilder)]
pub struct TemplateCommand {
/// The recipe file to create a template from
#[arg()]
@ -23,12 +23,12 @@ pub struct TemplateCommand {
/// Optional Containerfile to use as a template
#[arg(short, long)]
#[builder(default)]
#[builder(default, setter(into))]
containerfile: Option<PathBuf>,
/// File to output to instead of STDOUT
#[arg(short, long)]
#[builder(default)]
#[builder(default, setter(into))]
output: Option<PathBuf>,
}
@ -72,10 +72,6 @@ impl TemplateCommand {
Ok(())
}
pub fn builder() -> TemplateCommandBuilder {
TemplateCommandBuilder::default()
}
fn setup_tera(&self) -> Result<(Tera, Context)> {
trace!("TemplateCommand::setup_tera()");

View file

@ -1,5 +1,12 @@
workflow:
rules:
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
when: never
- if: "$CI_COMMIT_TAG"
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
when: never
- if: "$CI_COMMIT_BRANCH"
stages:
- build
@ -19,7 +26,6 @@ build-image:
- if: $ACTION == "build-image"
parallel:
matrix:
# As you create more recipes to build, add them below
- RECIPE:
- recipe.yml
id_tokens: