Files

93 lines
2.2 KiB
Bash
Raw Permalink Normal View History

2026-05-25 03:01:46 +03:00
#!/bin/bash
# dnc-kitty-launcher - Launch kitty with container context
find_dnc_file() {
local dir="$PWD"
while [ "$dir" != "/" ]; do
if [ -f "$dir/.dnc" ]; then
echo "$dir/.dnc"
return
fi
dir="$(dirname "$dir")"
done
}
dnc_file=$(find_dnc_file)
if [ -z "$dnc_file" ]; then
echo "Error: .dnc file not found. Run 'dnc setup' first." >&2
exit 1
fi
container=""
dnc_dir="$(dirname "$dnc_file")"
while IFS= read -r line; do
case "$line" in
container*)
container="$(echo "$line" | cut -d= -f2 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
;;
esac
done < "$dnc_file"
if [ -z "$container" ]; then
echo "Error: container= not found in $dnc_file" >&2
exit 1
fi
container_status=$(docker inspect --format='{{.State.Status}}' "$container" 2>/dev/null || true)
if [ "$container_status" != "running" ]; then
echo "Starting container $container..." >&2
docker start "$container" >/dev/null
fi
tmpdir=$(mktemp -d -t dnc-kitty-XXXXXX)
cleanup() { rm -rf "$tmpdir"; }
trap cleanup EXIT
uid=$(id -u)
container_home=$(docker exec "$container" sh -c "
user_name=\$(getent passwd $uid | cut -d: -f1 2>/dev/null || echo '')
if [ -z \"\$user_name\" ]; then
echo /root
else
eval echo ~\$user_name
fi
")
config_src="$container_home/.config/kitty"
docker cp "$container:$config_src/." "$tmpdir/" 2>/dev/null || true
if [ -x "/opt/dnc/kitty/bin/kitty" ]; then
kitty="/opt/dnc/kitty/bin/kitty"
elif command -v kitty >/dev/null 2>&1; then
kitty="kitty"
else
echo "Error: kitty not found (not installed and no system kitty)" >&2
exit 1
fi
mkdir -p "$tmpdir"
if [ -x "/opt/dnc/libexec/dnc-exec" ]; then
dnc_exec="/opt/dnc/libexec/dnc-exec"
else
script_dir="$(cd "$(dirname "$0")" && pwd)"
dnc_exec="$script_dir/dnc-exec"
fi
if [ -f "$tmpdir/kitty.conf" ]; then
echo "" >> "$tmpdir/kitty.conf"
echo "# dnc: all tabs go through docker exec" >> "$tmpdir/kitty.conf"
else
:
fi
echo "shell $dnc_exec" >> "$tmpdir/kitty.conf"
export KITTY_CONFIG_DIRECTORY="$tmpdir"
export DNC_CONTAINER="$container"
cd "$dnc_dir" || exit 1
exec "$kitty" "$@"