Skip to content

Instantly share code, notes, and snippets.

@ajkerrigan
Last active March 1, 2023 05:32
Show Gist options
  • Save ajkerrigan/408571cf1081faadbee6d2af7fb28dec to your computer and use it in GitHub Desktop.
Save ajkerrigan/408571cf1081faadbee6d2af7fb28dec to your computer and use it in GitHub Desktop.
VisiData Cloud Custodian Sidebar

A Cloud Custodian Output Directory Sidebar Supplement in VisiData

Some experimentation with providing dynamic sidebar help information following discussion in saulpw/visidata#1733

The idea is to conditionally override the help text on a DirSheet only if it looks like we're in a Cloud Custodian output directory.

This is a hack that could be added to ~/.visidatarc... but probably shouldn't yet.

Demo

asciicast

import json
import textwrap
from visidata import DirSheet, vd
from functools import cache, wraps
def c7n_help(original_help):
'''Add Cloud Custodian-specific help to the DirSheet when relevant
If it looks like we're in a DirSheet for a Cloud Custodian output directory,
show some high-level details about the policy and output files.
'''
c7n_filenames = {'custodian-run.log', 'metadata.json', 'resources.json'}
@property
@cache
@wraps(DirSheet.help)
def wrapper(self):
filenames = {row._path.name.removesuffix('.gz') for row in self.rows}
if not c7n_filenames <= filenames:
return original_help
policy_metadata = json.loads(next(row for row in vd.sheet.rows if row._path.name == 'metadata.json').read_text())
resources = json.loads(next(row for row in vd.sheet.rows if row._path.name == 'resources.json').read_text())
source_files = '\n'.join(f'''
- {config}
'''
for config in policy_metadata['config']['configs']
)
return original_help + textwrap.dedent(f'''
## Cloud Custodian
This looks like a Cloud Custodian output directory!
[:bold]Policy:[:] {policy_metadata['policy']['name']}
[:bold]Source Files:[:] {source_files}
[:bold]Target Resource:[:] {policy_metadata['policy']['resource']}
[:bold]Account ID:[:] {policy_metadata['config']['account_id']}
[:bold]Region:[:] {policy_metadata['config']['region']}
[:bold]Resources Matched:[:] {len(resources)}
[:bold]Custodian Version:[:] {policy_metadata['version']}
## Output File Reference
- `custodian-run.log`: log messages and errors from the policy run
- `metadata.json`: the policy body and information about where it ran
- `resources.json`: a list of all resources that matched the policy
''')
return wrapper
DirSheet.help = c7n_help(DirSheet.help)
@saulpw
Copy link

saulpw commented Mar 1, 2023

I'd call this def help or at least add a @wraps(DirSheet.help).

@saulpw
Copy link

saulpw commented Mar 1, 2023

I think this is computed frequently, so the actual loading should probably be moved out of the actual wrapper function so it's only done once.

@ajkerrigan
Copy link
Author

I'd call this def help or at least add a @wraps(DirSheet.help).

Good call 👍

@ajkerrigan
Copy link
Author

I think this is computed frequently, so the actual loading should probably be moved out of the actual wrapper function so it's only done once.

Ah yeah I see that running a bunch. Caching on the property seems to address that and still report the expected dir-specific info. Would be even more noticeable for s3, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment