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
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
#[derive(Clone, Debug)]
pub(crate) struct PackBuildCommand {
builder: String,
buildpacks: Vec<BuildpackReference>,
env: BTreeMap<String, String>,
image_name: String,
path: PathBuf,
pull_policy: PullPolicy,
verbose: bool,
}
#[derive(Clone, Debug)]
pub(crate) enum BuildpackReference {
Id(String),
Path(PathBuf),
}
impl From<PathBuf> for BuildpackReference {
fn from(path: PathBuf) -> Self {
BuildpackReference::Path(path)
}
}
impl From<&TempDir> for BuildpackReference {
fn from(path: &TempDir) -> Self {
BuildpackReference::Path(path.path().into())
}
}
impl From<String> for BuildpackReference {
fn from(id: String) -> Self {
BuildpackReference::Id(id)
}
}
#[derive(Clone, Debug)]
#[allow(dead_code)]
pub(crate) enum PullPolicy {
Always,
IfNotPresent,
Never,
}
impl PackBuildCommand {
pub fn new(
builder: impl Into<String>,
path: impl Into<PathBuf>,
image_name: impl Into<String>,
pull_policy: PullPolicy,
) -> PackBuildCommand {
PackBuildCommand {
builder: builder.into(),
buildpacks: Vec::new(),
env: BTreeMap::new(),
image_name: image_name.into(),
path: path.into(),
pull_policy,
verbose: false,
}
}
pub fn buildpack(&mut self, b: impl Into<BuildpackReference>) -> &mut Self {
self.buildpacks.push(b.into());
self
}
pub fn env(&mut self, k: impl Into<String>, v: impl Into<String>) -> &mut Self {
self.env.insert(k.into(), v.into());
self
}
}
impl From<PackBuildCommand> for Command {
fn from(pack_build_command: PackBuildCommand) -> Self {
let mut command = Command::new("pack");
let mut args = vec![
String::from("build"),
pack_build_command.image_name,
String::from("--builder"),
pack_build_command.builder,
String::from("--path"),
pack_build_command.path.to_string_lossy().to_string(),
String::from("--pull-policy"),
match pack_build_command.pull_policy {
PullPolicy::Always => "always".to_string(),
PullPolicy::IfNotPresent => "if-not-present".to_string(),
PullPolicy::Never => "never".to_string(),
},
];
for buildpack in pack_build_command.buildpacks {
args.push(String::from("--buildpack"));
match buildpack {
BuildpackReference::Id(id) => {
args.push(id);
}
BuildpackReference::Path(path_buf) => {
args.push(path_buf.to_string_lossy().to_string());
}
}
}
for (env_key, env_value) in &pack_build_command.env {
args.push(String::from("--env"));
args.push(format!("{env_key}={env_value}"));
}
if pack_build_command.verbose {
args.push(String::from("-v"));
}
command.args(args);
command
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::OsStr;
#[test]
fn from_pack_build_command_to_command() {
let mut input = PackBuildCommand {
builder: String::from("builder:20"),
buildpacks: vec![
BuildpackReference::Id(String::from("libcnb/buildpack1")),
BuildpackReference::Path(PathBuf::from("/tmp/buildpack2")),
],
env: BTreeMap::from([
(String::from("ENV_FOO"), String::from("FOO_VALUE")),
(String::from("ENV_BAR"), String::from("WHITESPACE VALUE")),
]),
image_name: String::from("my-image"),
path: PathBuf::from("/tmp/foo/bar"),
pull_policy: PullPolicy::IfNotPresent,
verbose: true,
};
let command: Command = input.clone().into();
assert_eq!(command.get_program(), "pack");
assert_eq!(
command.get_args().collect::<Vec<&OsStr>>(),
vec![
"build",
"my-image",
"--builder",
"builder:20",
"--path",
"/tmp/foo/bar",
"--pull-policy",
"if-not-present",
"--buildpack",
"libcnb/buildpack1",
"--buildpack",
"/tmp/buildpack2",
"--env",
"ENV_BAR=WHITESPACE VALUE",
"--env",
"ENV_FOO=FOO_VALUE",
"-v"
]
);
assert_eq!(command.get_envs().collect::<Vec<_>>(), vec![]);
input.verbose = false;
let command: Command = input.into();
assert!(!command.get_args().any(|arg| arg == OsStr::new("-v")));
}
}