-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.rs
415 lines (339 loc) · 12.1 KB
/
main.rs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#![deny(rust_2018_idioms, warnings)]
#![deny(clippy::all, clippy::pedantic)]
#![allow(
clippy::cognitive_complexity,
clippy::default_trait_access,
clippy::let_underscore_drop,
clippy::must_use_candidate,
clippy::similar_names,
clippy::too_many_arguments,
clippy::too_many_lines,
clippy::type_complexity,
clippy::unseparated_literal_suffix,
clippy::use_self,
)]
mod fixups;
mod logger;
mod supported_version;
use k8s_openapi_codegen_common::swagger20;
struct Error(Box<dyn std::error::Error + Send + Sync>, backtrace::Backtrace);
impl<E> From<E> for Error where E: Into<Box<dyn std::error::Error + Send + Sync>> {
fn from(value: E) -> Self {
Error(value.into(), backtrace::Backtrace::new())
}
}
impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.0)?;
let mut source = self.0.source();
while let Some(err) = source {
writeln!(f, "caused by: {}", err)?;
source = err.source();
}
write!(f, "{:?}", self.1)?;
Ok(())
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}
fn main() -> Result<(), Error> {
let Options { versions: requested_versions } = structopt::StructOpt::from_args();
{
let logger = logger::Logger;
log::set_boxed_logger(Box::new(logger))?;
let mut builder = env_logger::Builder::new();
let rust_log = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
builder.parse_filters(&rust_log);
let logger = builder.build();
log::set_max_level(logger.filter());
}
let client =
std::sync::Arc::new(
reqwest::blocking::ClientBuilder::new()
.timeout(None)
.build().expect("could not create reqwest client"));
let out_dir_base: &std::path::Path = env!("CARGO_MANIFEST_DIR").as_ref();
let out_dir_base = std::sync::Arc::new(out_dir_base.parent().ok_or("path does not have a parent")?.join("src"));
let requested_versions =
if requested_versions.is_empty() {
supported_version::ALL.iter().map(|&version| RequestedVersion { version, overriden_spec_url: None }).collect()
}
else {
requested_versions
};
let threads: Vec<_> =
requested_versions.into_iter()
.map(|RequestedVersion { version, overriden_spec_url }| {
let version_name = version.name();
std::thread::Builder::new().name(version_name.to_owned()).spawn({
let out_dir_base = out_dir_base.clone();
let client = client.clone();
move || -> Result<(), Error> {
{
let mut builder = env_logger::Builder::new();
builder.format(move |buf, record| {
use std::io::Write;
writeln!(buf, "[{}] {} {}:{} {}", version_name, record.level(), record.file().unwrap_or("?"), record.line().unwrap_or(0), record.args())
});
let rust_log = std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
builder.parse_filters(&rust_log);
logger::register_thread_local_logger(builder.build());
}
let spec_url = overriden_spec_url.as_deref().unwrap_or_else(|| version.spec_url());
run(version, spec_url, &out_dir_base, &client)?;
Ok(())
}
}).unwrap()
})
.collect();
let mut result = Ok(());
for thread in threads {
let thread_name = thread.thread().name().unwrap().to_owned();
if let Err(err) = thread.join().unwrap() {
eprintln!("[{}] {:?}", thread_name, err);
result = Err("one or more runs failed".into());
}
}
result
}
#[derive(structopt::StructOpt)]
struct Options {
/// This parameter specifies the versions of Kubernetes that the API bindings should be generated for.
///
/// `--generate=1.20` means "generate bindings for Kubernetes v1.20,
/// based on that version's OpenAPI spec from the https://github.com/kubernetes/kubernetes repository".
///
/// `--generate=1.20:https://example.org/spec.json` means "generate binding for v1.20,
/// based on the OpenAPI spec at https://example.org/spec.json".
///
/// This parameter can be specified multiple times to specify multiple versions.
///
/// If this parameter isn't specified, the API bindings will be generated for all supported versions,
/// based on their OpenAPI specs from the https://github.com/kubernetes/kubernetes repository.
#[structopt(long = "generate", value_name = "VERSION")]
versions: Vec<RequestedVersion>,
}
struct RequestedVersion {
version: supported_version::SupportedVersion,
overriden_spec_url: Option<String>,
}
impl std::str::FromStr for RequestedVersion {
type Err = Error;
fn from_str(spec: &str) -> Result<Self, Error> {
let (version, overriden_spec_url) =
spec.split_once(':')
.map_or(
(spec, None),
|(version, url)| (version, Some(url.to_owned())),
);
let &version =
supported_version::ALL.iter()
.find(|v| v.name() == version)
.ok_or_else(|| {
let mut err = format!("unknown version {:?} requested, supported versions are ", version);
for (i, &version) in supported_version::ALL.iter().enumerate() {
if i > 0 {
err.push_str(", ");
}
err.push('"');
err.push_str(version.name());
err.push('"');
}
err
})?;
Ok(RequestedVersion {
version,
overriden_spec_url,
})
}
}
fn run(
supported_version: supported_version::SupportedVersion,
spec_url: &str,
out_dir_base: &std::path::Path,
client: &reqwest::blocking::Client,
) -> Result<(), Error> {
let mod_root = supported_version.mod_root();
let out_dir = out_dir_base.join(mod_root);
let mut num_generated_structs = 0usize;
let mut num_generated_type_aliases = 0usize;
let mut num_generated_apis = 0usize;
let mut spec: swagger20::Spec = {
log::info!("Parsing spec file at {} ...", spec_url);
let response = client.get(spec_url).send()?;
let status = response.status();
if status != http::StatusCode::OK {
return Err(status.to_string().into());
}
response.json()?
};
log::info!("Applying fixups...");
supported_version.fixup(&mut spec)?;
let expected_num_generated_types: usize = spec.definitions.len();
let expected_num_generated_apis: usize = spec.operations.len();
log::info!("OK. Spec has {} definitions and {} operations", expected_num_generated_types, expected_num_generated_apis);
loop {
log::info!("Removing output directory {} ...", out_dir.display());
match std::fs::remove_dir_all(&out_dir) {
Ok(()) => log::trace!("OK"),
Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => {
log::trace!("OK. Directory doesn't exist");
log::info!("Creating output directory {} ...", out_dir.display());
match std::fs::create_dir(&out_dir) {
Ok(()) => {
log::trace!("OK");
break;
},
Err(err) => log::warn!("Error: {}", err),
}
},
Err(err) => log::warn!("Error: {}", err),
}
}
log::info!("Generating types...");
for definition_path in spec.definitions.keys() {
log::trace!("Working on {} ...", definition_path);
let run_state = RunState {
out_dir: &out_dir,
parent_mod_rs_file_and_mod_name: None,
};
let run_result = k8s_openapi_codegen_common::run(
&spec.definitions,
&mut spec.operations,
definition_path,
&MapNamespace,
"pub ",
Some("api"),
run_state,
)?;
num_generated_structs += run_result.num_generated_structs;
num_generated_type_aliases += run_result.num_generated_type_aliases;
num_generated_apis += run_result.num_generated_apis;
}
// Top-level operations
{
let mut mod_root_file = std::io::BufWriter::new(std::fs::OpenOptions::new().append(true).open(out_dir.join("mod.rs"))?);
spec.operations.sort_by(|o1, o2| o1.id.cmp(&o2.id));
for operation in spec.operations {
if let Some(swagger20::KubernetesGroupKindVersion { group, kind, version }) = operation.kubernetes_group_kind_version {
return Err(format!(
"Operation {} is associated with {}/{}/{} but did not get emitted with that definition",
operation.id, group, version, kind).into());
}
k8s_openapi_codegen_common::write_operation(
&mut mod_root_file,
&operation,
&MapNamespace,
"pub ",
None,
Some("api"),
)?;
num_generated_apis += 1;
}
}
log::info!("OK");
log::info!("Generated {} structs", num_generated_structs);
log::info!("Generated {} type aliases", num_generated_type_aliases);
log::info!("Generated {} API functions", num_generated_apis);
if num_generated_structs + num_generated_type_aliases != expected_num_generated_types {
return Err("Did not generate or skip expected number of types".into());
}
if num_generated_apis != expected_num_generated_apis {
return Err("Did not generate expected number of API functions".into());
}
log::info!("");
Ok(())
}
struct MapNamespace;
impl k8s_openapi_codegen_common::MapNamespace for MapNamespace {
fn map_namespace<'a>(&self, path_parts: &[&'a str]) -> Option<Vec<&'a str>> {
match path_parts {
["io", "k8s", rest @ ..] => Some(std::iter::once("crate").chain(rest.iter().copied()).collect()),
_ => None,
}
}
}
struct RunState<'a> {
out_dir: &'a std::path::Path,
parent_mod_rs_file_and_mod_name: Option<(<Self as k8s_openapi_codegen_common::RunState>::Writer, std::borrow::Cow<'static, str>)>,
}
impl k8s_openapi_codegen_common::RunState for RunState<'_> {
type Writer = std::io::BufWriter<std::fs::File>;
fn make_writer(
&mut self,
parts: &[&str],
type_feature: Option<&str>,
) -> std::io::Result<Self::Writer> {
use std::io::Write;
let mut current = self.out_dir.to_owned();
for part in parts.iter().skip(1).rev().skip(1).rev() {
log::trace!("Current directory: {}", current.display());
let mod_name = k8s_openapi_codegen_common::get_rust_ident(part);
current.push(&*mod_name);
log::trace!("Checking if subdirectory {} exists...", current.display());
if !current.is_dir() {
log::trace!(" Subdirectory does not exist. Creating mod.rs with a reference to it...");
let current_mod_rs_path = current.with_file_name("mod.rs");
let append_newline = current_mod_rs_path.exists();
let mut parent_mod_rs = std::io::BufWriter::new(std::fs::OpenOptions::new().append(true).create(true).open(current_mod_rs_path)?);
if append_newline {
writeln!(parent_mod_rs)?;
}
writeln!(parent_mod_rs, "pub mod {};", mod_name)?;
log::trace!(" OK");
log::trace!(" Creating subdirectory...");
std::fs::create_dir(¤t)?;
log::trace!(" OK");
}
log::trace!("OK");
}
let type_name = parts.last().unwrap();
let mod_name = k8s_openapi_codegen_common::get_rust_ident(type_name);
let mut parent_mod_rs = std::io::BufWriter::new(std::fs::OpenOptions::new().append(true).create(true).open(current.join("mod.rs"))?);
writeln!(parent_mod_rs)?;
if let Some(type_feature) = type_feature {
writeln!(parent_mod_rs, r#"#[cfg(feature = "{}")]"#, type_feature)?;
}
writeln!(parent_mod_rs, "mod {};", mod_name)?;
if let Some(type_feature) = type_feature {
writeln!(parent_mod_rs, r#"#[cfg(feature = "{}")]"#, type_feature)?;
}
writeln!(parent_mod_rs, "pub use self::{}::{};", mod_name, type_name)?;
let file_name = current.join(&*mod_name).with_extension("rs");
let file = std::io::BufWriter::new(std::fs::File::create(file_name)?);
self.parent_mod_rs_file_and_mod_name = Some((parent_mod_rs, mod_name));
Ok(file)
}
fn handle_operation_types(
&mut self,
operation_optional_parameters_name: Option<&str>,
operation_result_name: Option<&str>,
) -> std::io::Result<()> {
use std::io::Write;
let (parent_mod_rs, mod_name) = self.parent_mod_rs_file_and_mod_name.as_mut().unwrap();
match (operation_optional_parameters_name, operation_result_name) {
(Some(operation_optional_parameters_name), Some(operation_result_name)) =>
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{{{}, {}}};"#,
mod_name, operation_optional_parameters_name, operation_result_name)?,
(Some(operation_optional_parameters_name), None) =>
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{};"#,
mod_name, operation_optional_parameters_name)?,
(None, Some(operation_result_name)) =>
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{};"#,
mod_name, operation_result_name)?,
(None, None) =>
(),
}
Ok(())
}
fn finish(&mut self, _writer: Self::Writer) { }
}