1#!/usr/bin/env bash
2
3set -e
4
5listFile=shell_test_list.json
6
7case $1 in
8	"store")
9		in=$(</dev/stdin)
10		server=$(echo "$in" | jq --raw-output ".ServerURL")
11		serverHash=$(echo "$server" | sha1sum - | awk '{print $1}')
12
13		username=$(echo "$in" | jq --raw-output ".Username")
14		password=$(echo "$in" | jq --raw-output ".Secret")
15		echo "{ \"Username\": \"${username}\", \"Secret\": \"${password}\" }" > $TEMP/$serverHash
16		# add the server to the list file
17		if [[ ! -f $TEMP/$listFile ]]; then
18			echo "{ \"${server}\": \"${username}\" }" > $TEMP/$listFile
19		else
20			list=$(<$TEMP/$listFile)
21			echo "$list" | jq ". + {\"${server}\": \"${username}\"}" > $TEMP/$listFile
22		fi
23		;;
24	"get")
25		in=$(</dev/stdin)
26		serverHash=$(echo "$in" | sha1sum - | awk '{print $1}')
27		if [[ ! -f $TEMP/$serverHash ]]; then
28			echo "credentials not found in native keychain"
29			exit 1
30		fi
31		payload=$(<$TEMP/$serverHash)
32		echo "$payload"
33		;;
34	"erase")
35		in=$(</dev/stdin)
36		serverHash=$(echo "$in" | sha1sum - | awk '{print $1}')
37		rm -f $TEMP/$serverHash
38
39		# Remove the server from the list
40		list=$(<$TEMP/$listFile)
41		echo "$list" | jq "del(.[\"${in}\"])" > $TEMP/$listFile
42		;;
43	"list")
44		if [[ ! -f $TEMP/$listFile ]]; then
45			echo "{}"
46		else
47			payload=$(<$TEMP/$listFile)
48			echo "$payload"
49		fi
50		;;
51	*)
52		echo "unknown credential option"
53		exit 1
54		;;
55esac
56