Skip to content

Commit

Permalink
Enable short options
Browse files Browse the repository at this point in the history
- but not added to Usage or README

Add unofficial strict mode

shellcheck linting continued
- quoting in `cfg_parser`
- if cmd intead of `$?` checking
  • Loading branch information
virgilwashere committed Jul 2, 2019
1 parent e37535b commit b7de2c3
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions get-aws-profile.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#!/bin/bash -f
#!/usr/bin/env bash
# -*- coding: utf-8 -*-

# "unofficial" bash strict mode
# See: http://redsymbol.net/articles/unofficial-bash-strict-mode
set -o errexit # Exit when simple command fails 'set -e'
set -o errtrace # Exit on error inside any functions or subshells.
set -o nounset # Trigger error when expanding unset variables 'set -u'
set -o pipefail # Do not hide errors within pipes 'set -o pipefail'
IFS=$'\n\t'

#
# Fetch the AWS access key and/or secret for an AWS profile
Expand All @@ -17,24 +26,27 @@
# http://pastebin.com/m4fe6bdaf (supports spaces in values)
#

# shellcheck disable=SC2206
cfg_parser ()
{
IFS=$'\n' && ini=( $(<$1) ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments ;
ini=( ${ini[*]//\#*/} ) # remove comments #
ini=( ${ini[*]/\ =/=} ) # remove tabs before =
ini=( ${ini[*]/=\ /=} ) # remove tabs be =
ini=( ${ini[*]/\ *=\ /=} ) # remove anything with a space around =
# IFS=$'\n' && ini=( $(<$1) ) # convert to line-array
mapfile ini <"${1:?Missing INI filename}"
ini=( ${ini[*]//;*/} ) # remove comments ;
ini=( ${ini[*]//\#*/} ) # remove comments #
ini=( ${ini[*]/\ =/=} ) # remove tabs before =
ini=( ${ini[*]/=\ /=} ) # remove tabs be =
ini=( ${ini[*]/\ *=\ /=} ) # remove anything with a space around =
ini=( ${ini[*]/#[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/=/=\( } ) # convert item to array
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
eval "$(echo "${ini[*]}")" # eval the result
ini=( ${ini[*]/%]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/=/=\( } ) # convert item to array
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
# shellcheck disable=SC2116
eval "$(echo "${ini[*]}")" # eval the result
}

# echo a message to standard error (used for messages not intended
Expand Down Expand Up @@ -67,31 +79,31 @@ display_usage ()
for i in "$@"
do
case $i in
--credentials=*)
-c=*|-f=*|--credentials=*)
CREDENTIALS="${i#*=}"
shift # past argument=value
;;
--profile=*)
-p=*|--profile=*)
PROFILE="${i#*=}"
shift # past argument=value
;;
--key)
-k|--key)
SHOW_KEY=true
shift # past argument with no value
;;
--secret)
-s|--secret)
SHOW_SECRET=true
shift # past argument with no value
;;
--session-token)
-t|--session-token)
SHOW_SESSION_TOKEN=true
shift # past argument with no value
;;
--help)
-h*|--h*)
display_usage
exit 0
;;
*)
*)
# unknown option
echo_stderr "Unknown option $1"
display_usage
Expand Down Expand Up @@ -125,18 +137,17 @@ if [[ ! -r "${CREDENTIALS}" ]]; then
exit 3
fi

cfg_parser "${CREDENTIALS}"
if [[ $? -ne 0 ]]; then
if ! cfg_parser "${CREDENTIALS}"; then
echo_stderr "Parsing credentials file '${CREDENTIALS}' failed"
exit 4
fi

cfg.section.${PROFILE}
if [[ $? -ne 0 ]]; then
if ! cfg.section."${PROFILE}" >/dev/null; then
echo_stderr "Profile '${PROFILE}' not found"
exit 5
fi

# shellcheck disable=SC2154
if [[ "${SHOW_KEY}" = false && "${SHOW_SECRET}" = false && "${SHOW_SESSION_TOKEN}" = false ]]; then
echo_stderr "# Profile: ${PROFILE}"
printf 'export AWS_ACCESS_KEY_ID=%s\n' "${aws_access_key_id}"
Expand Down

0 comments on commit b7de2c3

Please sign in to comment.