-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenapi.rs
More file actions
46 lines (39 loc) · 1.38 KB
/
openapi.rs
File metadata and controls
46 lines (39 loc) · 1.38 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
use std::{
io::Write,
process::{Command, Stdio},
};
use snafu::{ResultExt, Snafu, ensure};
use stackable_cockpitd::api_doc::openapi;
#[derive(Debug, Snafu)]
pub enum GenOpenapiError {
#[snafu(display("error serializing openapi"))]
SerializeOpenApi { source: serde_json::Error },
#[snafu(display("error running importing openapi schema importer"))]
ImportOpenapiSchemaRun { source: std::io::Error },
#[snafu(display("openapi schema importer failed with error code {error_code:?}"))]
ImportOpenapiSchema { error_code: Option<i32> },
#[snafu(display("error writing openapi schema into importer"))]
WriteOpenapiSchema { source: std::io::Error },
}
pub fn generate() -> Result<(), GenOpenapiError> {
let openapi_json = openapi().to_json().context(SerializeOpenApiSnafu)?;
let mut codegen = Command::new("yarn")
.args(["--cwd", "web", "run", "openapi-codegen"])
.stdin(Stdio::piped())
.spawn()
.context(ImportOpenapiSchemaRunSnafu)?;
codegen
.stdin
.take()
.expect("child stdin must be available")
.write_all(openapi_json.as_bytes())
.context(WriteOpenapiSchemaSnafu)?;
let status = codegen.wait().context(ImportOpenapiSchemaRunSnafu)?;
ensure!(
status.success(),
ImportOpenapiSchemaSnafu {
error_code: status.code()
}
);
Ok(())
}