Skip to content

Instantly share code, notes, and snippets.

@AlexanderOMara
Last active July 11, 2021 19:50
Show Gist options
  • Save AlexanderOMara/216cb168da866199105c707de623d9c9 to your computer and use it in GitHub Desktop.
Save AlexanderOMara/216cb168da866199105c707de623d9c9 to your computer and use it in GitHub Desktop.
rclonenv - Rclone with directory-level environment variable config files via wrapper script
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Licenses: Public Domain, CC0, MIT
# For ZSH autocompletion add to .zshrc:
# compdef rclonenv=rclone
# Name of env file.
env_name='rclone.env'
# Sample rclone.env:
# RCLONE_CONFIG_EXAMPLE_TYPE=alias
# RCLONE_CONFIG_EXAMPLE_REMOTE=remote:subpath
# List env files from parent directories.
env_files=()
dir="${PWD}"
while [[ "${dir}" != '' ]]; do
env_file="${dir}/${env_name}"
if [[ -f "${env_file}" ]]; then
env_files+=("${env_file}")
fi
dir="${dir%/*}"
done
# Process env files, high to low.
for ((i="${#env_files[@]}"; i-->0;)); do
env_file="${env_files[$i]}"
while IFS= read -r line; do
# Trim carriage return character if present.
if [[ "${line}" == *$'\r' ]]; then
line="${line%?}"
fi
# Skip any empty and comment lines.
if [[ -z "${line}" ]] || [[ "${line}" == '#'* ]]; then
continue
fi
export "${line}"
done < "${env_file}"
done
exec rclone "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment