use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BuildArg {
pub name: String,
pub default: Option<String>,
}
#[cfg(test)]
impl BuildArg {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
default: None,
}
}
pub fn with_default(mut self, default: &str) -> Self {
self.default = Some(default.to_string());
self
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ExposePort {
pub port: u16,
#[serde(default = "default_protocol")]
pub protocol: String,
}
fn default_protocol() -> String {
"tcp".to_string()
}
impl ExposePort {
pub fn parse(s: &str) -> Option<Self> {
let parts: Vec<&str> = s.split('/').collect();
let port = parts.first()?.parse().ok()?;
let protocol = parts
.get(1)
.map_or_else(default_protocol, std::string::ToString::to_string);
if protocol != "tcp" && protocol != "udp" && protocol != "sctp" {
return None;
}
Some(Self { port, protocol })
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CopySpec {
pub src: String,
pub dest: String,
pub mode: Option<u32>,
pub owner: Option<String>,
}
impl CopySpec {
pub fn new(src: &str, dest: &str) -> Self {
Self {
src: src.to_string(),
dest: dest.to_string(),
mode: None,
owner: None,
}
}
}
#[derive(Debug, Clone, Serialize)]
pub enum Instruction {
From(String),
Arg(BuildArg),
Env(String, String),
Run(String),
Copy(CopySpec),
Workdir(String),
Expose(ExposePort),
Cmd(String),
Entrypoint(String),
Stop(String),
User(String),
Label(String, String),
Volume(String),
Comment(String),
}
impl Instruction {
pub const fn name(&self) -> &'static str {
match self {
Self::From(_) => "FROM",
Self::Arg(_) => "ARG",
Self::Env(_, _) => "ENV",
Self::Run(_) => "RUN",
Self::Copy(_) => "COPY",
Self::Workdir(_) => "WORKDIR",
Self::Expose(_) => "EXPOSE",
Self::Cmd(_) => "CMD",
Self::Entrypoint(_) => "ENTRYPOINT",
Self::Stop(_) => "STOP",
Self::User(_) => "USER",
Self::Label(_, _) => "LABEL",
Self::Volume(_) => "VOLUME",
Self::Comment(_) => "#",
}
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct JailfileMetadata {
pub name: Option<String>,
pub version: Option<String>,
pub description: Option<String>,
pub author: Option<String>,
#[serde(default)]
pub labels: HashMap<String, String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Jailfile {
pub metadata: JailfileMetadata,
pub from: Option<String>,
pub args: Vec<BuildArg>,
pub instructions: Vec<Instruction>,
pub cmd: Option<String>,
pub entrypoint: Option<String>,
pub stop: Option<String>,
pub workdir: Option<String>,
pub user: Option<String>,
pub expose: Vec<ExposePort>,
pub volumes: Vec<String>,
pub env: HashMap<String, String>,
}
impl Default for Jailfile {
fn default() -> Self {
Self::new()
}
}
impl Jailfile {
pub fn new() -> Self {
Self {
metadata: JailfileMetadata::default(),
from: None,
args: Vec::new(),
instructions: Vec::new(),
cmd: None,
entrypoint: None,
stop: None,
workdir: None,
user: None,
expose: Vec::new(),
volumes: Vec::new(),
env: HashMap::new(),
}
}
}
#[cfg(test)]
impl Jailfile {
pub fn from_release(release: &str) -> Self {
let mut jf = Self::new();
jf.from = Some(release.to_string());
jf.instructions.push(Instruction::From(release.to_string()));
jf
}
pub fn arg(mut self, name: &str, default: Option<&str>) -> Self {
let arg = BuildArg {
name: name.to_string(),
default: default.map(String::from),
};
self.args.push(arg.clone());
self.instructions.push(Instruction::Arg(arg));
self
}
pub fn env(mut self, name: &str, value: &str) -> Self {
self.env.insert(name.to_string(), value.to_string());
self.instructions
.push(Instruction::Env(name.to_string(), value.to_string()));
self
}
pub fn run(mut self, command: &str) -> Self {
self.instructions
.push(Instruction::Run(command.to_string()));
self
}
pub fn copy(mut self, src: &str, dest: &str) -> Self {
let spec = CopySpec::new(src, dest);
self.instructions.push(Instruction::Copy(spec));
self
}
pub fn workdir(mut self, path: &str) -> Self {
self.workdir = Some(path.to_string());
self.instructions
.push(Instruction::Workdir(path.to_string()));
self
}
pub fn expose(mut self, port: u16, protocol: &str) -> Self {
let exp = ExposePort {
port,
protocol: protocol.to_string(),
};
self.expose.push(exp.clone());
self.instructions.push(Instruction::Expose(exp));
self
}
pub fn cmd(mut self, command: &str) -> Self {
self.cmd = Some(command.to_string());
self.instructions
.push(Instruction::Cmd(command.to_string()));
self
}
pub fn run_commands(&self) -> Vec<&str> {
self.instructions
.iter()
.filter_map(|i| match i {
Instruction::Run(cmd) => Some(cmd.as_str()),
_ => None,
})
.collect()
}
pub fn copy_specs(&self) -> Vec<&CopySpec> {
self.instructions
.iter()
.filter_map(|i| match i {
Instruction::Copy(spec) => Some(spec),
_ => None,
})
.collect()
}
}
#[cfg(test)]
mod tests;