-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.rs
80 lines (66 loc) · 1.96 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
// Copyright 2022 Oxide Computer Company
use anyhow::{anyhow, Result};
use clap::Parser;
use semver::Version;
use std::io::Write;
use std::net::SocketAddr;
use std::path::PathBuf;
use crucible_pantry::*;
#[derive(Debug, Parser)]
#[clap(name = PROG, about = "Crucible volume maintenance agent")]
enum Args {
OpenApi {
#[clap(short = 'o', action)]
output: PathBuf,
},
Run {
#[clap(short = 'l', action)]
listen: SocketAddr,
},
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::try_parse()?;
match args {
Args::OpenApi { output } => {
let mut f = std::fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(output)?;
write_openapi(&mut f)
}
Args::Run { listen } => {
let (log, pantry) = initialize_pantry()?;
let (_, join_handle) = server::run_server(&log, listen, &pantry)?;
join_handle.await?.map_err(|e| anyhow!(e))
}
}
}
fn write_openapi<W: Write>(f: &mut W) -> Result<()> {
let api = server::make_api().map_err(|e| anyhow!(e))?;
api.openapi("Crucible Pantry", Version::new(0, 0, 1))
.write(f)?;
Ok(())
}
#[cfg(test)]
mod tests {
use openapiv3::OpenAPI;
use crate::write_openapi;
#[test]
fn test_crucible_pantry_openapi() {
let mut raw = Vec::new();
write_openapi(&mut raw).unwrap();
let actual = String::from_utf8(raw).unwrap();
// Make sure the result parses as a valid OpenAPI spec.
let spec = serde_json::from_str::<OpenAPI>(&actual)
.expect("output was not valid OpenAPI");
// Check for lint errors.
let errors = openapi_lint::validate(&spec);
assert!(errors.is_empty(), "{}", errors.join("\n\n"));
expectorate::assert_contents(
"../openapi/crucible-pantry.json",
&actual,
);
}
}