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

instance-ssh() tweaks

rawiriblundell opened this issue · comments

Hi,
As I'm not 100% clear on the expected coding style and/or contribution guidelines here, I'm going to raise this as an issue rather than a PR and see what gets accepted/influenced/rejected.

First, a tiny critique: there's a number of ShellCheck SC2155's. I see that this has been ad-hoc disabled globally in at least one other lib file, which seems to me to be a sub-optimal choice. FWIW my take on this rule is to simply use the "everybody's happy" option of just declaring your local vars once on a single line and moving on.

With respect specifically to instance-ssh(), this:

    local instance_details="$(instance-ssh-details $instance_id)"
    local instance_id=$(echo $instance_details | awk '{print $1}')
    local keyname=$(echo $instance_details | awk '{print $2}')
    local private_ip=$(echo $instance_details | awk '{print $3}')
    local instance_name=$(echo $instance_details | awk '{print $4}')
    local instance_default_user=$(echo $instance_details | awk '{print $5}')

... could potentially be expressed more efficiently like so:

local instance_id keyname private_ip instance_name instance_default_user
read -r instance_id keyname private_ip instance_name instance_default_user < <(instance-ssh-details "$instance_id")

Or perhaps something like this:

local instance_id keyname private_ip instance_name instance_default_user
set -- $(instance-ssh-details "$instance_id")
instance_id="$1"
keyname="$2"
... etc

The other tweak I'd like to suggest is changing this:

-i "${BMA_SSH_DIR:-~/.ssh/}$keyname"

to

-i "${BMA_SSH_DIR:-~/.ssh}/$keyname"

The rationale is that if BMA_SSH_DIR is missing a trailing slash, this will ensure that one is present. If BMA_SSH_DIR does have a trailing slash, then a non-fatal double-slash will be present (i.e. /path/to/BMA_SSH_DIR//keyname) and the connection will still work. Either way, the default behaviour of ~/.ssh/$keyname remains intact.

With the current code, a BMA_SSH_DIR without a trailing slash results in e.g. /path/to/BMA_SSH_DIRkeyname which will obviously fail.

Cheers!

Thanks Rawiri.

local instance_id keyname private_ip instance_name instance_default_user
read -r instance_id keyname private_ip instance_name instance_default_user < <(instance-ssh-details "$instance_id")

It works fine as it is but your suggestion makes it a lot shorter and simpler. Can you think of anything that could go wrong?

<philosophical digression>
I started using shellcheck a few years ago and sunk some hours into resolving things it flagged. Most functions assign STDIN+args to a variable which should be local. At the time I decided the extra line to declare the variable local was not worth the space.

I recently started using vscode (I've always been vim in the terminal) and the linter has been decorating my markdown with complaints about formatting. I've found myself complying by making changes just to shut it up. On the one hand I think it's dangerous to act on advice without considering the merits on each occasion - but on the other I think it's generally a good thing for code bases to follow standards that are programmatically verifiable.
</philosophical digression>

-i "${BMA_SSH_DIR:-~/.ssh}/$keyname"

This tweak is good for user experience. To be honest I think BMA_SSH_DIR shouldn't need a trailing slash. That's not what I'd expect to have to include.

Thanks again. I care about BMA working and also being simple, shallow, readable code.

I'd be happy to review PRs for both of those.

thanks,

Mike