Debian Codegraph @

src/builder.rs

raw symbols SCIP
1 use std::{
2 borrow::Cow,
3 fmt,
4 };
5
6 use crate::{
7 instructions::{
8 Arg,
9 Directive,
10 From,
11 Instruction,
12 },
13 };
14
15 /// A Dockerfile represented in code.
16 ///
17 /// A Dockerfile, conceptually, is a series of instructions. In code, that is exactly how they are
18 /// represented here. A wrapper around a `Vec<Instruction>` with a few convenience methods and
19 /// such.
20 pub struct Dockerfile(Vec<Instruction>);
21
22 impl fmt::Display for Dockerfile {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 write!(f, "{}", self.0.iter().fold(String::new(), |acc, elem| acc + &elem.to_string()))
25 }
26 }
27
28 impl Dockerfile {
29 /// Start building a new Dockerfile from the specified base image.
30 ///
31 /// Only requirement is the initial `FROM` instruction. Call `.finish()` when complete.
32 pub fn base<T: Into<Cow<'static, str>>>(from: T) -> DockerfileBuilder {
33 DockerfileBuilder{
34 initial_directives: None,
35 initial_args: None,
36 from: From::new(from),
37 instructions: None,
38 }
39 }
40 }
41
42 /// A Dockerfile builder.
43 pub struct DockerfileBuilder {
44 /// Any parser directives.
45 initial_directives: Option<Vec<Directive>>,
46
47 /// Any initial `ARG`s which are to appear before the initial `FROM` instruction.
48 initial_args: Option<Vec<Arg>>,
49
50 /// The new Dockerfile's initial `FROM` instruction.
51 from: From,
52
53 /// Any additional instructions part of the Dockerfile.
54 instructions: Option<Vec<Instruction>>,
55 }
56
57 impl DockerfileBuilder {
58 /// Push a new initial directive to this Dockerfile instance.
59 pub fn push_initial_directive(mut self, directive: Directive) -> Self {
60 if let Some(ref mut directives) = self.initial_directives {
61 directives.push(directive);
62 } else {
63 self.initial_directives = Some(vec![directive]);
64 }
65 self
66 }
67
68 /// Push a new initial arg to this Dockerfile instance.
69 pub fn push_initial_arg(mut self, arg: Arg) -> Self {
70 if let Some(ref mut args) = self.initial_args {
71 args.push(arg);
72 } else {
73 self.initial_args = Some(vec![arg]);
74 }
75 self
76 }
77
78 /// Push a new instruction into this Dockerfile instance.
79 pub fn push<I: Into<Instruction>>(mut self, instruction: I) -> Self {
80 if let Some(ref mut instructions) = self.instructions {
81 instructions.push(instruction.into());
82 } else {
83 self.instructions = Some(vec![instruction.into()]);
84 }
85 self
86 }
87
88 /// Append a vector of instructions to this Dockerfile instance.
89 pub fn append<I: Into<Instruction>>(mut self, new: Vec<I>) -> Self {
90 let mut new = new.into_iter().map(Into::into).collect();
91 if let Some(ref mut instructions) = self.instructions {
92 instructions.append(&mut new);
93 } else {
94 self.instructions = Some(new);
95 }
96 self
97 }
98
99 /// Generate the output Dockerfile as a string.
100 pub fn finish(self) -> Dockerfile {
101 // Add directives to the Dockerfile.
102 let mut all_instructions: Vec<Instruction> = vec![];
103 if let Some(directives) = self.initial_directives {
104 all_instructions.extend(directives.into_iter().map(|inst| Instruction::Directive(inst)));
105 }
106
107 // Add initial args to the Dockerfile.
108 if let Some(args) = self.initial_args {
109 all_instructions.extend(args.into_iter().map(|inst| Instruction::Arg(inst)));
110 }
111
112 // Add from instruction to Dockerfile.
113 all_instructions.push(Instruction::From(self.from));
114
115 // Append any other instructions in serial order.
116 if let Some(instructions) = self.instructions {
117 all_instructions.extend(instructions.into_iter());
118 }
119
120 Dockerfile(all_instructions)
121 }
122 }