Skip to content

Commit

Permalink
* opa works (#106)
Browse files Browse the repository at this point in the history
* cargo fix now works
* check_overwrite now doesn't delete entire dir but only warns about overwrite
  • Loading branch information
bird-dancer authored Jul 14, 2023
1 parent a3c85b9 commit 7d7f23f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/generator/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ 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(_) => {
if input.trim() != "y" {
println!("Aborting generation...");
std::process::exit(0);
}
std::fs::remove_dir_all(output_path).unwrap();
}
Err(err) => {
println!("❌ Error reading input: {}", err);
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ fn main() {
"fix",
"--manifest-path",
output_path.join("Cargo.toml"),
"--allow-dirty"
"--allow-dirty",
"--allow-staged"
);

if args.doc {
Expand Down
4 changes: 2 additions & 2 deletions templates/src/handler/$$producer$$.rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use log::{debug, warn, error};
match serde_json::from_value::<{{ .payload.struct_reference }}>(payload) {
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 {
Expand Down Expand Up @@ -87,12 +87,12 @@ use log::{debug, warn, error};
{{ end }}
match serde_json::from_value::<{{ .payload.struct_reference }}>(payload) {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion templates/src/policy/mod.rs.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod policy;
use policy::*;
use policy::*;
7 changes: 3 additions & 4 deletions templates/src/policy/policy.rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(input: I) -> Result<serde_json::Value>
pub async fn opa_eval<I>(input: &I) -> Result<serde_json::Value>
where
Body: From<I>,
I: Serialize,
{
if let Ok(enabled) = env::var("OPA_ENABLED") {
Expand All @@ -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();
Expand Down

0 comments on commit 7d7f23f

Please sign in to comment.