SyncM8/syncm8

View on GitHub
bin/dev

Summary

Maintainability
Test Coverage
#!/bin/bash

DEVBOX_VERSION=$(cat DEVBOX_VERSION)

DOCKER_COMPOSE="export DEVBOX_VERSION=$DEVBOX_VERSION && docker-compose"

dockerComposeRun(){
    bin/util logAndRun "$DOCKER_COMPOSE run --rm -u $(id -u):$(id -g) --no-deps "$*
}

runDevboxCommand(){
    dockerComposeRun "dev "$*
}

# Run a devbox container as super user
runDevboxSudo(){
    bin/util logAndRun "$DOCKER_COMPOSE run --rm --no-deps dev "$*
}

# Start devbox/mongo containers
runDockerUp(){
    bin/util logAndRun "$DOCKER_COMPOSE up --detach"
}

# Stop docker-compose devbox and run one interactively for debugging
# Restart docker-compose devbox when done
runBackendStart(){
    runDockerUp
    bin/util logAndRun "$DOCKER_COMPOSE stop dev"
    dockerComposeRun "--service-ports dev"
    bin/util logAndRun "$DOCKER_COMPOSE start dev"
}

# Stop and remove all active containers associated with syncm8
runDockerDown(){
    bin/util logAndRun "$DOCKER_COMPOSE down"
}

# Show running docker containers associated with syncm8
runDockerPs(){
    bin/util logAndRun "$DOCKER_COMPOSE ps"
}

# Clean up unused docker stuff
# N.b. deletes all db data associated with syncm8 if mongo is not running
runDockerClean(){
    bin/util logAndRun "docker volume prune -f"
    bin/util logAndRun "docker image prune -f"
    bin/util logAndRun "docker container prune -f"
}

# Run frontend webpack and attach shell
runWebpack(){
    cd client
    npm run start
}

# Run frontend requirements
runClientRequirements(){
    cd client
    npm install
}

# Launch a devbox container for the purpose of installing python dependencies
runPyRequirements(){
    runDevboxCommand "pipenv install --dev"
}

# Run migration script
runMigrate(){
    runDevboxCommand "pipenv run python3 -m src.db.runner"
}

# Run js tests
# args: list of args to pass to npm run test
runJstest(){
    bin/util logAndRun "cd client && npm run test $*"
}

# Run precommit inside new devbox container
# args: list of args to pass to pre-commit
runPrecommit(){
    # if no cli args, run standard pre-push
    if [ ! $1 ]; then
        args="run --hook-stage push --from-ref origin/main --to-ref HEAD"
    else
        args=$*
    fi
    runDevboxCommand "pre-commit "$args
}

# One time setup to change git hooks path
runSetup(){
    bin/util logAndRun "git config core.hooksPath bin"
}

# Pull images from aws ECR
runPull(){
    bin/util logAndRun "bin/infra pull-env"
    bin/util logAndRun "bin/infra pull"
}

runLogs(){
    bin/util logAndRun "$DOCKER_COMPOSE logs "$*
}

# Print help message
printHelp(){
    echo "Usage:  bin/dev COMMAND [options]

A command runner for the syncm8 project.

Commands:
  attach      Attach shell to devbox container if running
  backend     Create containers if not up and attach dev container to shell to faciliatate pdb.
  client      Run frontend webpack and attach shell.
  client-req  Install dependencies for frontend.
  check       Run pre-commit inside dev container on committed files.
                All [options] are transparently passed.
  clean       Clean docker data. N.b will delete all db data if mongo is not running.
  down        Stop and remove all active containers associated with syncm8.
  frontend    Run frontend webpack and attach shell.
  jstest      Run js tests.
                All [options] are transparently passed.
  migrate     Run migration scripts for database.
  pre-commit  Alias for check.
  ps          Show running docker images associated with syncm8.
  pull        Pull images from aws.
  py-req      Install all python requirements.
  setup       One time setup to add all git hooks.
  webpack     Alias for frontend.

  run         Commands are transparently run in new devbox container. E.g.
                pytest
                pipenv install pkg
                sudo apt install pkg
                bash"
}

op=$1
restArgs="${@:2}"

case $op in
    attach)
        runBackendAttach
        ;;
    clean)
        runDockerClean
        ;;
    client-req)
        runClientRequirements
        ;;
    debug|backend)
        runBackendStart
        ;;
    down)
        runDockerDown
        ;;
    jstest)
        runJstest $restArgs
        ;;
    logs)
        runLogs $restArgs
        ;;
    migrate)
        runMigrate
        ;;
    pre-commit|check)
        runPrecommit $restArgs
        ;;
    ps)
        runDockerPs
        ;;
    pull)
        runPull
        ;;
    py-req)
        runPyRequirements
        ;;
    setup)
        runSetup
        ;;
    up)
        runDockerUp
        ;;
    webpack|frontend|client)
        runWebpack
        ;;
    sudo)
        runDevboxSudo $restArgs
        ;;
    run)
        runDevboxCommand $restArgs
        ;;
    *)
        printHelp
        ;;
esac