Skip to content

Commit 8fa7615

Browse files
[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
1 parent 3dc0f22 commit 8fa7615

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

Cargo.lock

Lines changed: 25 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
members = [
44
"guard",
55
"guard-lambda",
6-
"guard-ffi"
6+
"guard-ffi",
7+
"guard-examples/library"
78
]

guard-examples/library/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "library"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
cfn-guard = { version = "3.1.0-beta", path = "../../guard" }
10+
anyhow = "1.0.81"

guard-examples/library/src/main.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::io::Cursor;
2+
3+
use anyhow::Context;
4+
use cfn_guard::{
5+
commands::{
6+
validate::{OutputFormatType, ShowSummaryType},
7+
Executable,
8+
},
9+
utils::{
10+
reader::{ReadBuffer, Reader},
11+
writer::{WriteBuffer, Writer},
12+
},
13+
CommandBuilder, ValidateBuilder,
14+
};
15+
16+
#[derive(Debug)]
17+
pub struct Payload {
18+
pub data: String,
19+
pub rules: Vec<String>,
20+
}
21+
22+
fn main() -> anyhow::Result<()> {
23+
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\"" ]}"#;
24+
let mut reader = Reader::new(ReadBuffer::Cursor(Cursor::new(Vec::from(
25+
payload.as_bytes(),
26+
))));
27+
let mut writer = Writer::new_with_err(WriteBuffer::Vec(vec![]), WriteBuffer::Vec(vec![]));
28+
29+
let cmd = ValidateBuilder::default()
30+
.payload(true)
31+
.output_format(OutputFormatType::JSON)
32+
.structured(true)
33+
.show_summary(vec![ShowSummaryType::None])
34+
.try_build()
35+
.context("failed to build validate command")?;
36+
37+
cmd.execute(&mut writer, &mut reader)?;
38+
39+
let content = writer.stripped().context("failed to read from writer")?;
40+
println!("{content}");
41+
42+
Ok(())
43+
}

0 commit comments

Comments
 (0)