-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_codespace_cfg
More file actions
executable file
·33 lines (26 loc) · 1.03 KB
/
Copy pathexport_codespace_cfg
File metadata and controls
executable file
·33 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
command -v gh >/dev/null 2>&1 || { echo "gh CLI required" >&2; exit 1; }
SSH_DIR="$HOME/.ssh"
CONFIG_FILE="$SSH_DIR/config"
BACKUP="$CONFIG_FILE.$(date +%Y%m%d%H%M%S).bak"
MANAGED_START="# >>> codespaces (managed) >>>"
MANAGED_END="# <<< codespaces (managed) <<<"
mkdir -p "$SSH_DIR"
[[ -f "$CONFIG_FILE" ]] && cp "$CONFIG_FILE" "$BACKUP"
echo "Generating Codespaces SSH config block..." >&2
TMP=$(mktemp)
gh codespace ssh --config | sed -E 's@ -- -i [^ ]*codespaces\.auto@@g; s@IdentityFile [^ ]*codespaces\.auto@@g' > "$TMP"
# Remove previous managed block if present
if grep -q "$MANAGED_START" "$CONFIG_FILE" 2>/dev/null; then
awk -v start="$MANAGED_START" -v end="$MANAGED_END" 'BEGIN{skip=0} {if($0==start){skip=1} else if($0==end){skip=0; next} if(!skip) print}' "$CONFIG_FILE" > "$CONFIG_FILE.tmp"
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
fi
{
echo "$MANAGED_START"
cat "$TMP"
echo "$MANAGED_END"
} >> "$CONFIG_FILE"
rm -f "$TMP"
echo "Updated $CONFIG_FILE (backup: $BACKUP)" >&2