-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Docs] Providing an example for the validate command (#487)
* providing a getting started example of how to use the validate command from the library * cleanup
- Loading branch information
1 parent
3dc0f22
commit 8fa7615
Showing
4 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
members = [ | ||
"guard", | ||
"guard-lambda", | ||
"guard-ffi" | ||
"guard-ffi", | ||
"guard-examples/library" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "library" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
cfn-guard = { version = "3.1.0-beta", path = "../../guard" } | ||
anyhow = "1.0.81" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::io::Cursor; | ||
|
||
use anyhow::Context; | ||
use cfn_guard::{ | ||
commands::{ | ||
validate::{OutputFormatType, ShowSummaryType}, | ||
Executable, | ||
}, | ||
utils::{ | ||
reader::{ReadBuffer, Reader}, | ||
writer::{WriteBuffer, Writer}, | ||
}, | ||
CommandBuilder, ValidateBuilder, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct Payload { | ||
pub data: String, | ||
pub rules: Vec<String>, | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let payload = r#"{"data": ["{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}","{\"Resources\":{\"NewVolume\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":500,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2b\"}},\"NewVolume2\":{\"Type\":\"AWS::EC2::Volume\",\"Properties\":{\"Size\":50,\"Encrypted\":false,\"AvailabilityZone\":\"us-west-2c\"}}},\"Parameters\":{\"InstanceName\":\"TestInstance\"}}"], "rules" : [ "Parameters.InstanceName == \"TestInstance\"","Parameters.InstanceName == \"TestInstance\"" ]}"#; | ||
let mut reader = Reader::new(ReadBuffer::Cursor(Cursor::new(Vec::from( | ||
payload.as_bytes(), | ||
)))); | ||
let mut writer = Writer::new_with_err(WriteBuffer::Vec(vec![]), WriteBuffer::Vec(vec![])); | ||
|
||
let cmd = ValidateBuilder::default() | ||
.payload(true) | ||
.output_format(OutputFormatType::JSON) | ||
.structured(true) | ||
.show_summary(vec![ShowSummaryType::None]) | ||
.try_build() | ||
.context("failed to build validate command")?; | ||
|
||
cmd.execute(&mut writer, &mut reader)?; | ||
|
||
let content = writer.stripped().context("failed to read from writer")?; | ||
println!("{content}"); | ||
|
||
Ok(()) | ||
} |