-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
run.sh
executable file
·148 lines (131 loc) · 4.96 KB
/
run.sh
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
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2019-12-20 16:01:28 +0000 (Fri, 20 Dec 2019)
#
# 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
#
# https://www.linkedin.com/in/HariSekhon
#
# Helper script for calling from vim function to run programs or execute with args extraction
#
# Runs the value of the 'run:' header from the given file
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/utils.sh"
# shellcheck disable=SC1090,SC1091
. "$srcdir/.bash.d/aliases.sh"
# shellcheck disable=SC1090,SC1091
. "$srcdir/.bash.d/functions.sh"
# shellcheck disable=SC2034,SC2154
usage_description="
Runs one or more files
Auto-determines the file types, any run or arg headers and executes each file using the appropriate script or CLI tool
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="file1 [file2 file3 ...]"
help_usage "$@"
min_args 1 "$@"
# defer expansion
# shellcheck disable=SC2016
trap_cmd 'exitcode=$?; echo; echo "Exit Code: $exitcode"'
filename="$1"
# examples:
#
# # run: kubectl apply -f file.yaml
# // run: go run file.go
# -- run: psql -f file.sql
run_cmd="$(parse_run_args "$filename")"
filename="$(readlink -f "$filename")"
dirname="$(dirname "$filename")"
basename="${filename##*/}"
cd "$dirname"
docker_compose_up(){
local dc_args=()
local env_file="${filename%.*}.env"
if [ -f "$env_file" ]; then
dc_args+=(--env-file "$env_file")
fi
docker-compose -f "$filename" ${dc_args:+"${dc_args[@]}"} up
}
if [ -n "$run_cmd" ]; then
eval "$run_cmd"
# fails to do the open for d2 diagrams
#elif test -x "$basename"; then
# ./"$basename"
else
case "$basename" in
Makefile) make
;;
Dockerfile) if [ -f Makefile ]; then
make
else
docker build .
fi
;;
*docker-compose*.y*ml) docker_compose_up
;;
Gemfile) bundle install
;;
cloudbuild*.y*ml) gcloud builds submit --config "$basename" .
;;
kustomization.yaml) kustomize build --enable-helm
;;
.envrc) direnv allow .
;;
*.d2) if test -x "$basename"; then
# use its shebang line to get the settings like --theme or --layout elk eg. for github_actions_cicd.d2 in https://github.com/HariSekhon/Diagrams-as-Code
./"$basename"
# shellcheck disable=SC2012
latest_image="$(ls -t "${basename%.d2}".{png,svg} 2>/dev/null | head -n1 || :)"
else
image="${basename%.d2}.svg"
d2 --dark-theme 200 "$basename" "$image"
latest_image="$image"
fi
open "$latest_image"
;;
*.go) eval go run "'$filename'" "$("$srcdir/lib/args_extract.sh" "$filename")"
;;
*.tf) #terraform plan
terraform apply
;;
terragrunt.hcl) terragrunt apply
;;
*.pkr.hcl|*.pkr.json) packer init "$filename" &&
packer build "$filename"
;;
*.md) bash -ic "cd '$dirname'; gitbrowse"
;;
*.gv) file_png="${filename%.gv}.png"
dot -T png "$filename" -o "$file_png" >/dev/null && open "$file_png"
;;
.pylintrc) pylint ./*.py
;;
*) if [[ "$filename" =~ /docker-compose/.+\.ya?ml$ ]]; then
docker_compose_up
elif [[ "$filename" =~ \.ya?ml$ ]] &&
grep -q '^apiVersion:' "$filename" &&
grep -q '^kind:' "$filename"; then
# a yaml with these apiVersion and kind fields is almost certainly a kubernetes manifest
kubectl apply -f "$filename"
exit 0
fi
if ! [ -x "$filename" ]; then
echo "ERROR: file '$filename' is not set executable!" >&2
exit 1
fi
args="$("$srcdir/lib/args_extract.sh" "$filename")"
echo "'$filename'" "$args" >&2
eval "'$filename'" "$args"
;;
esac
fi