Download a Directus example

Download a Directus example

This is a simple bash script that when passed an example, will download just that example from the Directus examples repo, handy to get started quickly with an example and build something.

directus-example
#! /usr/bin/env bash
if [ -z "$1" ]; then
  echo USAGE: "$0" EXAMPLE
  exit
fi

url="https://github.com/directus/examples/$1"
# default to branch master
branch=main

# split url by /
IFS=/ read -r -a parts <<< "$url"
user="${parts[3]}"
repo="${parts[4]}"
# get folder segments starting at 5 to end of array
folders=("${parts[@]:5}")
folder="$(IFS=/; printf '%s' "${folders[*]}")"
# strip off parent folders above specific folder
strip="$((${#parts[@]} - 5))"

# download git archive
# pipe to tar to extract (strip off parent folder)
curl -L "https://github.com/${user}/${repo}/archive/${branch}.tar.gz" \
  | tar -xv --strip-components=${strip} "${repo}-${branch}/${folder}"
1

How to Use It

bash directus-example nextjs

And that's it, it will create a folder named nextjs that contains the code from the nextjs example repo.