Skip to content

Commit f195b01

Browse files
committed
feat: Add examples to conventions file
1 parent be707a0 commit f195b01

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

CONVENTIONS.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,86 @@ This document outlines the coding conventions used in our project, particularly
7373
- Use consistent naming conventions across all functions.
7474

7575
These conventions are derived from the observed patterns in the project files. They should be followed when contributing new functions or modifying existing ones to maintain consistency across the project.
76+
77+
## Examples
78+
79+
### Example 1: Listing a Resource (VPCs)
80+
81+
The `vpcs` function is an example of listing a resource type. It demonstrates how to query and format the output for VPCs:
82+
83+
```bash
84+
vpcs() {
85+
# List VPCs
86+
#
87+
# $ vpcs
88+
# vpc-018d9739 default-vpc NO_NAME 172.31.0.0/16 NO_STACK NO_VERSION
89+
90+
local vpc_ids=$(skim-stdin)
91+
local filters=$(__bma_read_filters $@)
92+
93+
aws ec2 describe-vpcs \
94+
${vpc_ids/#/'--vpc-ids '} \
95+
--output text \
96+
--query '
97+
Vpcs[].[
98+
VpcId,
99+
((IsDefault==`false`)&&`not-default`)||`default-vpc`,
100+
join(`,`, [Tags[?Key==`Name`].Value || `NO_NAME`][]),
101+
CidrBlock,
102+
join(`,`, [Tags[?Key==`aws:cloudformation:stack-name`].Value || `NO_STACK`][]),
103+
join(`,`, [Tags[?Key==`version`].Value || `NO_VERSION`][])
104+
]' |
105+
grep -E -- "$filters" |
106+
columnise
107+
}
108+
```
109+
110+
This function showcases:
111+
- Using `skim-stdin` to handle both piped input and arguments
112+
- Applying filters with `__bma_read_filters`
113+
- Using AWS CLI with a complex query to format the output
114+
- Using `columnise` for consistent output formatting
115+
116+
### Example 2: Listing Associated Resources (VPC Subnets)
117+
118+
The `vpc-subnets` function is an example of listing resources associated with another resource. It demonstrates how to query and format the output for subnets associated with specific VPCs:
119+
120+
```bash
121+
vpc-subnets() {
122+
# List subnets for a specific VPC
123+
#
124+
# USAGE: vpc-subnets vpc-id [vpc-id]
125+
#
126+
# EXAMPLE:
127+
# $ vpc-subnets vpc-018d9739
128+
# subnet-34fd9cfa vpc-018d9739 ap-southeast-2c 172.31.32.0/20 NO_NAME
129+
# subnet-8bb774fe vpc-018d9739 ap-southeast-2a 172.31.0.0/20 NO_NAME
130+
# subnet-9eea2c07 vpc-018d9739 ap-southeast-2b 172.31.16.0/20 NO_NAME
131+
132+
local vpc_ids=$(skim-stdin "$@")
133+
[[ -z "$vpc_ids" ]] && __bma_usage "vpc-id [vpc-id]" && return 1
134+
135+
local vpc_id
136+
for vpc_id in $vpc_ids; do
137+
aws ec2 describe-subnets \
138+
--output text \
139+
--query "
140+
Subnets[?VpcId=='$vpc_id'].[
141+
SubnetId,
142+
VpcId,
143+
AvailabilityZone,
144+
CidrBlock,
145+
join(',', [Tags[?Key=='Name'].Value || 'NO_NAME'][])
146+
]"
147+
done |
148+
columnise
149+
}
150+
```
151+
152+
This function showcases:
153+
- Handling multiple VPC IDs
154+
- Error checking for empty input
155+
- Looping through VPC IDs to query associated subnets
156+
- Formatting output consistently with `columnise`
157+
158+
These examples illustrate the consistent patterns used across the project for querying AWS resources and formatting the output.

0 commit comments

Comments
 (0)