bash-my-aws / bash-my-aws

Bash-my-AWS provides simple but powerful CLI commands for managing AWS resources

Home Page:https://bash-my-aws.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expansion of jmespath fields via an env variable

nitrocode opened this issue · comments

Similar to #306 where I would like to expand the fields outputted, it would be nice if there was an easy method to pass in additional json keys to every function.

For example...

✗ bma type instances
instances is a function
instances ()
{
    local instance_ids=$(skim-stdin);
    local filters=$(__bma_read_filters $@);
    aws ec2 describe-instances ${instance_ids/#/'--instance-ids '} --output text --query "
      Reservations[].Instances[][
        InstanceId,
        ImageId,
        InstanceType,
        State.Name,
        [Tags[?Key=='Name'].Value][0][0],
        LaunchTime,
        Placement.AvailabilityZone,
        VpcId $_bma_instances_expand_fields
      ]" | grep -E -- "$filters" | LC_ALL=C sort -b -k 6 | column -s'	' -t
}
$ _bma_instances_expand_fields=",NetworkInterfaces[0].PrivateIpAddresses[0].Association.PublicDnsName" \
  instances ec2-snip.us-east-1.compute.amazonaws.com

i-snip  ami-snip  c3.xlarge  running  None  2021-01-20T22:38:20+00:00  us-east-1a  vpc-snip  ec2-snip.us-east-1.compute.amazonaws.com

@mbailey what do you think about using an env variable that follow this style $_bma_{func}_expand_fields e.g. $_bma_instances_expand_fields to expand the query fields ?

Some years ago I envisaged getting the full field list from an ENV VAR but decided against it because some commands might
use columns from others and it could break that.

By appending to the end of the output, your solution is clean and won't break anything.

As maintainer, I have the columns how I like them but I like the idea of others being able to add extra columns easily.