feat: with bootstrap

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-09-25 22:16:06 +02:00
parent 3be2a792dc
commit e16604782c
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
2 changed files with 23 additions and 13 deletions

View File

@ -109,15 +109,13 @@ async fn main() -> anyhow::Result<()> {
) )
.with_default("src/gencrunch") .with_default("src/gencrunch")
.prompt()?; .prompt()?;
let entity = inquire::Text::new("entity")
.with_help_message("please set which entity you want to publish for")
.with_default("example")
.prompt_skippable()?;
let bootstrap_schema = inquire::Confirm::new("bootstrap schema file") if let Some(entity) = entity {
.with_help_message( let config = config.add_publish(&schema_path, &output_path, &[&entity]);
"will create an example protobuf file in the supplied schema_path",
)
.with_default(true)
.prompt()?;
if bootstrap_schema {
let config = config.add_publish(&schema_path, &output_path);
config.write_file(&cli.global_args.crunch_file).await?; config.write_file(&cli.global_args.crunch_file).await?;
let schema_output_path = cli let schema_output_path = cli
@ -126,7 +124,7 @@ async fn main() -> anyhow::Result<()> {
.parent() .parent()
.unwrap_or(&PathBuf::from("")) .unwrap_or(&PathBuf::from(""))
.join(&schema_path) .join(&schema_path)
.join("event.proto"); .join(format!("{}.proto", entity));
if let Some(dir) = schema_output_path.parent() { if let Some(dir) = schema_output_path.parent() {
if !dir.exists() { if !dir.exists() {
tokio::fs::create_dir_all(dir).await?; tokio::fs::create_dir_all(dir).await?;
@ -146,7 +144,7 @@ message MyEvent {{
string my_field = 1; string my_field = 1;
}} }}
"#, "#,
config.service.domain, config.service.service config.service.domain, entity
) )
.as_bytes(), .as_bytes(),
) )

View File

@ -26,6 +26,7 @@ pub struct Publish {
pub schema_path: String, pub schema_path: String,
#[serde(alias = "output-path")] #[serde(alias = "output-path")]
pub output_path: String, pub output_path: String,
pub entities: Vec<String>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -65,11 +66,22 @@ impl File {
Ok(content) Ok(content)
} }
pub fn add_publish(&mut self, schema_path: &str, output_path: &str) -> &mut Self { pub fn add_publish(
&mut self,
schema_path: &str,
output_path: &str,
entities: &[&str],
) -> &mut Self {
let mut publish = toml_edit::Table::new(); let mut publish = toml_edit::Table::new();
publish["schema-path"] = value(schema_path); publish["schema-path"] = value(schema_path);
publish["output-path"] = value(output_path); publish["output-path"] = value(output_path);
let mut entities_arr = toml_edit::Array::new();
for entity in entities {
entities_arr.push(entity.to_string());
}
publish["entities"] = value(entities_arr);
if !self.doc.contains_key("publish") { if !self.doc.contains_key("publish") {
tracing::debug!("publish key not existing, adding new"); tracing::debug!("publish key not existing, adding new");
self.doc["publish"] = toml_edit::array() self.doc["publish"] = toml_edit::array()
@ -141,7 +153,7 @@ schema-path = "some-schema"
output-path = "some-output" output-path = "some-output"
"#; "#;
let mut config = File::parse(raw).await?; let mut config = File::parse(raw).await?;
let config = config.add_publish("some-schema", "some-output"); let config = config.add_publish("some-schema", "some-output", &[]);
let output = config.write().await?; let output = config.write().await?;
pretty_assertions::assert_eq!(output, expected); pretty_assertions::assert_eq!(output, expected);
@ -166,7 +178,7 @@ schema-path = "some-schema"
output-path = "some-output" output-path = "some-output"
"#; "#;
let mut config = File::parse(raw).await?; let mut config = File::parse(raw).await?;
let config = config.add_publish("some-schema", "some-output"); let config = config.add_publish("some-schema", "some-output", &[]);
let output = config.write().await?; let output = config.write().await?;
pretty_assertions::assert_eq!(output, expected); pretty_assertions::assert_eq!(output, expected);