dofigen_lib/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
//! # dofigen_lib
//!
//! `dofigen_lib` help creating Dockerfile with a simplified structure and made to cache the build with Buildkit.
//! You also can parse the structure from YAML or JSON.
//!
//! ```
//! use dofigen_lib::*;
//! use pretty_assertions_sorted::assert_eq_sorted;
//!
//! let mut context = DofigenContext::new();
//!
//! let dofigen = context.parse_from_string(r#"
//! fromImage:
//! path: ubuntu
//! "#).unwrap();
//!
//! let dockerfile = generate_dockerfile(&dofigen).unwrap();
//! ```
mod context;
mod deserialize;
mod dockerfile_struct;
mod dofigen_struct;
mod errors;
mod extend;
#[cfg(feature = "permissive")]
mod from_str;
mod generator;
#[cfg(feature = "json_schema")]
mod json_schema;
pub mod lock;
use dockerfile_struct::{DockerfileContent, DockerfileLine};
use generator::{DockerfileGenerator, GenerationContext};
#[cfg(feature = "json_schema")]
use schemars::gen::*;
pub use {context::*, deserialize::*, dofigen_struct::*, errors::*, extend::*};
#[cfg(all(feature = "strict", feature = "permissive"))]
compile_error!("You can't enable both 'strict' and 'permissive' features at the same time.");
pub(crate) const DOCKERFILE_VERSION: &str = "1.7";
const FILE_HEADER_COMMENTS: [&str; 2] = [
concat!(
"This file is generated by Dofigen v",
env!("CARGO_PKG_VERSION")
),
concat!("See ", env!("CARGO_PKG_REPOSITORY")),
];
/// Generates the Dockerfile content from a Dofigen struct.
///
/// # Examples
///
/// ```
/// use dofigen_lib::*;
/// use pretty_assertions_sorted::assert_eq_sorted;
///
/// let dofigen = Dofigen {
/// stage: Stage {
/// from: ImageName {
/// path: String::from("ubuntu"),
/// ..Default::default()
/// }.into(),
/// ..Default::default()
/// },
/// ..Default::default()
/// };
/// let dockerfile: String = generate_dockerfile(&dofigen).unwrap();
/// assert_eq_sorted!(
/// dockerfile,
/// "# syntax=docker/dockerfile:1.7\n# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\n# runtime\nFROM ubuntu AS runtime\nUSER 1000:1000\n"
/// );
/// ```
pub fn generate_dockerfile(dofigen: &Dofigen) -> Result<String> {
let mut lines = dofigen.generate_dockerfile_lines(&GenerationContext::default())?;
let mut line_number = 1;
for line in FILE_HEADER_COMMENTS {
lines.insert(line_number, DockerfileLine::Comment(line.to_string()));
line_number += 1;
}
Ok(format!(
"{}\n",
lines
.iter()
.map(DockerfileLine::generate_content)
.collect::<Vec<String>>()
.join("\n")
))
}
/// Generates the .dockerignore file content from an Dofigen struct.
///
/// # Examples
///
/// ## Define the build context
///
/// ```
/// use dofigen_lib::*;
/// use pretty_assertions_sorted::assert_eq_sorted;
///
/// let dofigen = Dofigen {
/// context: vec![String::from("/src")].into(),
/// ..Default::default()
/// };
/// let dockerfile: String = generate_dockerignore(&dofigen);
/// assert_eq_sorted!(
/// dockerfile,
/// "# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\n**\n!/src\n"
/// );
/// ```
///
/// ## Ignore a path
///
/// ```
/// use dofigen_lib::*;
/// use pretty_assertions_sorted::assert_eq_sorted;
///
/// let dofigen = Dofigen {
/// ignore: vec![String::from("target")].into(),
/// ..Default::default()
/// };
/// let dockerfile: String = generate_dockerignore(&dofigen);
/// assert_eq_sorted!(
/// dockerfile,
/// "# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\ntarget\n"
/// );
/// ```
///
/// ## Define context ignoring a specific files
///
/// ```
/// use dofigen_lib::*;
/// use pretty_assertions_sorted::assert_eq_sorted;
///
/// let dofigen = Dofigen {
/// context: vec![String::from("/src")].into(),
/// ignore: vec![String::from("/src/*.test.rs")].into(),
/// ..Default::default()
/// };
/// let dockerfile: String = generate_dockerignore(&dofigen);
/// assert_eq_sorted!(
/// dockerfile,
/// "# This file is generated by Dofigen v0.0.0\n# See https://github.com/lenra-io/dofigen\n\n**\n!/src\n/src/*.test.rs\n"
/// );
/// ```
pub fn generate_dockerignore(dofigen: &Dofigen) -> String {
let mut content = String::new();
for line in FILE_HEADER_COMMENTS {
content.push_str("# ");
content.push_str(line);
content.push_str("\n");
}
content.push_str("\n");
if !dofigen.context.is_empty() {
content.push_str("**\n");
dofigen.context.iter().for_each(|path| {
content.push_str("!");
content.push_str(path);
content.push_str("\n");
});
}
if !dofigen.ignore.is_empty() {
dofigen.ignore.iter().for_each(|path| {
content.push_str(path);
content.push_str("\n");
});
}
content
}
/// Generates the effective Dofigen content from a Dofigen struct.
///
/// # Examples
///
/// ```
/// use dofigen_lib::*;
/// use pretty_assertions_sorted::assert_eq_sorted;
///
/// let dofigen = Dofigen {
/// stage: Stage {
/// from: ImageName {
/// path: String::from("ubuntu"),
/// ..Default::default()
/// }.into(),
/// ..Default::default()
/// },
/// ..Default::default()
/// };
/// let dofigen: String = generate_effective_content(&dofigen).unwrap();
/// assert_eq_sorted!(
/// dofigen,
/// "fromImage:\n path: ubuntu\n"
/// );
/// ```
pub fn generate_effective_content(dofigen: &Dofigen) -> Result<String> {
Ok(serde_yaml::to_string(&dofigen)?)
}
/// Generates the JSON schema for the Dofigen struct.
/// This is useful to validate the structure and IDE autocompletion.
#[cfg(feature = "json_schema")]
pub fn generate_json_schema() -> String {
let settings = SchemaSettings::default().with(|s| {
s.option_nullable = true;
s.option_add_null_type = true;
});
let gen = settings.into_generator();
let schema = gen.into_root_schema_for::<Extend<DofigenPatch>>();
serde_json::to_string_pretty(&schema).unwrap()
}