#!/bin/bash set -e if [[ -a ".env" ]]; then source .env fi function help() { echo echo "task [options]" echo echo "commands:" echo # Define column widths CMD_WIDTH=20 OPT_WIDTH=14 DESC_WIDTH=36 COLUMN="| %-${CMD_WIDTH}s | %-${OPT_WIDTH}s | %-${DESC_WIDTH}s |\n" # Print table header printf "$COLUMN" "Command" "Option" "Description" echo "|$(printf '%*s' $((CMD_WIDTH + 2)) '' | tr ' ' '-')|$(printf '%*s' $((OPT_WIDTH + 2)) '' | tr ' ' '-')|$(printf '%*s' $((DESC_WIDTH + 2)) '' | tr ' ' '-')|" # Print table rows printf "$COLUMN" "version" "" "Show version of required tools." printf "$COLUMN" "install" "" "Install node dependencies." printf "$COLUMN" "dev-vitepress" "" "Start VitePress dev server." printf "$COLUMN" "build-vitepress" "" "Build site with VitePress." printf "$COLUMN" "preview-vitepress" "" "Server VitePress build." printf "$COLUMN" "start-kind" "" "Start kind Kubernetes cluster." printf "$COLUMN" "delete-kind" "" "Delete kind Kubernetes cluster." printf "$COLUMN" "start-minikube" "" "Start minikube Kubernetes cluster." printf "$COLUMN" "install-chart" "[name]" "Install Helm Charts from local source." printf "$COLUMN" "uninstall-chart" "[name]" "Uninstall Helm Charts from local source." printf "$COLUMN" "forward-odoo" "" "Forward odoo port." printf "$COLUMN" "test-k8s" "[clean]" "Test Kubernetes environment." printf "$COLUMN" "logs" "" "Show pod logs." printf "$COLUMN" "list" "" "List Helm deployments." printf "$COLUMN" "list-pods" "" "List all pods." printf "$COLUMN" "package-repo" "" "Package Helm Charts." printf "$COLUMN" "index-repo" "" "Index Helm Chart." printf "$COLUMN" "publish-repo" "" "Copy Helm Chart files to dist." printf "$COLUMN" "add-repo" "" "Add the kubernetes-buld repo." printf "$COLUMN" "remove-repo" "" "Remove the kubernetes-buld repo." printf "$COLUMN" "install-repo" "[name]" "Install Chart from kubernetes-build repo." printf "$COLUMN" "update-with-llm" "[path][prompt]" "Update files with LLM prompt." echo } # Env Commands function version() { minikube version kind version helm version } function install() { pnpm install } # Docs Commands function dev-vitepress() { pnpm run docs:dev } function build-vitepress() { pnpm run docs:build } function preview-vitepress() { pnpm run docs:preview } # Kubernetes Commands function start-kind() { echo "Start kind." kind create cluster } function delete-kind() { echo "Delete kind." kind delete cluster } function start-minikube() { echo "Start minikube." minikube start } function install-chart() { if test -z "$1"; then echo "\$1 is empty." exit 1; fi echo "Install release $1 with helm." helm install "$1" "$1" POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "$1") echo -e "\nWait for $POD_ID pods to be running:" kubectl wait --for=condition=Ready "$POD_ID" --timeout=120s } function uninstall-chart() { if test -z "$1"; then echo "\$1 is empty." exit 1; fi echo "Uninstall release $1 with helm." helm uninstall "$1" } function test-k8s() { if [ "$1" = "clean" ]; then echo "Cleanup environment" delete-kind else start-kind install-chart postgres install-chart odoo fi } function forward-odoo() { echo "Open http://localhost:8069 url in your browser." kubectl port-forward service/odoo 8069:8069 } # Kubectl Commands function logs() { POD_ID=$(kubectl get pods -o=name --all-namespaces | grep "odoo") kubectl logs "$POD_ID" } function list-pods() { kubectl get pods } # Helm Commands function list() { helm list } function package-repo() { echo "Package Helm Charts." helm package postgres helm package odoo } function index-repo() { echo "Update index.yaml" helm repo index . } function publish-repo() { build-vitepress cp *.tgz .vitepress/dist cp index.yaml .vitepress/dist } function add-repo(){ echo "Add the kubernetes-build repo." helm repo add kubernetes-build https://kubernetes.build } function remove-repo(){ echo "Remove the kubernetes-build repo." helm repo remove kubernetes-build } function install-repo(){ echo "Install release odoo from kubernetes-build repo." helm install odoo kubernetes-build/odoo } # LLM Commands function update-with-llm() { if test -z "$1"; then echo "\$1 is empty. Usage:"; help | grep "${FUNCNAME[0]}" exit 1; fi # Get files from path if [ -f "$1" ]; then FILES="$1" elif [ -d "$1" ]; then FILES=$(find "$1" -type f \( -name "*.md" -o -name "*.yml" -o -name "*.yaml" -o -name "*.conf" -o -name "*.nginx" \) \ | grep -vFf <(git -C "$1" ls-files --ignored --exclude-standard --others)) else echo "$1 is neither a file nor a directory." exit 1 fi echo -e "Loaded these files into prompt:\n\n$FILES\n" # Prompt task description if [ -z "$2" ]; then read -p "Enter the task description: " TASK_DESCRIPTION else TASK_DESCRIPTION="$2" fi # Prepare the files content for prompt FILE_CONTENTS="" for FILE in $FILES; do FILE_CONTENTS+="<<<$FILE>>> $(cat "$FILE") " done # Define prompt content mkdir -p tmp PROMPT_FILE="tmp/llm_update" echo -e "\nWrite prompt to $PROMPT_FILE." cat << EOF > "$PROMPT_FILE" Look at the code files below and do the following: $TASK_DESCRIPTION Output all files that you need to change in full again, including your changes. In the same format as I provide the files below. Under no circumstances output any other text, no additional infos, no code formatting chars. Only the code in the given format. Here are the files: $FILE_CONTENTS EOF # Run the llm command echo -e "Send prompt and wait for the response of the $LLM_MODEL LLM." RESULT=$(cat "$PROMPT_FILE" | llm -m "$LLM_MODEL") # Check if result is empty if [ -z "$RESULT" ]; then echo "No response from the model. Exiting." exit 1 fi # Save the result to a file RESULT_FILE="tmp/llm_update_result" echo "$RESULT" > "$RESULT_FILE" echo -e "Saved response to $RESULT_FILE.\n" # Show a preview of result file less "$RESULT_FILE" # Ask for confirmation before updating files read -p "Do you want to apply these updates to the files? (y/n): " CONFIRM if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then exit 0 fi # Parse the response from the file and update the files directly echo -e "Parsing the response and updating files...\n" CURRENT_FILE="" while IFS= read -r LINE; do if [[ $LINE =~ ^"<<<"(.*)">>>" ]]; then CURRENT_FILE="${BASH_REMATCH[1]}" echo "Update file $CURRENT_FILE." > "$CURRENT_FILE" elif [[ -n $CURRENT_FILE ]]; then echo "$LINE" >> "$CURRENT_FILE" fi done < "$RESULT_FILE" } if declare -f "$1" > /dev/null; then "$1" "${@:2}" else case "$1" in *) echo "Unknown command: $1" help exit 1 ;; esac fi