Skip to content

Instantly share code, notes, and snippets.

@xaethos
Created September 6, 2016 03:43
Show Gist options
  • Save xaethos/50930b508159388e24fcf3192e49a6b3 to your computer and use it in GitHub Desktop.
Save xaethos/50930b508159388e24fcf3192e49a6b3 to your computer and use it in GitHub Desktop.
Bash script to fetch, edit, and write back a Concourse pipeline
#!/bin/bash
set -euo pipefail
TARGET=${1:-}
PIPELINE=${2:-}
FLY=$(which fly)
TEMPDIR=$(mktemp -d -t concourse)
cleanup() {
rm -rf "$TEMPDIR"
}
trap cleanup EXIT
fail() {
echo "$*" >&2
exit 1
}
usage() {
echo "Usage: $0 TARGET PIPELINE" >&2
exit 2
}
checksum() {
local file="$1"
cat "$file" | md5
}
write_header() {
local file="$1"
printf "# Pipeline $PIPELINE in target $TARGET\n---\n" >"$file"
}
get_pipeline() {
local file="$1"
write_header "$file"
"$FLY" -t $TARGET get-pipeline -p $PIPELINE >>"$file"
}
set_pipeline() {
local file="$1"
"$FLY" -t $TARGET set-pipeline -p $PIPELINE -c "$file"
}
edit_pipeline() {
local file="$TEMPDIR/pipeline.yml"
local old_checksum
get_pipeline "$file"
old_checksum=$(checksum "$file")
nvim -n -- "$file"
if [ $old_checksum = $(checksum "$file") ]
then echo "No changes to pipeline"
else set_pipeline "$file"
fi
}
[ -n "$TARGET" ] || usage
[ -n "$PIPELINE" ] || usage
[ -x "$FLY" ] || fail "fly command not executable: $FLY"
edit_pipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment