add_workspace now checks if there is a WS for pwd and asks if to override

This commit is contained in:
Sagi Dayan 2022-01-23 12:28:42 +02:00
parent fe3b4dd7ee
commit cbe5e8442f
Signed by: sagi
GPG key ID: FAB96BFC63B46458

View file

@ -2,9 +2,32 @@
JSON_PATH=~/.shellconfig/workspaces/workspaces.json
function yes_no_question() {
QUESTION=$1
RESPONSE=$(cat <<EOF | fzf --reverse --header "${QUESTION}"
No
Yes
EOF
)
echo $RESPONSE
}
NAME=${1:-}
# check if folder already saved as a workspace
EXISTING_WSP=$(cat $JSON_PATH | jq -r --arg pwd $(pwd) 'to_entries[] | select(.value.dir == $pwd)')
if [ ! -z "$EXISTING_WSP" ]; then
WSP_NAME=$(echo "$EXISTING_WSP" | jq -r --arg pwd $(pwd) '.key')
RESPONSE=$(yes_no_question "⚠️ There is already a workspace named '${WSP_NAME}' for this folder. Would you like to override?")
if [[ "$RESPONSE" != "Yes" ]]; then
echo Canceled
exit
fi
NAME=$WSP_NAME
fi
if [ -z "$NAME" ]; then
read -p "🖥 Please give this workspace a name: " NAME
fi
@ -15,12 +38,8 @@ if [ -z "$NAME" ]; then
fi
# Check if workspace already exists
if [ $(cat $JSON_PATH | jq 'keys' | grep "${NAME}") ]; then
OVERRIDE=$(cat <<EOF | fzf --reverse --header "a Worspace with name '${NAME}' already exists. Override?"
No
Yes
EOF
)
if [ -z "$EXISTING_WSP" ] && [ $(cat $JSON_PATH | jq 'keys' | grep "${NAME}") ]; then
OVERRIDE=$(yes_no_question "a Worspace with name '${NAME}' already exists. Override?")
if [[ "$OVERRIDE" == "No" ]]; then
echo Canceled
exit