|
| 1 | +#!/usr/bin/env bash |
| 2 | +args=("$@") |
| 3 | +plugin_name=${args[0]} |
| 4 | + |
| 5 | +YELLOW='\033[33m' |
| 6 | +RESET='\033[0m' # No Color |
| 7 | + |
| 8 | +if [ -z "$plugin_name" ]; then |
| 9 | + echo "Usage: ./plugin-env <plugin_name>" |
| 10 | + echo " <plugin_name> is the name of the dd-trace plugin to enter the dev environment for." |
| 11 | + echo "" |
| 12 | + echo " It can be one of the following:" |
| 13 | + node - << EOF |
| 14 | + const fs=require('fs'); |
| 15 | + const yaml = require('yaml'); |
| 16 | + const pluginsData = fs.readFileSync('.github/workflows/plugins.yml', 'utf8'); |
| 17 | + const env=Object.keys(yaml.parse(pluginsData).jobs); |
| 18 | + console.log(...env); |
| 19 | +EOF |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +if ! hash node 2>/dev/null; then |
| 24 | + echo "Node.js is not installed. Please install Node.js before running this script." |
| 25 | + echo "You can use nvm to install Node.js. See https://nvm.sh for more information." |
| 26 | + echo "For best results, use the latest version of Node.js." |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +if ! hash yarn 2>/dev/null; then |
| 31 | + echo "[email protected] is not installed. Please install [email protected] before running this script." |
| 32 | + echo "You can install yarn by running 'npm install -g yarn'." |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +read -r PLUGINS SERVICES <<<$(node - << EOF |
| 37 | +const fs=require('fs'); |
| 38 | +const yaml = require('yaml'); |
| 39 | +const pluginsData = fs.readFileSync('.github/workflows/plugins.yml', 'utf8'); |
| 40 | +const { PLUGINS, SERVICES } = yaml.parse(pluginsData).jobs['$plugin_name'].env; |
| 41 | +console.log(PLUGINS || '', SERVICES || '') |
| 42 | +EOF |
| 43 | +) |
| 44 | + |
| 45 | +export PLUGINS |
| 46 | +export SERVICES |
| 47 | + |
| 48 | +if [ -z "$SERVICES" ]; then |
| 49 | + echo "The plugin '$plugin_name' does not have any services defined. Nothing to do here." |
| 50 | +else |
| 51 | + if ! hash docker 2>/dev/null; then |
| 52 | + echo "Docker is not installed. Please install Docker before running this script." |
| 53 | + echo "You can install Docker by following the instructions at https://docs.docker.com/get-docker/." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + if (! docker stats --no-stream >/dev/null); then |
| 57 | + echo "The docker daemon is not running. Please start Docker before running this script." |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + if [ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q $SERVICES)` ]; then |
| 61 | + teardown=1 |
| 62 | + docker compose up -d $SERVICES |
| 63 | + fi |
| 64 | +fi |
| 65 | + |
| 66 | +yarn services |
| 67 | + |
| 68 | +echo -e $YELLOW |
| 69 | +echo -e "You are now in a sub-shell (i.e. a dev environment) for the dd-trace plugin '$plugin_name'." |
| 70 | +echo -e "The following environment variables set:${RESET}" |
| 71 | +echo -e "\tPLUGINS=$PLUGINS" |
| 72 | +echo -e "\tSERVICES=$SERVICES" |
| 73 | +echo -e "${YELLOW}The ${RESET}versions${YELLOW} directory has been populated, and any ${RESET}\$SERVICES${YELLOW} have been brought up if not already running." |
| 74 | +echo -e "You can now run the plugin's tests with:" |
| 75 | +echo -e "\t${RESET}yarn test:plugins" |
| 76 | +echo -e "${YELLOW}To exit this shell, type 'exit' or do Ctrl+D." |
| 77 | +echo -e $RESET |
| 78 | + |
| 79 | +$SHELL |
| 80 | + |
| 81 | +if [ -n "$teardown" ]; then |
| 82 | + docker compose stop $SERVICES |
| 83 | +fi |
| 84 | + |
| 85 | +echo -e $YELLOW |
| 86 | +echo "Exited the sub-shell for the dd-trace plugin '$plugin_name'." |
| 87 | +if [ -n "$teardown" ]; then |
| 88 | + echo "Also stopped any services that were started." |
| 89 | +fi |
| 90 | +echo "You're now back in the main shell." |
| 91 | +echo -e $RESET |
0 commit comments