From 129802f290b4daa5a6abc03fc48df74db0877891 Mon Sep 17 00:00:00 2001 From: Mark Bailey Date: Sat, 26 Oct 2024 20:40:32 -0400 Subject: [PATCH] refactor: add start command --- bin/configure | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/bin/configure b/bin/configure index a98ae8e..9cae53f 100755 --- a/bin/configure +++ b/bin/configure @@ -1,6 +1,10 @@ #!/bin/sh set -euC +_activate_venv() { + . .venv/bin/activate +} + _venv() { _py_version=${1:-3.12.0} @@ -17,7 +21,7 @@ VENV_match=venv _init() { if [ -d .venv ]; then - source .venv/bin/activate + _activate_venv pip install --upgrade pip pip install -r requirements.txt else @@ -28,7 +32,7 @@ INIT_match=init _freeze() { if [ -d .venv ]; then - source .venv/bin/activate + _activate_venv mv requirements.txt requirements.txt.bak pip freeze >requirements.txt else @@ -37,11 +41,32 @@ _freeze() { } FREEZE_match=freeze +_start() { + _env=${1:-dev} + _port=${2:-8001} + _host=${3:-0.0.0.0} + + case "$_env" in + dev) ;; + prod) _env=run ;; + *) + echo "Invalid environment: $_env" + exit 1 + ;; + esac + + _activate_venv + + fastapi $_env src/main.py --host $_host --port $_port +} +START_match=start + _help() { - echo -e "Usage: $0 [help, -h | init {python_version} | venv {python_version} | freeze]\n" + echo -e "Usage: $0 [help, -h | venv | init | start | freeze]\n" echo " help, -h: Show this help message" - echo " init: Initialize virtual environment with {python_version} and install dependencies" - echo " venv: Create virtual environment with {python_version}" + echo " venv \${python_version}: Create virtual environment with \${python_version}" + echo " init \${python_version}: Initialize virtual environment with \${python_version} and install dependencies" + echo " start \${env(dev|prod):-dev} \${port:-8001} \${host:-0.0.0.0}: Start the application" echo " freeze: Freeze dependencies" } @@ -68,10 +93,12 @@ _main() { case "$_cmd" in --*) ;; help | -h) _help ;; + "$START_match") _start "$@" ;; "$VENV_match") _venv "$@" ;; "$INIT_match") _venv "$@" - _init + shift + _init "$@" ;; "$FREEZE_match") _freeze ;; esac