Skip to content

Instantly share code, notes, and snippets.

@cadorn
Last active August 29, 2015 14:01
Show Gist options
  • Save cadorn/73609798c63c60489b63 to your computer and use it in GitHub Desktop.
Save cadorn/73609798c63c60489b63 to your computer and use it in GitHub Desktop.
workspace activation

You activate a workspace by placing environment variables into your shell and modifying the PATH.

Shell environment variables represent the root config layer and all config resolving must happen from this base layer. If any code in your codebase must resolve config info it MUST have access to environment variables directly or indirectly or at the very least to the root path of the workspace so the environment variables may be established by loading and parsing the raw files.

Any config resolution logic must in its entirety be seeded by the environment variables and may then branch out with arbitrary logic. Ideally further config resolution layers follow rules and patterns that me be shared across implementations.

pio conventions

Given a file tree such as:

/project   <-   clone from github
/project/bin/activate.sh
/project.activate.sh
/project.profile.json

With file content such as:

/project.activate.sh

#!/bin/bash -e

# Digital Ocean - https://digitalocean.com/
export DIGIO_EMAIL="xxx"
export DIGIO_CLIENT_ID="xxx"
export DIGIO_API_KEY="xxx"

# pio credentials
export PIO_SEED_SALT="xxx"
export PIO_SEED_KEY="xxx"
export PIO_USER_ID="xxx"
export PIO_USER_SECRET="xxx"

# pio config
export PIO_PROFILE_PATH="/project.profile.json"

/project.profile.sh

{
    "config": {
        "pio.vm": {
            "adapter": "digitalocean"
        }
    }
}

/project/bin/activate.sh

#!/bin/bash

# TODO: Relocate into helper module.
# @credit http://stackoverflow.com/a/246128/330439
SOURCE="${BASH_SOURCE[0]:-$0}"
while [ -h "$SOURCE" ]; do
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
BASE_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"


echo "[pio] Switching environment ..."

export PATH=$BASE_PATH:$PATH

PS1="\[\033[1;34m\]\[\033[47m\](OS)\[\033[0m\] \[\033[1;35m\]$(basename $(dirname $BASE_PATH))\[\033[0m\] \[\033[33m\]\u\[\033[1;33m\]$\[\033[0m\] "


if [ ! -f "$BASE_PATH/../../$(basename $(dirname $BASE_PATH)).activate.sh" ]; then
  # Create /project.activate.sh file
fi

if [ -f "$BASE_PATH/../../$(basename $(dirname $BASE_PATH)).activate.sh" ]; then
  . $BASE_PATH/../../$(basename $(dirname $BASE_PATH)).activate.sh
fi

The following can be done:

  • The shell environment variables modified when calling source bin/activate.sh
  • The /project.activate.sh created by a script if it does not exist
  • The /project.activate.sh holding private credentials loaded into the shell environment
  • Establishing a root config layer based on shell environment variables
  • Arbitrary config resolution logic on top of root config layer
  • Shared config resolution logic across implementations by adhering to patterns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment