124 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -e
_activate_venv() {
. .venv/bin/activate
}
_upgrade_deps() {
_activate_venv
echo $(cat requirements.txt | sed 's/==.*//g') | xargs pip install -U
}
UPGRADE_DEPS_match=upgrade-deps
_venv() {
case "$1" in
"") _skip=false ;;
--skip-install)
_skip=true
shift
;;
*) _skip=false ;;
esac
_py_version=${1:-3.12.7}
if ! $_skip; then
pyenv install $_py_version
fi
pyenv local $_py_version
if [ -d .venv ]; then
rm -rf .venv
fi
python -m venv ".venv"
}
VENV_match=venv
_init() {
if [ -d .venv ]; then
_activate_venv
pip install --upgrade pip
pip install -r requirements.txt
else
echo "Please run 'bin/configure venv' first"
fi
}
INIT_match=init
_freeze() {
if [ -d .venv ]; then
_activate_venv
mv requirements.txt requirements.txt.bak
pip freeze >requirements.txt
else
echo "Please run 'bin/configure init' first"
fi
}
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 | venv | init | start | freeze]"
echo -e "help, -h:\n\tShow this help message"
echo -e "venv \${python_version}:\n\tCreate virtual environment with \${python_version}"
echo -e "init [--skip-install] \${python_version}:\n\tInitialize virtual environment with \${python_version} and install dependencies"
echo -e "start \${env(dev|prod):-dev} \${port:-8001}\n\t\${host:-0.0.0.0}: Start the application"
echo -e "freeze:\n\tFreeze dependencies"
}
_check_cmds() {
if ! command -v pyenv >/dev/null; then
echo "Please install pyenv"
_fail=true
fi
}
_main() {
export _fail=false
_check_cmds
if $_fail; then
exit 1
fi
_cmd="${1:-help}"
if ! [[ -z "${1+x}" ]]; then
shift
fi
case "$_cmd" in
help | -h) _help ;;
"$START_match") _start "$@" ;;
"$VENV_match") _venv "$@" ;;
"$INIT_match")
_venv "$@"
_init
;;
"$FREEZE_match") _freeze ;;
"$UPGRADE_DEPS_match") _upgrade_deps ;;
--*) ;;
esac
}
_main "$@"