Skip to content

Commit

Permalink
Merge pull request #358 from DaveYesland/feature/add_jq_command
Browse files Browse the repository at this point in the history
add jq command to parse collected data
  • Loading branch information
DaveYesland authored Aug 7, 2023
2 parents c4eefa9 + e184f0d commit 55bed39
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
20 changes: 20 additions & 0 deletions pacu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from datetime import datetime

try:
import jq # type: ignore
import requests
import boto3
import botocore
Expand Down Expand Up @@ -90,6 +91,7 @@ def display_pacu_help():
data Display all data that is stored in this session. Only fields
with values will be displayed
data <service> [<sub-service>] Display all data for a specified service in this session
jq <query> <service> [<sub-service>] Run a jq statement on the specified service's data
services Display a list of services that have collected data in the
current session to use with the "data" command
regions Display a list of all valid AWS regions
Expand Down Expand Up @@ -667,6 +669,8 @@ def parse_command(self, command):
return
elif command[0] == 'data':
self.parse_data_command(command)
elif command[0] == 'jq':
self.parse_jq_command(command)
elif command[0] == 'sessions' or command[0] == 'list_sessions':
self.list_sessions()
elif command[0] == 'swap_session':
Expand Down Expand Up @@ -810,6 +814,22 @@ def _parse_data_command_sub_service(self, service_data: dict, sub_service: str)
else:
return json.dumps(service_data[name], indent=2, sort_keys=True, default=str)

def parse_jq_command(self, command):
session = self.get_active_session()
data_command = ["data"] + command[2:]
data = self._parse_data_command(data_command, session)
try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
print(data)
return
try:
jq_output = jq.all(command[1], data)
except ValueError as e:
print(e)
return
print(json.dumps(jq_output, indent=2, sort_keys=True, default=str))

def parse_set_regions_command(self, command):
session = self.get_active_session()

Expand Down
67 changes: 66 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typing-extensions = "^3.7.4.3"
dsnap = "^1.0.0"
chalice = "^1.23.0"
policyuniverse = "^1.5.0.20220613"
jq = "^1.4.1"

[tool.poetry.dev-dependencies]
flake8 = "^3.9.1"
Expand Down

0 comments on commit 55bed39

Please sign in to comment.