tinyscripts/creds_getter.sh

70 lines
1.5 KiB
Bash

function _scan_config
{
c="$HOME/.config/"
i=0 # funny wc -l
while read dirname; do
if [ -d "$c$dirname" -a -a "$c$dirname/accounts" ]; then
dirs[$i]="$dirname"
i=$(($i+1))
fi
done <<< $(ls "$c")
}
function _select_conf
{
for n in "${dirs[@]}"; do
if [ "$1" == "$n" ]; then conf="$HOME/.config/$1/accounts"; fi
done
if [ -z "$conf" ]; then # Still haven't picked one means the input wasn't a valid target
echo "no such conf in $1 (specifically $HOME/.config/$1/accounts)"
return 1
fi
}
function creds
{
local conf OPTIND # Specifically putting these on same line so I can have funny number of lines.
while getopts 'hpf:' o; do case "${o}" in
h) echo 'Usage: creds [-p] [-f conf file] [~/.config file] [account]'; return;;
p) print=set; break;;
f) conf=$OPTARG; break;;
esac done
shift $((OPTIND-1))
# Selecting config file
if [ -z "$conf" ]; then
declare -a dirs
_scan_config
# Displaying config files available
if [ -z "$1" ]; then
for n in "${dirs[@]}"; do echo $n; done
return
fi
_select_conf $1 || return
shift
fi
# Selecting account
# Displaying accounts is mashed up among it
local flag
while read lin; do
if [ -z $flag ]; then
if [ -z $1 ]; then
echo $lin
else
read headers <<< "$lin"
fi
else
read -a lin <<< "$lin"
if [ -z $1 ]; then
echo "${lin[0]}"
elif [ "$1" == "${lin[0]}" ]; then
if [ -z $print ]
then read $headers <<< ${lin[*]}
else echo ${lin[*]}
fi
fi
fi
flag=set
done < "$conf"
}