Skip to content

Instantly share code, notes, and snippets.

@gburgett
Last active February 26, 2018 15:11
Show Gist options
  • Save gburgett/4035730cf56c9314cded6409ac1c6186 to your computer and use it in GitHub Desktop.
Save gburgett/4035730cf56c9314cded6409ac1c6186 to your computer and use it in GitHub Desktop.
Bash script in rails `bin` directory wrapping Contentful command line tools to make them similar to rails DB tools
#!/bin/bash
COLOR_NC='\033[0m' # No Color
COLOR_GRAY='\033[1;30m'
COLOR_RED='\033[0;31m'
COLOR_LCYAN='\033[1;36m'
COLOR_YELLOW='\033[1;33m'
COLOR_LGREEN='\033[1;32m'
logv() {
[[ ! -z "$VERBOSE" ]] && >&2 echo -e "${COLOR_GRAY}$@${COLOR_NC}"
}
logerr() {
>&2 echo -e "${COLOR_RED}$@${COLOR_NC}"
}
usage() {
echo "$0 <command> [opts]
Commands:
migrate [dir|file] - runs pending migration files in the given directory
[dir|file] Default: db/migrate
setup [file] - initializes a space with bare-minimum schema and seeds
[file] default: db/contentful-schema.json
backup [file] - downloads a backup of the current space to the given file
[file] default: timestamped file in current directory
restore [file] - restores a given backup file into the current space
[file] default: the most recent backup file in the current directory
generate [name] - Creates a sample migration in the db/migrate directory
[name] default: 'contentful_migration'
Flags:" && \
grep " .)\ #" $0
echo "Examples:" && \
grep -i "#\ example:" $0 | awk '{$1=""; $2=""; print $0}'
}
subcommand=$(echo "$1" | tr '[:upper:]' '[:lower:]')
case "$subcommand" in
migrate|setup|backup|restore|generate|help|h|\?)
shift
;;
esac
# Parse flags
while getopts ":hys:a:" arg; do
case $arg in
y) # Yes - skip prompts
YES="-y"
;;
s) # Contentful Space ID - overrides env var CONTENTFUL_SPACE_ID
CONTENTFUL_SPACE_ID=$OPTARG
;;
a) # Contentful Mgmt Token - overrides env var CONTENTFUL_MANAGEMENT_TOKEN
CONTENTFUL_MANAGEMENT_TOKEN=$OPTARG
;;
h) # Display help.
usage
exit 0
;;
*)
logerr "Unknown option: '$OPTARG'"
usage
exit -1
;;
esac
done
shift $(($OPTIND - 1))
[[ -z "$CONTENTFUL_SPACE_ID" ]] && logerr "Please set CONTENTFUL_SPACE_ID" && exit -1;
[[ -z "$CONTENTFUL_MANAGEMENT_TOKEN" ]] && logerr "Please set CONTENTFUL_MANAGEMENT_TOKEN" && exit -1;
if [[ ! -f node_modules/.bin/contentful-migration ]]; then
npm install
[[ ! -f node_modules/.bin/contentful-migration ]] && logerr "Failed installing node modules - please ensure contentful CLI is installed" && exit -1;
fi
confirm() {
[[ ! -z "$YES" ]] && return 0;
while true; do
read -p $'\033[1;36m'"$1"' (y/n): '$'\033[0m' yn
case $yn in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
set -e
# Example: bin/contentful migrate
# Example: bin/contentful migrate -y -s 1xab -a $MY_TOKEN db/migrate/20180101120000_add_content_type_dog.ts
# equivalent to: bin/rake db:migrate
migrate() {
ARG="$1"
[[ -z "$ARG" ]] && ARG="db/migrate"
[[ -d "$ARG" ]] && ARG="batch $ARG"
node_modules/.bin/ts-node node_modules/.bin/contentful-migration \
-s $CONTENTFUL_SPACE_ID -a $CONTENTFUL_MANAGEMENT_TOKEN \
$YES -p $ARG
echo "Dumping Schema"
mkdir -p db
node_modules/.bin/contentful-export --export-dir db --content-file contentful-schema.json \
--space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
--query-entries 'content_type=migrationHistory' \
--query-assets 'sys.id=false'
if [[ $(git diff-index --name-only HEAD | grep 'db/contentful-schema.json') == "" ]]; then
echo -e "${COLOR_LGREEN}✓ Schema in contentful space is equivalent to stored schema${COLOR_NC}"
else
echo -e "${COLOR_YELLOW}⚠️ Schema changed after running migrations${COLOR_NC}"
fi
}
# Example: bin/contentful backup -s 1xab -a $MY_TOKEN 2018_01_01.1xab.dump.json
# equivalent to: bin/rake db:dump[2018_01_01.dump]
backup() {
FILE="$1"
[[ ! -z "$FILE" ]] && FILE="--content-file $FILE"
node_modules/.bin/contentful-export $FILE \
--space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN
}
# Example: bin/contentful restore -y -s 1xab -a $MY_TOKEN 2018_01_01.1xab.dump.json
# equivalent to: bin/rake db:restore[2018_01_01.dump]
restore() {
FILE="$1"
if [[ -z "$FILE" ]]; then
FILE=$(ls contentful-export-$CONTENTFUL_SPACE_ID-* | sort -r | head -n 1)
[[ -z "$FILE" ]] && logerr "No file given on command line" && exit -1
fi
confirm "Import $FILE into $CONTENTFUL_SPACE_ID?" || exit -1
node_modules/.bin/contentful-import \
--space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
--content-file $FILE
}
# Example: bin/contentful setup -y -s 1xab -a $MY_TOKEN db/my-schema.json
# equivalent to: bin/rake db:setup
setup() {
FILE="$1"
[[ -z "$FILE" ]] && FILE=db/contentful-schema.json
confirm "Initialize space $CONTENTFUL_SPACE_ID from seed file $FILE?" || exit -1
node_modules/.bin/contentful-import \
--space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
--content-file $FILE
migrate
}
# Example: bin/contentful generate add content type dog
# equivalent to: bin/rails generate migration add_content_type_dog
generate() {
migration="
import Migration from 'contentful-migration-cli'
export = function (migration: Migration) {
const dog = migration.createContentType('dog', {
name: 'Dog'
})
const name = dog.createField('name')
name.name('Name')
.type('Symbol')
.required(true)
}
"
timestamp=$(date +%Y%m%d%H%M%S)
filename="$@"
[[ -z "$filename" ]] && filename="contentful_migration"
filename=${filename// /\_}
filename="db/migrate/${timestamp}_${filename}.ts"
echo "$migration" > $filename
echo "generated file $filename"
}
case $subcommand in
migrate)
migrate $@
;;
backup)
backup $@
;;
restore)
restore $@
;;
setup)
setup $@
;;
generate)
generate $@
;;
help|h|\?)
usage
;;
*)
logerr "Unknown command: '$subcommand'"
usage
exit -1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment