Add default paths. Shortern full path.

This commit is contained in:
John Internet 2020-01-02 22:38:02 +00:00
parent fa30c8125a
commit 73618b6bfb
2 changed files with 11 additions and 16 deletions

View file

@ -1,17 +1,12 @@
#!/bin/sh #!/bin/sh
STORE_PATH=/tmp FOLDER=${1:-default}
if [ ! -z "$1" ]; then STORE_PATH="/tmp/ssh-agent/$FOLDER"
# This allows us to work with things like gitlab.
# where the same thing might be running concurrently.
# If default, it only stores in /tmp/, but if a path is added, it's interpolated.
STORE_PATH="$STORE_PATH/$1"
mkdir -p "$STORE_PATH" mkdir -p "$STORE_PATH"
fi
# Start the SSH agent if it isn't already. # Start the SSH agent if it isn't already.
if [ -z "$SSH_AGENT_PID" ]; then if [ -z "$SSH_AGENT_PID" ]; then
eval "$(ssh-agent)" > /dev/null eval "$(ssh-agent)" > /dev/null
echo "$SSH_AGENT_PID" > "$STORE_PATH"/ssh-agent-id echo "$SSH_AGENT_PID" > "$STORE_PATH"/id
echo "$SSH_AUTH_SOCK" > "$STORE_PATH"/ssh-auth-sock echo "$SSH_AUTH_SOCK" > "$STORE_PATH"/sock
fi fi

View file

@ -6,10 +6,8 @@ if [ ! -z "$SSH_AGENT_PID" ]; then
exit $? exit $?
else else
# The env isn't set, construct the file path. # The env isn't set, construct the file path.
STORE_PATH=/tmp FOLDER=${1:-default}
if [ ! -z "$1" ]; then STORE_PATH="/tmp/ssh-agent/$FOLDER"
STORE_PATH="$STORE_PATH/$1"
fi
if [ ! -d "$STORE_PATH" ]; then if [ ! -d "$STORE_PATH" ]; then
echo "Store Path $STORE_PATH doesn't exist!" >&2 echo "Store Path $STORE_PATH doesn't exist!" >&2
exit 1 exit 1
@ -18,14 +16,16 @@ else
# And check our files exist. # And check our files exist.
if [ -f "$STORE_PATH/ssh-agent-id" ]; then if [ -f "$STORE_PATH/ssh-agent-id" ]; then
# Grab our PID and socket. # Grab our PID and socket.
SSH_AGENT_PID=$(cat "$STORE_PATH/ssh-agent-id") SSH_AGENT_PID=$(cat "$STORE_PATH/id")
export SSH_AGENT_PID export SSH_AGENT_PID
rm "$STORE_PATH/ssh-agent-id" rm "$STORE_PATH/ssh-agent-id"
SSH_AUTH_SOCK=$(cat "$STORE_PATH/ssh-auth-sock") SSH_AUTH_SOCK=$(cat "$STORE_PATH/sock")
export SSH_AUTH_SOCK export SSH_AUTH_SOCK
rm "$STORE_PATH/ssh-auth-sock" rm "$STORE_PATH/ssh-auth-sock"
rmdir "$STORE_PATH"
eval $(ssh-agent -k) >/dev/null eval $(ssh-agent -k) >/dev/null
exit $? exit $?
else else