Skip to content

Commit

Permalink
feat: Shell UI for Todo Application
Browse files Browse the repository at this point in the history
  • Loading branch information
cy-yun committed Feb 20, 2025
1 parent 523d193 commit 18c505d
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
59 changes: 59 additions & 0 deletions issues/Charlotte Y - Noogler Project/shell/authenticate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
API_KEY="REDACTED"

# Assign arguments to variables
echo "Welcome to Todo Application. Would. you like to LOG IN (1) or SIGN UP (2)?"
read -p "Enter 1 or 2: " MODE
if [[ "$MODE" != "1" && "$MODE" != "2" ]]; then
echo "Invalid input. Please enter 1 or 2."
return
fi
read -p "Enter email: " EMAIL
read -s -p "Enter password: " PASSWORD
echo

if [ "$MODE" == "2" ]; then
# Perform the signup request
SIGNUP_RESPONSE=$(curl -s -X POST \
"https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=$API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\"
}")
# Check for errors in the response
if echo "$SIGNUP_RESPONSE" | grep -q "error"; then
echo "Error: Sign Up failed."
echo "Detailed error message: $(echo "$SIGNUP_RESPONSE" | jq -r '.error.message')"
return
fi
fi

# Perform the login request
RESPONSE=$(curl -s -X POST \
"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=$API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\",
\"returnSecureToken\": true
}")

# Check for errors in the response
if echo "$RESPONSE" | grep -q "error"; then
echo "Error: Log In failed."
echo "Detailed error message: $(echo "$RESPONSE" | jq -r '.error.message')"
return
fi

# Extract the ID token
ID_TOKEN=$(echo "$RESPONSE" | jq -r '.idToken')

# Check if jq returned an empty string
if [ -z "$ID_TOKEN" ]; then
echo "Error: Could not extract ID token from response."
echo "Raw response: $RESPONSE"
return
fi

export ID_TOKEN
echo 'ID Token retrieved. Log in successful.'
150 changes: 150 additions & 0 deletions issues/Charlotte Y - Noogler Project/shell/call.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash

# Script: call_functions.sh
# Description: Calls a gcloud function with operation and body, inserting an ID token.

# --- Input Validation ---

# Check for the correct number of arguments (at least 2: operation and body)
if [ $# -lt 1 ]; then
echo "Usage: $0 <operation>"
echo " <operation>: 'create-todo', 'update-todo', 'read-todos', or 'delete-todo'"
return
fi

# Check if ID_TOKEN environment variable is set
if [ -z "$ID_TOKEN" ]; then
echo "Error: Missing ID Token."
echo "Run: source authenticate.sh"
return
fi

# Assign arguments to variables
OPERATION="$1"
BODY="{\"idToken\": \"$ID_TOKEN\""

# Validate the OPERATION
case "$OPERATION" in
create-todo)
read -p "Enter title: " TITLE
if [ -n "$TITLE" ]; then
BODY+=", \"title\": \"$TITLE\""
else
echo "Error: Title is required"
return
fi

read -p "Enter description: " DESCRIPTION
if [ -n "$DESCRIPTION" ]; then
BODY+=", \"description\": \"$DESCRIPTION\""
else
echo "Error: Description is required"
return
fi

read -p "[Optional] Enter priority (1-5): " PRIORITY
if [ -n "$PRIORITY" ]; then
BODY+=", \"priority\": $PRIORITY"
fi

read -p "[Optional] Enter status (NotStarted, InProgress, Completed): " STATUS
if [ -n "$STATUS" ]; then
BODY+=", \"status\": \"$STATUS\""
fi
;;
read-todos)
read -p "[Optional] Enter task ID: " TASK_ID
if [ -n "$TASK_ID" ]; then
BODY+=", \"taskId\": \"$TASK_ID\""
fi

read -p "[Optional] Enter priority (1-5): " PRIORITY
if [ -n "$PRIORITY" ]; then
BODY+=", \"priority\": $PRIORITY"
fi

read -p "[Optional] Enter status (NotStarted, InProgress, Completed): " STATUS
if [ -n "$STATUS" ]; then
BODY+=", \"status\": \"$STATUS\""
fi
;;
update-todo)
read -p "Enter task ID: " TASK_ID
if [ -n "$TASK_ID" ]; then
BODY+=", \"taskId\": \"$TASK_ID\""
else
echo "Error: Task ID is required"
return
fi

read -p "[Optional] Enter new title: " TITLE
if [ -n "$TITLE" ]; then
BODY+=", \"title\": \"$TITLE\""
fi

read -p "[Optional] Enter new description: " DESCRIPTION
if [ -n "$DESCRIPTION" ]; then
BODY+=", \"description\": \"$DESCRIPTION\""
fi

read -p "[Optional] Enter new priority (1-5): " PRIORITY
if [ -n "$PRIORITY" ]; then
BODY+=", \"priority\": $PRIORITY"
fi

read -p "[Optional] Enter new status (NotStarted, InProgress, Completed): " STATUS
if [ -n "$STATUS" ]; then
BODY+=", \"status\": \"$STATUS\""
fi
;;
delete-todo)
read -p "Enter task ID: " TASK_ID
if [ -n "$TASK_ID" ]; then
BODY+=", \"taskId\": \"$TASK_ID\""
else
echo "Error: Task ID is required"
return
fi
;;
*)
echo "Error: Invalid operation '$OPERATION'"
echo "Valid operations are: create-todo, update-todo, read-todos, delete-todo"
return
;;
esac

BODY+="}"
echo

# Validate that BODY is a valid JSON object (basic check)
if ! echo "$BODY" | jq empty >/dev/null 2>&1; then
echo "Error: Invalid JSON body"
return
fi

# --- Construct gcloud command ---
REGION="us-west1"

GCLOUD_COMMAND=(
gcloud
functions
call
"$OPERATION"
--region="$REGION"
--data="$BODY"
)

# Execute the gcloud command
GCLOUD_RESPONSE=$("${GCLOUD_COMMAND[@]}")

# Check for errors
if [ $? -ne 0 ]; then
echo "Error: gcloud functions call failed"
echo "gcloud output: $GCLOUD_RESPONSE"
return
fi

# Print the gcloud command output
echo "$GCLOUD_RESPONSE"

return
4 changes: 4 additions & 0 deletions issues/Charlotte Y - Noogler Project/shell/log_out.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID_TOKEN=""
export ID_TOKEN
echo "Successfully logged out"
return

0 comments on commit 18c505d

Please sign in to comment.