123 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
set -euC
_activate_venv() {
. .venv/bin/activate
}
_venv() {
_py_version=${1:-3.12.0}
case "$1" in
--skip)
_skip=true
shift
;;
*) _skip=false ;;
esac
if ! $_skip; then
pyenv install $_py_version
#init pyenv
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
pyenv shell $_py_version
fi
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]\n"
echo " help, -h: Show this help message"
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"
}
_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 "$@" ]; then
shift
fi
case "$_cmd" in
help | -h) _help ;;
"$START_match") _start "$@" ;;
"$VENV_match") _venv "$@" ;;
"$INIT_match")
_venv "$@"
_init
;;
"$FREEZE_match") _freeze ;;
--*) ;;
esac
}
_main "$@"