You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB
Bash

4 years ago
#!/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)
#
2 years ago
# https://github.com/HariSekhon/DevOps-Bash-tools
4 years ago
#
# 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
#
2 years ago
# https://www.linkedin.com/in/HariSekhon
4 years ago
#
4 years ago
# 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
4 years ago
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
4 years ago
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4 years ago
4 years ago
# shellcheck disable=SC1090
. "$srcdir/lib/utils.sh"
4 years ago
4 years ago
# shellcheck disable=SC1090
. "$srcdir/.bash.d/aliases.sh"
4 years ago
# shellcheck disable=SC1090
. "$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 "$@"
4 years ago
# defer expansion
# shellcheck disable=SC2016
4 years ago
trap_cmd 'exitcode=$?; echo; echo "Exit Code: $exitcode"'
4 years ago
4 years ago
filename="$1"
4 years ago
# examples:
#
# # run: kubectl apply -f file.yaml
# // run: go run file.go
# -- run: psql -f file.sql
run_cmd="$(parse_run_args "$filename")"
4 years ago
4 years ago
dirname="$(dirname "$filename")"
4 years ago
basename="${filename##*/}"
4 years ago
cd "$dirname"
3 years ago
docker_compose_up(){
3 years ago
docker-compose -f "$filename" up
3 years ago
}
4 years ago
if [ -n "$run_cmd" ]; then
eval "$run_cmd"
else
4 years ago
case "$basename" in
2 years ago
Makefile) make
;;
Dockerfile) if [ -f Makefile ]; then
make
else
docker build .
fi
;;
*docker-compose*.y*ml) docker_compose_up
;;
2 years ago
Gemfile) bundle install
;;
2 years ago
cloudbuild*.y*ml) gcloud builds submit --config "$basename" .
;;
kustomization.yaml) kustomize build --enable-helm
;;
.envrc) cd "$dirname" && direnv allow .
;;
*.go) eval go run "'$filename'" "$("$srcdir/lib/args_extract.sh" "$filename")"
2 years ago
;;
*.tf) #terraform plan
terraform apply
;;
*.md) bash -ic "cd '$dirname'; gitbrowse"
;;
*) 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")"
2 years ago
echo "'$filename'" "$args" >&2
eval "'$filename'" "$args"
;;
4 years ago
esac
4 years ago
fi