-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathconfig.rs
More file actions
199 lines (175 loc) · 6.17 KB
/
config.rs
File metadata and controls
199 lines (175 loc) · 6.17 KB
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
// Copyright (c) [2025] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.
use fluent_uri::Uri;
use merge::Merge;
use serde::{Deserialize, Serialize};
use crate::api::files::{
scripts::{InitScript, PostPartitioningScript, PostScript, PreScript},
user_file::UserFile,
FileSourceError, WithFileSource,
};
#[derive(Clone, Debug, Default, Serialize, Deserialize, Merge, utoipa::ToSchema)]
#[schema(as = files::Config)]
pub struct Config {
#[serde(skip_serializing_if = "Option::is_none")]
#[merge(strategy = merge::option::overwrite_none)]
pub files: Option<Vec<UserFile>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[merge(strategy = merge::option::overwrite_none)]
pub scripts: Option<ScriptsConfig>,
}
impl Config {
pub fn resolve_urls(&mut self, base_uri: &Uri<String>) -> Result<(), FileSourceError> {
resolve_urls_for(&mut self.files, base_uri)?;
if let Some(scripts) = &mut self.scripts {
scripts.resolve_urls(base_uri)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, Merge, utoipa::ToSchema, PartialEq)]
#[serde(rename_all = "camelCase")]
#[merge(strategy = merge::option::overwrite_none)]
pub struct ScriptsConfig {
/// User-defined pre-installation scripts
#[serde(skip_serializing_if = "Option::is_none")]
pub pre: Option<Vec<PreScript>>,
/// User-defined post-partitioning scripts
#[serde(skip_serializing_if = "Option::is_none")]
pub post_partitioning: Option<Vec<PostPartitioningScript>>,
/// User-defined post-installation scripts
#[serde(skip_serializing_if = "Option::is_none")]
pub post: Option<Vec<PostScript>>,
/// User-defined init scripts
#[serde(skip_serializing_if = "Option::is_none")]
pub init: Option<Vec<InitScript>>,
}
impl ScriptsConfig {
pub fn to_option(self) -> Option<Self> {
if self.pre.is_none()
&& self.post_partitioning.is_none()
&& self.post.is_none()
&& self.init.is_none()
{
None
} else {
Some(self)
}
}
/// Resolve relative URLs in the scripts.
///
/// * `base_uri`: The base URI to resolve relative URLs against.
pub fn resolve_urls(&mut self, base_uri: &Uri<String>) -> Result<(), FileSourceError> {
resolve_urls_for(&mut self.pre, base_uri)?;
resolve_urls_for(&mut self.post_partitioning, base_uri)?;
resolve_urls_for(&mut self.post, base_uri)?;
resolve_urls_for(&mut self.init, base_uri)?;
Ok(())
}
}
fn resolve_urls_for<T: WithFileSource>(
files: &mut Option<Vec<T>>,
base_uri: &Uri<String>,
) -> Result<(), FileSourceError> {
if let Some(ref mut files) = files {
for file in files {
file.resolve_url(base_uri)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use crate::api::files::{BaseScript, FileSource};
use super::*;
fn base_script(name: &str) -> BaseScript {
BaseScript {
name: name.to_string(),
source: FileSource::Text {
content: "".to_string(),
},
}
}
fn build_pre_script(name: &str) -> PreScript {
PreScript {
base: base_script(name),
}
}
#[test]
fn test_merge_with_default_scripts() {
let mut new_config = ScriptsConfig {
pre: Some(vec![build_pre_script("test")]),
..Default::default()
};
new_config.merge(Default::default());
let pre_scripts = new_config.pre.unwrap();
assert_eq!(pre_scripts.len(), 1);
}
#[test]
fn test_merge_scripts() {
let original = ScriptsConfig {
pre: Some(vec![build_pre_script("test")]),
..Default::default()
};
let mut updated = ScriptsConfig {
pre: Some(vec![build_pre_script("updated")]),
..Default::default()
};
updated.merge(original);
let pre_scripts = updated.pre.unwrap();
assert_eq!(pre_scripts.len(), 1);
let script = pre_scripts.first().unwrap();
assert_eq!(&script.base.name, "updated");
}
#[test]
fn test_merge_files_config() {
let mut updated = Config {
files: Some(vec![UserFile {
destination: "/foo".to_string(),
..Default::default()
}]),
scripts: Some(ScriptsConfig {
pre: Some(vec![build_pre_script("updated_pre")]),
..Default::default()
}),
};
let original = Config {
files: Some(vec![UserFile {
destination: "/bar".to_string(),
..Default::default()
}]),
scripts: Some(ScriptsConfig {
pre: Some(vec![build_pre_script("original_pre")]),
post: Some(vec![]),
..Default::default()
}),
};
let updated_clone = updated.clone();
updated.merge(original);
// Assert for files (overwrite_none)
// `updated.files` is Some, so it is not overwritten.
assert_eq!(updated.files, updated_clone.files);
// Assert for scripts (overwrite_none on Option<ScriptsConfig>)
// `updated.scripts` is Some, so it is not overwritten.
assert_eq!(updated.scripts, updated_clone.scripts);
// The inner fields of `updated.scripts` should not have changed.
assert!(updated.scripts.as_ref().unwrap().post.is_none());
}
}