lib/generators/wcc/templates/contentful_shell_wrapper in wcc-contentful-0.1.0 vs lib/generators/wcc/templates/contentful_shell_wrapper in wcc-contentful-0.2.0
- old
+ new
@@ -6,17 +6,30 @@
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}") || true
+ [[ -z "$VERBOSE" ]] && return 0;
+
+ local msg=$(echo "$@" | sed "s/$CONTENTFUL_MANAGEMENT_TOKEN/\*\*\*\*\*/" )
+ >&2 echo -e "${COLOR_GRAY}$msg${COLOR_NC}" || true
}
logerr() {
>&2 echo -e "${COLOR_RED}$@${COLOR_NC}"
}
+curlv() {
+ logv "curl" $@
+ curl "$@"
+}
+
+execv() {
+ logv "$@"
+ "$@"
+}
+
## *** Argument Parsing & validation ***
usage() {
echo "$0 <command> [opts]
Commands:
@@ -39,14 +52,13 @@
restore [file]
restores a given backup file into the current space
* [file] optional - default: the most recent backup file in the current directory
- restore_from -s [to space ID] <from space ID>
- restores all data from the given space into the current space
- * <space ID> required - the ID of the space to receive data from
- * -s [to space ID] optional - the current working space. Default: \$CONTENTFUL_SPACE_ID
+ new_env
+ deletes the current working environment if it exists and makes a new clone of 'master'.
+ * -e [to environment ID] optional - the current working environment. Default: \$USER
generate [name]
Creates a sample migration in the db/migrate directory
* [name] optional - default: 'contentful_migration'
@@ -59,28 +71,31 @@
parse_args() {
OPTIND=1
local s=$(echo "$1" | tr '[:upper:]' '[:lower:]')
case "$s" in
- migrate|setup|backup|export|restore|restore_from|import|generate|clean|help|h|\?)
+ migrate|setup|backup|export|restore|new_env|import|generate|clean|help|h|\?)
export subcommand=$s
OPTIND=2
;;
esac
# Parse flags
- while getopts ":hyvs:a:" arg; do
+ while getopts ":hyvse:a:" arg; do
case $arg in
y) # Yes - skip prompts
export YES="-y"
;;
s) # Contentful Space ID - overrides env var CONTENTFUL_SPACE_ID
export CONTENTFUL_SPACE_ID=$OPTARG
;;
a) # Contentful Mgmt Token - overrides env var CONTENTFUL_MANAGEMENT_TOKEN
export CONTENTFUL_MANAGEMENT_TOKEN=$OPTARG
;;
+ e) # Contentful environment ID - overrides env var CONTENTFUL_ENVIRONMENT
+ export CONTENTFUL_ENVIRONMENT=$OPTARG
+ ;;
v) # Verbose mode - extra output
export VERBOSE=true
;;
h) # Display help.
usage
@@ -104,12 +119,11 @@
require_environment() {
[[ -z "$CONTENTFUL_SPACE_ID" ]] && logerr "Please set CONTENTFUL_SPACE_ID environment variable or use '-s' flag." && exit -1;
[[ -z "$CONTENTFUL_MANAGEMENT_TOKEN" ]] && logerr "Please set CONTENTFUL_MANAGEMENT_TOKEN environment variable or use '-a' flag." && exit -1;
if [[ ! -f node_modules/.bin/contentful-migration ]]; then
command -v npm >/dev/null 2>&1 || (logerr "I require 'npm' but it's not installed. Please install nodejs."; exit -1)
- logv "npm install"
- npm install
+ execv npm install
[[ -f node_modules/.bin/contentful-migration ]] || (logerr "Failed installing node modules - please ensure contentful CLI is installed"; exit -1)
fi
}
## *** Utility functions ***
@@ -131,14 +145,26 @@
esac
done
}
get_space_name() {
- logv "curl -s https://api.contentful.com/spaces/$1?access_token=****"
- curl -s https://api.contentful.com/spaces/$1?access_token=$CONTENTFUL_MANAGEMENT_TOKEN | jq -r .name | tr '[:upper:]' '[:lower:]'
+ curlv -s https://api.contentful.com/spaces/$1?access_token=$CONTENTFUL_MANAGEMENT_TOKEN | jq -r .name | tr '[:upper:]' '[:lower:]'
}
+# Man I wish I understood sed... https://stackoverflow.com/a/29060802
+# $1 File
+# $2 Find
+# $3 Replace / Append
+replace_append() {
+ if grep -q "^$2" "$1"
+ then
+ sed -i.bak "s/^$2.*$/$3/" "$1"
+ else
+ echo "\n$3" >> "$1"
+ fi
+}
+
set -e
# *** Commands ***
# Example: bin/contentful migrate -y -s 1xab -a $MY_TOKEN db/migrate/20180101120000_add_content_type_dog.ts
@@ -148,18 +174,16 @@
[[ -z "$ARG" ]] && ARG="db/migrate"
[[ -d "$ARG" ]] && ARG="batch $ARG"
require_environment
- logv "contentful-migration -s $CONTENTFUL_SPACE_ID -a ***** $YES -p $ARG"
- node_modules/.bin/ts-node node_modules/.bin/contentful-migration \
+ execv node_modules/.bin/ts-node node_modules/.bin/contentful-migration \
-s $CONTENTFUL_SPACE_ID -a $CONTENTFUL_MANAGEMENT_TOKEN \
$YES -p $ARG
mkdir -p db
- logv "contentful-export -s --export-dir db --content-file contentful-schema.json --space-id $CONTENTFUL_SPACE_ID --management-token ***** --query-entries 'content_type=migrationHistory' --query-assets 'sys.id=false'"
- node_modules/.bin/contentful-export --export-dir db --content-file contentful-schema.json \
+ execv 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
@@ -175,13 +199,14 @@
FILE="$1"
[[ ! -z "$FILE" ]] && FILE="--content-file $FILE" && shift
require_environment
- logv "contentful-export $FILE --space-id $CONTENTFUL_SPACE_ID --management-token ***** $@"
- node_modules/.bin/contentful-export $FILE \
- --space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
+ [[ ! -z "$CONTENTFUL_ENVIRONMENT" ]] && ENV="--environment-id $CONTENTFUL_ENVIRONMENT"
+
+ execv node_modules/.bin/contentful-export $FILE \
+ --space-id $CONTENTFUL_SPACE_ID $ENV --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]
@@ -190,33 +215,42 @@
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
+ name=$(get_space_name $CONTENTFUL_SPACE_ID)
+ if [[ ! -z "$CONTENTFUL_ENVIRONMENT" ]]; then
+ ENV="--environment-id $CONTENTFUL_ENVIRONMENT"
+ name="$name/$CONTENTFUL_ENVIRONMENT"
+ fi
+ confirm "Import $FILE into $name?" || exit -1
require_environment
- logv "contentful-import --space-id $CONTENTFUL_SPACE_ID --management-token ***** --content-file $FILE"
- node_modules/.bin/contentful-import \
- --space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
+ execv node_modules/.bin/contentful-import \
+ --space-id $CONTENTFUL_SPACE_ID $ENV --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
+ name=$(get_space_name $CONTENTFUL_SPACE_ID)
+ if [[ ! -z "$CONTENTFUL_ENVIRONMENT" ]]; then
+ ENV="--environment-id $CONTENTFUL_ENVIRONMENT"
+ name="$name/$CONTENTFUL_ENVIRONMENT"
+ fi
+ confirm "Initialize space $name from seed file $FILE?" || exit -1
+
require_environment
- logv "contentful-import --space-id $CONTENTFUL_SPACE_ID --management-token ***** --content-file $FILE"
- node_modules/.bin/contentful-import \
- --space-id $CONTENTFUL_SPACE_ID --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
+ execv node_modules/.bin/contentful-import \
+ --space-id $CONTENTFUL_SPACE_ID $ENV --management-token $CONTENTFUL_MANAGEMENT_TOKEN \
--content-file $FILE
migrate
}
@@ -224,64 +258,71 @@
clean() {
command -v jq >/dev/null 2>&1 || (logerr "I require 'jq' but it's not installed. Please run 'brew install jq'"; exit -1)
require_environment
+ [[ -z "$CONTENTFUL_ENVIRONMENT" ]] && CONTENTFUL_ENVIRONMENT="$USER"
+ [[ "$CONTENTFUL_ENVIRONMENT" == "master" ]] && logerr "cannot delete the master environment" && exit -1
+
name=$(get_space_name $CONTENTFUL_SPACE_ID)
+ code=$(curlv -s -o /dev/null -w "%{http_code}" https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/environments/$CONTENTFUL_ENVIRONMENT\?access_token\=$CONTENTFUL_MANAGEMENT_TOKEN)
+ [[ $code == "404" ]] && logerr "$CONTENTFUL_ENVIRONMENT does not exist in $name" && return 0;
- confirm "This will delete all data and content types from $name. Are you sure?" || exit -1
- [[ "$name" != *staging ]] && [[ "$name" != *test ]] && confirm "$name is not a staging or test environment! Are you really sure?" dangerous!
-
- local bkup_file="contentful-export-$CONTENTFUL_SPACE_ID-`date +"%Y-%m-%dT%H-%M-%S"`.json"
+ confirm "This will delete the '$CONTENTFUL_ENVIRONMENT' environment from $name. Are you sure?" || exit -1
+
+ local bkup_file="contentful-export-$CONTENTFUL_SPACE_ID-${CONTENTFUL_ENVIRONMENT}-`date +"%Y-%m-%dT%H-%M-%S"`.json"
backup $bkup_file
- content_types=$(cat $bkup_file | jq -r '.contentTypes[].sys.id')
- entries=$(cat $bkup_file | jq -r '.entries[].sys.id')
- assets=$(cat $bkup_file | jq -r '.assets[].sys.id')
-
- delete() {
- logv "curl -XDELETE https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/$1/$2/published\?access_token\=*****"
- curl -s --fail -XDELETE https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/$1/$2/published\?access_token\=$CONTENTFUL_MANAGEMENT_TOKEN > /dev/null
- logv "curl -XDELETE https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/$1/$2\?access_token\=*****"
- curl -s --fail -XDELETE https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/$1/$2\?access_token\=$CONTENTFUL_MANAGEMENT_TOKEN > /dev/null
- }
-
- [[ ! -z "$assets" ]] && while read -r id; do delete 'assets' $id; done <<< "${assets}"
- [[ ! -z "$entries" ]] && while read -r id; do delete 'entries' $id; done <<< "${entries}"
- [[ ! -z "$content_types" ]] && while read -r id; do delete 'content_types' $id; done <<< "${content_types}"
-
- echo -e "${COLOR_LGREEN}All content deleted - prior content saved at${COLOR_NC} $bkup_file"
- [[ "$1" == "no-init" ]] || (setup || logerr "Error setting up space $name!")
+
+ curlv --fail -XDELETE https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/environments/$CONTENTFUL_ENVIRONMENT\?access_token\=$CONTENTFUL_MANAGEMENT_TOKEN
}
-# Example: bin/contentful restore_from -s $staging_space_id -a $MY_TOKEN $production_space_id
-restore_from() {
+# Example: bin/contentful new_env -e gordon_dev
+new_env() {
command -v jq >/dev/null 2>&1 || (logerr "I require 'jq' but it's not installed. Please run 'brew install jq'"; exit -1)
require_environment
+ name=$(get_space_name $CONTENTFUL_SPACE_ID)
+ [[ -z "$CONTENTFUL_ENVIRONMENT" ]] && CONTENTFUL_ENVIRONMENT="$USER"
+ [[ "$CONTENTFUL_ENVIRONMENT" == "master" ]] && logerr "cannot delete the master environment" && exit -1
- from_space_id=$1
- [[ -z "$from_space_id" ]] && logerr "Please provide the space ID from which to restore data" && exit -1
- to_space_id=$CONTENTFUL_SPACE_ID
-
- from_name=$(get_space_name $from_space_id)
- to_name=$(get_space_name $CONTENTFUL_SPACE_ID)
-
- echo -e "${COLOR_LCYAN}This will backup all data from $from_name, delete all data in $to_name, and then write the $from_name data over it.${COLOR_NC}"
+ echo -e "${COLOR_LCYAN}This will delete '$CONTENTFUL_ENVIRONMENT' and recreate it from master.${COLOR_NC}"
confirm "Continue?" || exit -1
export YES='-y' # don't keep bugging the user
- export CONTENTFUL_SPACE_ID=$from_space_id
- local bkup_file="contentful-export-$from_space_id-`date +"%Y-%m-%dT%H-%M-%S"`.json"
- logv "bin/contentful backup -s $CONTENTFUL_SPACE_ID $bkup_file"
- backup $bkup_file --skip-roles --skip-webhooks
+ clean
- export CONTENTFUL_SPACE_ID=$to_space_id
- logv "bin/contentful clean -s $CONTENTFUL_SPACE_ID"
- clean no-init
+ # make the environment
+ resp=$(curlv -s --fail -XPUT https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/environments/$CONTENTFUL_ENVIRONMENT \
+ -H "Authorization: Bearer ${CONTENTFUL_MANAGEMENT_TOKEN}" \
+ -H "Content-Type: application/vnd.contentful.management.v1+json" \
+ -d "{ \"name\": \"${CONTENTFUL_ENVIRONMENT}\" }")
+
+ while [ ! $(echo "$resp" | jq -r .sys.status.sys.id) == "ready" ]
+ do
+ logv "waiting for environment $CONTENTFUL_ENVIRONMENT to be ready..."
+ sleep 1
+ resp=$(curlv -s https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/environments/$CONTENTFUL_ENVIRONMENT \
+ -H "Authorization: Bearer ${CONTENTFUL_MANAGEMENT_TOKEN}")
+ done
+
+ logv "get the API keys and update the one matching ours to point to the new environment"
+ keys=$(curlv -s --fail https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/api_keys\?access_token\=$CONTENTFUL_MANAGEMENT_TOKEN)
+ my_key=$(echo "$keys" | jq -r ".items[] | select(.accessToken == \"$CONTENTFUL_ACCESS_TOKEN\")")
+ my_key_id=$(echo "$my_key" | jq -r ".sys.id")
+ my_key_version=$(echo "$my_key" | jq -r ".sys.version")
+ new_env_links=$(echo "$my_key" | jq ".environments + [{ \"sys\": { \"id\": \"$CONTENTFUL_ENVIRONMENT\", \"type\": \"Link\", \"linkType\": \"Environment\" } }] | { \"environments\": . }")
+
+ curlv -s -o /dev/null --fail -XPUT https://api.contentful.com/spaces/$CONTENTFUL_SPACE_ID/api_keys/$my_key_id \
+ -H "Authorization: Bearer ${CONTENTFUL_MANAGEMENT_TOKEN}" \
+ -H "Content-Type: application/vnd.contentful.management.v1+json" \
+ -H "X-Contentful-Version: ${my_key_version}" \
+ -d "${new_env_links}"
- logv "bin/contentful restore -s $CONTENTFUL_SPACE_ID $bkup_file"
- restore $bkup_file
+ execv replace_append .env.local "CONTENTFUL_ENVIRONMENT\=" "CONTENTFUL_ENVIRONMENT=$CONTENTFUL_ENVIRONMENT"
+ execv replace_append .env.test.local "CONTENTFUL_ENVIRONMENT\=" "CONTENTFUL_ENVIRONMENT=$CONTENTFUL_ENVIRONMENT"
+
+ echo "Environment ${CONTENTFUL_ENVIRONMENT} successfully created!"
}
# Example: bin/contentful generate add content type dog
# equivalent to: bin/rails generate migration add_content_type_dog
generate() {
@@ -326,11 +367,11 @@
generate $@
;;
clean)
clean $@
;;
- restore_from)
- restore_from $@
+ new_env)
+ new_env $@
;;
help|h|\?)
usage
;;
*)
\ No newline at end of file