2021-04-29 00:27:14 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
if [ "$DEBUG" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
cmd="$1"; shift
|
|
|
|
case "$cmd" in
|
|
|
|
info)
|
|
|
|
cat <<-EOF
|
|
|
|
repo: $(repo)
|
|
|
|
branch: $(branch)
|
2021-05-04 21:22:52 +02:00
|
|
|
env: $(current_env)
|
2021-04-29 00:27:14 +02:00
|
|
|
EOF
|
|
|
|
;;
|
|
|
|
list)
|
2021-05-04 21:22:52 +02:00
|
|
|
dashA="${1:-}"
|
|
|
|
if [ "$dashA" = "-a" ]; then
|
|
|
|
list_all_envs
|
|
|
|
else
|
|
|
|
list_all_envs | {
|
|
|
|
while read path; do
|
|
|
|
if store_stat "dagger/$(branch)" "$path" ; then
|
|
|
|
echo "$path"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
fi
|
2021-04-29 00:27:14 +02:00
|
|
|
;;
|
|
|
|
*)
|
2021-05-04 21:22:52 +02:00
|
|
|
dagger "$cmd" "$@"
|
2021-04-29 00:27:14 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function current_env() {
|
|
|
|
if [ "$(ls -A | grep '^.*\.cue$')" ]; then
|
|
|
|
git rev-parse --show-prefix
|
|
|
|
fi
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function repo() {
|
|
|
|
git rev-parse --show-toplevel
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function list_all_envs() {
|
|
|
|
(
|
|
|
|
cd $(repo)
|
|
|
|
find . \
|
|
|
|
-type f -name '*.cue' -exec dirname {} \; \
|
|
|
|
-o \
|
|
|
|
-type d -name 'cue.mod' -prune \
|
|
|
|
| sed 's/^\.\///' | sort -u
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-29 00:27:14 +02:00
|
|
|
function branch() {
|
|
|
|
git branch --show-current
|
|
|
|
}
|
|
|
|
|
|
|
|
function dagger() {
|
2021-05-04 21:22:52 +02:00
|
|
|
local env="$(current_env)"
|
|
|
|
if [ -z "$env" ]; then
|
|
|
|
fatal "current directory is not a valid environment"
|
2021-04-29 00:27:14 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
tmpHome="$(mktemp -d)"
|
2021-05-04 21:22:52 +02:00
|
|
|
tmpState="${tmpHome}/.dagger/store/my/deployment.json"
|
2021-04-29 00:27:14 +02:00
|
|
|
mkdir -p "$(dirname $tmpState)"
|
|
|
|
loadState "$env" > "$tmpState"
|
2021-05-04 21:22:52 +02:00
|
|
|
HOME="$tmpHome" "$(which dagger)" -e "my" "$@"
|
2021-04-29 00:27:14 +02:00
|
|
|
cat "$tmpState" | saveState "$env"
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function cleanPath() {
|
|
|
|
echo "$1" | sed -E 's/\/+/\//g'
|
|
|
|
}
|
|
|
|
|
2021-04-29 00:27:14 +02:00
|
|
|
function loadState() {
|
2021-05-04 21:22:52 +02:00
|
|
|
local env="$1"
|
|
|
|
local stateFile="$(cleanPath $env/state.json)"
|
2021-04-29 00:27:14 +02:00
|
|
|
# echo >&2 "Loading state for $(branch)::$env"
|
2021-05-04 21:22:52 +02:00
|
|
|
store_read "dagger/$(branch)" "$stateFile" |
|
|
|
|
jq ".name=\"my\"" |
|
2021-04-29 00:27:14 +02:00
|
|
|
jq ".plan.type=\"dir\"" |
|
2021-05-04 21:22:52 +02:00
|
|
|
jq ".plan.dir.path=\"$(repo)/$env\"" |
|
|
|
|
jq ".id=\"my\""
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveState() {
|
2021-05-04 21:22:52 +02:00
|
|
|
local env="$1"
|
|
|
|
local stateFile="$(cleanPath $env/state.json)"
|
2021-04-29 00:27:14 +02:00
|
|
|
# echo >&2 "Saving state for $(branch)::$env"
|
|
|
|
newState="$(cat)"
|
2021-05-04 21:22:52 +02:00
|
|
|
echo "$newState" | store_write "dagger/$(branch)" "$stateFile" "Write state for env $env"
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function fatal() {
|
|
|
|
echo >&2 "$@"
|
|
|
|
exit 1
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function store_read() {
|
|
|
|
local \
|
|
|
|
branch="$1" \
|
|
|
|
path="$2"
|
|
|
|
git cat-file -p "${branch}^{tree}:$path" 2>/dev/null || echo '{}'
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:52 +02:00
|
|
|
function store_stat() {
|
|
|
|
local \
|
|
|
|
branch="$1" \
|
|
|
|
path="$2"
|
|
|
|
git cat-file -p "${branch}^{tree}:$path" >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
function store_write() {
|
|
|
|
local \
|
|
|
|
branch="$1" \
|
|
|
|
path="$2" \
|
|
|
|
msg="${3:-write $path}"
|
|
|
|
blob="$(git hash-object -w --stdin)" \
|
|
|
|
idx="$(mktemp -d)/idx" \
|
|
|
|
oldcommit="$(git show-ref "refs/$branch" -s || true)"
|
|
|
|
# Load current state into index (will fail if state branch doesn't exist)
|
|
|
|
GIT_INDEX_FILE="$idx" git read-tree "refs/$branch" 2>&1 || true
|
|
|
|
GIT_INDEX_FILE="$idx" git \
|
|
|
|
update-index \
|
|
|
|
--add \
|
|
|
|
--cacheinfo 100644 "$blob" "$path"
|
|
|
|
# Create new tree object
|
|
|
|
local newtree=$(GIT_INDEX_FILE="$idx" git write-tree)
|
|
|
|
# Create new commit object
|
|
|
|
if [ -z "$oldcommit" ]; then
|
|
|
|
local newcommit="$(echo $msg | git commit-tree $newtree)"
|
|
|
|
else
|
|
|
|
local newcommit="$(echo $msg | git commit-tree $newtree -p $oldcommit)"
|
|
|
|
fi
|
|
|
|
git update-ref "refs/$branch" "$newcommit"
|
2021-04-29 00:27:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|