dockerfile_parser_rs/
file.rs1use std::fs::File;
2use std::io::Write;
3use std::path::PathBuf;
4
5use crate::ParseResult;
6use crate::ast::Instruction;
7use crate::error::ParseError;
8use crate::parser::instructions::add;
9use crate::parser::instructions::arg;
10use crate::parser::instructions::cmd;
11use crate::parser::instructions::copy;
12use crate::parser::instructions::entrypoint;
13use crate::parser::instructions::env;
14use crate::parser::instructions::expose;
15use crate::parser::instructions::from;
16use crate::parser::instructions::label;
17use crate::parser::instructions::run;
18use crate::parser::instructions::shell;
19use crate::parser::instructions::stopsignal;
20use crate::parser::instructions::user;
21use crate::parser::instructions::volume;
22use crate::parser::instructions::workdir;
23use crate::symbols::chars::HASHTAG;
24use crate::utils::read_lines;
25use crate::utils::split_instruction_and_arguments;
26
27#[derive(Debug)]
29pub struct Dockerfile {
30 pub path: PathBuf,
31 pub instructions: Vec<Instruction>,
32}
33
34impl Dockerfile {
35 pub fn new(path: PathBuf, instructions: Vec<Instruction>) -> Self {
39 Dockerfile { path, instructions }
40 }
41
42 pub fn empty(path: PathBuf) -> Self {
46 Dockerfile::new(path, Vec::new())
47 }
48
49 pub fn from(path: PathBuf) -> ParseResult<Self> {
68 let mut dockerfile = Dockerfile::empty(path);
69 dockerfile.instructions = dockerfile.parse()?;
70 Ok(dockerfile)
71 }
72
73 pub fn parse(&self) -> ParseResult<Vec<Instruction>> {
95 let file = File::open(&self.path).map_err(|e| ParseError::FileError(e.to_string()))?;
96 let lines = read_lines(&file);
97
98 let mut instructions = Vec::new();
99
100 for line in lines {
101 if line.is_empty() {
103 instructions.push(Instruction::EMPTY);
104 } else if line.starts_with(HASHTAG) {
106 instructions.push(Instruction::COMMENT(line.to_owned()));
107 } else {
108 let (instruction, arguments) = split_instruction_and_arguments(&line)?;
109 let instruction = match instruction.as_str() {
110 "ADD" => add::parse(arguments),
111 "ARG" => arg::parse(arguments),
112 "CMD" => cmd::parse(arguments),
113 "COPY" => copy::parse(arguments),
114 "ENTRYPOINT" => entrypoint::parse(arguments),
115 "ENV" => env::parse(arguments),
116 "EXPOSE" => expose::parse(arguments),
117 "LABEL" => label::parse(arguments),
118 "FROM" => from::parse(arguments),
119 "RUN" => run::parse(arguments),
120 "SHELL" => shell::parse(arguments),
121 "STOPSIGNAL" => stopsignal::parse(arguments),
122 "USER" => user::parse(arguments),
123 "VOLUME" => volume::parse(arguments),
124 "WORKDIR" => workdir::parse(arguments),
125 _ => return Err(ParseError::UnknownInstruction(instruction)),
126 };
127 match instruction {
128 Ok(instruction) => instructions.push(instruction),
129 Err(e) => {
130 return Err(ParseError::SyntaxError(format!("{line}: {e}")));
131 }
132 }
133 }
134 }
135 Ok(instructions)
136 }
137
138 pub fn dump(&self) -> std::io::Result<()> {
143 let mut file = File::create(&self.path)?;
144 for instruction in &self.instructions {
145 writeln!(file, "{instruction}")?;
146 }
147 Ok(())
148 }
149}