-
Notifications
You must be signed in to change notification settings - Fork 1k
/
.envrc
205 lines (171 loc) · 6.95 KB
/
.envrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: Mon Feb 22 17:42:01 2021 +0000
#
# https://github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
# ============================================================================ #
# D i r E n v
# ============================================================================ #
# https://direnv.net/man/direnv-stdlib.1.html
# See Also:
#
# .envrc-aws
# .envrc-gcp
# .envrc-kubernetes
# direnv stdlib - loads .envrc from parent dir up to /
#
# useful to accumulate parent and child directory .envrc settings eg. adding Kubernetes namespace, ArgoCD app etc.
#
# bypasses security authorization though - use with care
#source_up
#
# source_up must be loaded before set -u otherwise gets this error:
#
# direnv: loading .envrc
# /bin/bash: line 226: $1: unbound variable
#
# source_up causes this error is up .envrc is found in parent directories:
#
# direnv: No ancestor .envrc found
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
src="$(readlink -f "${BASH_SOURCE[0]}")"
srcdir="$(cd "$(dirname "$src")" && pwd)"
# ============================================================================ #
# P r e - C o m m i t
# ============================================================================ #
# Automatically install Pre-Commit Git hooks if not already present
if ! type -P pre-commit &>/dev/null; then
if uname -s | grep -q Darwin &&
type -P brew &>/dev/null; then
echo
echo "Pre-commit is not installed - installing now using Homebrew..."
echo
brew install pre-commit
echo
elif type -P pip &>/dev/null; then
echo
echo "Pre-commit is not installed - installing now using Pip..."
echo
pip install pre-commit
fi
fi
if [ -f .pre-commit-config.yaml ] &&
type -P pre-commit &>/dev/null &&
git rev-parse --is-inside-work-tree &>/dev/null; then
if ! [ -f "$(git rev-parse --show-toplevel)/.git/hooks/pre-commit" ]; then
echo
echo "Pre-commit hook is not installed in local Git repo checkout - installing now..."
echo
pre-commit install
fi
fi
# ============================================================================ #
# D o c k e r C o m p o s e
# ============================================================================ #
export COMPOSE_PROJECT_NAME="bash-tools"
# ============================================================================ #
# G i t H u b
# ============================================================================ #
#export GITHUB_ORGANIZATION=HariSekhon
# ============================================================================ #
# A n s i b l e
# ============================================================================ #
# use the local repo's ansible.cfg rather than:
#
# $PWD/ansible.cfg
# ~/.ansible.cfg
# /etc/ansible/ansible.cfg
#
# set this in project repos to ensure user environment ANSIBLE_CONFIG doesn't get used
#export ANSIBLE_CONFIG="/path/to/ansible.cfg"
# ============================================================================ #
# C l o u d f l a r e
# ============================================================================ #
#export CLOUDFLARE_EMAIL=hari@...
#export CLOUDFLARE_API_KEY=... # generate here: https://dash.cloudflare.com/profile/api-tokens
#export CLOUDFLARE_TOKEN=... # used by cloudflare_api.sh but not by terraform module
# export the variables for terraform
#export TF_VAR_cloudflare_email="$CLOUDFLARE_EMAIL"
#export TF_VAR_cloudflare_api_key="$CLOUDFLARE_API_KEY" # must be a key, not a token using the link above
# ============================================================================ #
# Load External Envrc Files If Present
# ============================================================================ #
# XXX: safer to bring all these external .envrc inline if you're worried about changes
# to it bypassing 'direnv allow' authorization
load_if_exists(){
# first arg is a path to a .envrc
# all other args are passed to the sourcing of .envrc - used by .envrc-kubernetes
# to pass the context name 'docker-desktop' to switch to
local envrc="$1"
shift
if ! [[ "$envrc" =~ ^/ ]]; then
envrc="$srcdir/$envrc"
fi
if [ -f "$envrc" ]; then
# prevent looping on symlinks to this .envrc if given
if [ "$(readlink "$envrc")" = "$src" ]; then
return
fi
echo
echo "Loading $envrc"
# shellcheck disable=SC1090,SC1091
. "$envrc" "$@"
fi
}
# don't do this it may lead to an infinite loop if 'make link' symlinking ~/.envrc to this repo's .envrc
# (which I do to keep Python virtual automatically loaded at all times because recent pip on Python refuses
# to install to system Python)
#load_if_exists ~/.envrc
# ============================================================================ #
# P y t h o n
# ============================================================================ #
#.envrc-aws \
#.envrc-gcp \
#.envrc-terraform \
# shellcheck disable=SC2043
for envrc in \
.envrc-python \
; do
load_if_exists "$envrc"
done
# ============================================================================ #
# A W S
# ============================================================================ #
if [[ "$PWD" =~ /aws/ ]]; then
load_if_exists .envrc-aws
fi
# ============================================================================ #
# G C P
# ============================================================================ #
if [[ "$PWD" =~ /gcp/ ]]; then
load_if_exists .envrc-gcp
fi
# ============================================================================ #
# T e r r a f o r m
# ============================================================================ #
if [[ "$PWD" =~ /(terra(form)?|tf)(/|$) ]]; then
load_if_exists .envrc-terraform
fi
# ============================================================================ #
# K u b e r n e t e s
# ============================================================================ #
if [ -f "$srcdir/.envrc-kubernetes" ]; then
load_if_exists .envrc-kubernetes docker-desktop
fi
# ============================================================================ #
# . E n v
# ============================================================================ #
echo
# read .env too
#dotenv
load_if_exists .envrc.local