Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jq command to parse collected data #358

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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