Overlay: validated manifest with dest record, ownership checks, clean reinstall
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QKxGW1raAWaqeU8WdHsMsp
This commit is contained in:
@@ -10,26 +10,48 @@ SRC=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
DEST="${OMARCHY_PATH:?OMARCHY_PATH must point at the target Omarchy install}"
|
||||
MANIFEST="$HOME/.local/state/omarchycn/overlay-manifest"
|
||||
|
||||
run_dest() {
|
||||
if [[ -w $DEST ]]; then
|
||||
run_in() {
|
||||
local root="$1"
|
||||
shift
|
||||
if [[ -w $root ]]; then
|
||||
"$@"
|
||||
else
|
||||
sudo "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
valid_entry() {
|
||||
[[ $1 == "cn" || $1 =~ ^bin/(omarchycn|omarchy-cn-[a-z0-9-]+)$ ]]
|
||||
}
|
||||
|
||||
uninstall() {
|
||||
if [[ ! -f $MANIFEST ]]; then
|
||||
echo "No overlay manifest at $MANIFEST" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local dest="" rel=""
|
||||
dest=$(grep -m1 '^dest=' "$MANIFEST" | cut -d= -f2-)
|
||||
if [[ -z $dest || ! -d $dest ]]; then
|
||||
echo "Manifest has no valid dest= record" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
while IFS= read -r rel; do
|
||||
[[ $rel == source=* ]] && continue
|
||||
run_dest rm -f "$DEST/$rel"
|
||||
[[ $rel == source=* || $rel == dest=* ]] && continue
|
||||
if ! valid_entry "$rel"; then
|
||||
echo "Refusing suspicious manifest entry: $rel" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ $rel == "cn" ]]; then
|
||||
run_in "$dest" rm -rf "$dest/cn"
|
||||
else
|
||||
run_in "$dest" rm -f "$dest/$rel"
|
||||
fi
|
||||
done < "$MANIFEST"
|
||||
run_dest rm -rf "$DEST/cn"
|
||||
|
||||
rm -f "$MANIFEST"
|
||||
echo "OmarchyCN overlay removed from $DEST"
|
||||
echo "OmarchyCN overlay removed from $dest"
|
||||
}
|
||||
|
||||
install() {
|
||||
@@ -42,20 +64,41 @@ install() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "${MANIFEST%/*}"
|
||||
echo "source=$SRC" > "$MANIFEST"
|
||||
# Reinstall: remove the previous overlay first so upgrades leave no residue
|
||||
if [[ -f $MANIFEST ]]; then
|
||||
uninstall
|
||||
fi
|
||||
|
||||
run_dest mkdir -p "$DEST/cn"
|
||||
run_dest cp -r "$SRC/cn/." "$DEST/cn/"
|
||||
if [[ -e $DEST/cn ]]; then
|
||||
echo "Refusing to take over unmanaged $DEST/cn" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local f rel
|
||||
local f rel entries=()
|
||||
for f in "$SRC/bin/omarchycn" "$SRC"/bin/omarchy-cn-*; do
|
||||
rel="bin/${f##*/}"
|
||||
run_dest cp "$f" "$DEST/$rel"
|
||||
echo "$rel" >> "$MANIFEST"
|
||||
if [[ -e $DEST/$rel ]]; then
|
||||
echo "Refusing to overwrite unmanaged $DEST/$rel" >&2
|
||||
exit 1
|
||||
fi
|
||||
entries+=("$rel")
|
||||
done
|
||||
|
||||
echo "OmarchyCN overlay installed into $DEST ($(grep -c '^bin/' "$MANIFEST") commands)"
|
||||
mkdir -p "${MANIFEST%/*}"
|
||||
{
|
||||
echo "source=$SRC"
|
||||
echo "dest=$DEST"
|
||||
echo "cn"
|
||||
printf '%s\n' "${entries[@]}"
|
||||
} > "$MANIFEST.tmp"
|
||||
|
||||
run_in "$DEST" cp -r "$SRC/cn" "$DEST/cn"
|
||||
for rel in "${entries[@]}"; do
|
||||
run_in "$DEST" cp "$SRC/$rel" "$DEST/$rel"
|
||||
done
|
||||
|
||||
mv "$MANIFEST.tmp" "$MANIFEST"
|
||||
echo "OmarchyCN overlay installed into $DEST (${#entries[@]} commands)"
|
||||
echo "Verify: omarchy cn version"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user