From 489b19f17a83b21e2f57aa2746284b2e00f37995 Mon Sep 17 00:00:00 2001 From: Felix Date: Tue, 11 Jul 2023 22:00:48 +0200 Subject: [PATCH] * opa works * cargo fix now works (we might not want this) * check_overwrite now doesn't delete entire dir but only warns about overwrite --- src/generator/common.rs | 3 +-- src/main.rs | 3 ++- templates/src/handler/$$producer$$.rs.go | 4 ++-- templates/src/policy/mod.rs.go | 4 ++-- templates/src/policy/policy.rs.go | 7 +++---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/generator/common.rs b/src/generator/common.rs index 6e1b4ea..6371818 100644 --- a/src/generator/common.rs +++ b/src/generator/common.rs @@ -27,7 +27,7 @@ macro_rules! cargo_command { /// checks if project with name already exists, if yes asks for permission to overwrite pub fn check_for_overwrite(output_path: &Path, project_title: &str) { if output_path.exists() { - println!("\nA project with the name {} already exists in the current directory: {}. Do you want to overwrite it? \n\nā— WARNING: This will permanently delete all files in the directory! \n\nType 'y' to continue or anything else to exit.", project_title, output_path.to_string_lossy()); + println!("\nA project with the name {} already exists in the current directory: {}. Do you want to overwrite it? \n\nā— WARNING: Existing files within the folder will be permanently replaced by newly generated files. \n\nType 'y' to continue or anything else to exit.", project_title, output_path.to_string_lossy()); let mut input = String::new(); match std::io::stdin().read_line(&mut input) { Ok(_) => { @@ -35,7 +35,6 @@ pub fn check_for_overwrite(output_path: &Path, project_title: &str) { println!("Aborting generation..."); std::process::exit(0); } - std::fs::remove_dir_all(output_path).unwrap(); } Err(err) => { println!("āŒ Error reading input: {}", err); diff --git a/src/main.rs b/src/main.rs index 089f7a3..cea249a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,7 +66,8 @@ fn main() { "fix", "--manifest-path", output_path.join("Cargo.toml"), - "--allow-dirty" + "--allow-dirty", + "--allow-staged" ); if args.doc { diff --git a/templates/src/handler/$$producer$$.rs.go b/templates/src/handler/$$producer$$.rs.go index 632e57f..68f5d78 100644 --- a/templates/src/handler/$$producer$$.rs.go +++ b/templates/src/handler/$$producer$$.rs.go @@ -25,7 +25,7 @@ use log::{debug, warn}; match serde_json::from_slice::<{{ .payload.struct_reference }}>(&message.message.payload.as_ref()) { Ok(deserialized_message) => { debug!("Received message {:#?}", deserialized_message); - let policy_reply = opa_eval(message); + let policy_reply = opa_eval(&deserialized_message); // TODO: Replace this with your own handler code {{ if eq .payload.model_type "enum"}} match deserialized_message { @@ -55,12 +55,12 @@ use log::{debug, warn}; {{ if .payload}} match serde_json::from_slice::<{{ .payload.struct_reference }}>(&message.payload.as_ref()) { Ok(deserialized_message) => { + let policy_reply = opa_eval(&deserialized_message); {{ if eq .payload.model_type "enum"}} match deserialized_message { {{$enumName := .payload.unique_id}} {{ range .payload.related_models }} {{ $enumName }}::{{ .unique_id }}(payload) => { - let policy_reply = opa_eval(message); // TODO: Replace this with your own handler code debug!("Received message payload {{ .unique_id }} {:?}", payload); } diff --git a/templates/src/policy/mod.rs.go b/templates/src/policy/mod.rs.go index 648760c..877f7a6 100644 --- a/templates/src/policy/mod.rs.go +++ b/templates/src/policy/mod.rs.go @@ -1,2 +1,2 @@ -mod policy; -use policy::*; +pub mod policy; +pub use policy::*; diff --git a/templates/src/policy/policy.rs.go b/templates/src/policy/policy.rs.go index 4656a86..4c57d92 100644 --- a/templates/src/policy/policy.rs.go +++ b/templates/src/policy/policy.rs.go @@ -2,12 +2,11 @@ use anyhow::{anyhow, Result}; use opa_wasm::Runtime; use reqwest::{self, Body, Client, IntoUrl, Response}; use serde::Serialize; -use std::{env, fs, path::Path}; +use std::env; use wasmtime::{Config, Engine, Module, Store}; -pub async fn opa_eval(input: I) -> Result +pub async fn opa_eval(input: &I) -> Result where - Body: From, I: Serialize, { if let Ok(enabled) = env::var("OPA_ENABLED") { @@ -18,7 +17,7 @@ where } if let Ok(url) = env::var("OPA_REMOTE_URL") { let url: String = url.parse().unwrap(); - return opa_eval_remote(url, input).await; + return opa_eval_remote(url, serde_json::to_string(&input)?).await; } if let Ok(path) = env::var("OPA_LOCAL_WASM_PATH") { let path: String = path.parse().unwrap();