Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

4.0 KB
Raw Blame
#! /usr/bin/env bash

set -e

# https://stackoverflow.com/a/1482133
OPENVERSE_PROJECT="$(dirname "$(readlink -f -- "$0")")"
export OPENVERSE_PROJECT

_self="$OPENVERSE_PROJECT/ov"

_cmd="$1"

dev_env="$OPENVERSE_PROJECT"/docker/dev_env
export DEV_ENV="$dev_env"

if [[ $_cmd == "help" || $_cmd == "-h" || $_cmd == "--help" || -z $_cmd ]]; then
  cat <<-'EOF'
Openverse development toolkit

USAGE
  ov <subcommand> [...ARGS]

COMMANDS
  ov init
    Initialise the Openverse development toolkit for the first time
    Alias for:

        ov build && ov setup-env && ov just install-hooks init-ov-profile install

  ov build [...ARGS]
    Build the Openverse development toolkit Docker image.
    ARGS are passed to `docker build`. See `docker build --help` for options.

  ov setup-env
    Setup the environment inside the container. `ov init` automatically runs this,
    and in most cases you should not need to run this manually. However, you may
    occasionally need to run it manually if tool requirements inside the container
    change. This can also be a helpful debugging step if tools in the container are
    not working as expected.

  ov stop
    Stop the persistent `ov` Docker container.

  ov doctor
    Try some basic automated troubleshooting steps for common issues.

  ov clean
    Remove the Openverse development toolkit Docker container, image and volume.
    Use in conjunction with `ov init` to recreate the environment from
    scratch:

        ov clean && ov init

    Warning: running `ov clean` will result in needing to fully rebuild the environment
    including downloading tools handled by `ov setup-env` and prek environments.

  ov hook HOOK
    Run Git hooks through prek inside the development toolkit container.

  ov aliases [--help|-h]
    Display information about ov aliases. By default, lists all available aliases.
    Passing --help will print documentation about how to use and configure aliases.

  ov COMMAND [...ARGS]
    Run COMMAND inside the development toolkit container. COMMAND will be processed
    through ov aliases (see `ov aliases --help` for details). ov is aware of your
    current working directory and will execute the command relative to your current directory
    inside the repository.

    Tip: The toolkit comes loaded with many tools for working with Openverse! Try
    some of the following:

      - ov just
      - ov uv
      - ov pnpm
      - ov python
      - ov bash
      - ov jq
      - ov http
EOF

  exit 0
fi

case "$_cmd" in
init)
  "$_self" build
  "$_self" setup-env
  "$_self" just install-hooks
  "$_self" just init-ov-profile
  "$_self" just install
  ;;

build)
  # k6 does not install on aarch64, so we must specify the platform on macOS.
  docker build "${@:2}" --tag openverse-dev_env:latest "$dev_env"
  ;;

setup-env)
  "$_self" sudo bash "$dev_env"/setup_env.sh
  ;;

stop)
  docker stop openverse-dev_env
  ;;

doctor)
  # Expand these checks/fixes as needed
  # Log a message for each fix so it's clear what is happening

  printf "🩺 The Openverse Doctor is here to help... I will try to apply some basic fixes that might help.\n"

  case "$OSTYPE" in
  linux*)
    # For some reason permissions can get mixed up in `/opt` and uv will fail to create venvs
    # This is only relevant to Linux hosts because macOS hosts run the container as root regardless
    printf "   - Fixing file permissions in container's /opt\n"
    "$_self" sudo chmod -R 0777 /opt
    ;;

  esac
  ;;

clean)
  # Using `|| true` allows these commands to be skipped if the
  # container, image or volume does not exist.
  docker container stop openverse-dev_env || true
  docker container rm openverse-dev_env || true
  docker image rm openverse-dev_env || true
  docker volume rm openverse_dev-env || true
  rm -rf "$dev_env"/.cache
  ;;

hook)
  # Arguments match the implementation of hooks installed by prek
  "$_self" prek hook-impl \
    --config=.pre-commit-config.yaml \
    --hook-type="$2" \
    --hook-dir "$OPENVERSE_PROJECT"/.git/hooks \
    --color=always \
    -- "${@:3}"
  ;;

*)
  "$dev_env"/run.sh "$@"
  ;;

esac