Skip to content

Commit 71b695c

Browse files
aaajiaoclaude
andcommitted
refactor: thin wrapper pattern for Mac commands to enable auto-updates
Move complex command logic (update, config, telegram, sandbox-rebuild) from inline generated scripts into scripts/commands/*.sh. The generated ~/bin/openclaw-* wrappers now delegate to repo scripts via exec, so openclaw-update can self-update by git-pulling the orchestrator repo before executing. Simple commands (status, logs, restart, etc.) remain inline but now resolve VM name at runtime instead of baking it in. Also eliminates ~320 lines of duplicated wrapper generation from openclaw-orbstack-setup.sh by delegating Step 8 to refresh-mac-commands.sh. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bb99ff4 commit 71b695c

7 files changed

Lines changed: 345 additions & 557 deletions

File tree

openclaw-orbstack-setup.sh

Lines changed: 2 additions & 317 deletions
Original file line numberDiff line numberDiff line change
@@ -458,323 +458,8 @@ else
458458
warn "$MSG_WARN_GATEWAY_ISSUE"
459459
fi
460460

461-
# --- Create Mac convenience commands ---
462-
mkdir -p ~/bin
463-
464-
# Save language preference and VM name for generated commands
465-
cat > ~/bin/.openclaw-lang << LANG_EOF
466-
OPENCLAW_LANG=$OPENCLAW_LANG_CODE
467-
LANG_EOF
468-
469-
cat > ~/bin/.openclaw-vm << VM_EOF
470-
OPENCLAW_VM=$VM_NAME
471-
VM_EOF
472-
473-
cat > ~/bin/openclaw-status << EOF
474-
#!/bin/bash
475-
set -e
476-
orb -m "$VM_NAME" bash -c "openclaw gateway status"
477-
EOF
478-
479-
cat > ~/bin/openclaw-logs << EOF
480-
#!/bin/bash
481-
set -e
482-
orb -m "$VM_NAME" bash -c "openclaw logs --follow"
483-
EOF
484-
485-
cat > ~/bin/openclaw-restart << EOF
486-
#!/bin/bash
487-
set -e
488-
orb -m "$VM_NAME" bash -c "openclaw gateway restart"
489-
EOF
490-
491-
cat > ~/bin/openclaw-stop << EOF
492-
#!/bin/bash
493-
set -e
494-
orb -m "$VM_NAME" bash -c "openclaw gateway stop"
495-
EOF
496-
497-
cat > ~/bin/openclaw-start << EOF
498-
#!/bin/bash
499-
set -e
500-
orb -m "$VM_NAME" bash -c "openclaw gateway start"
501-
EOF
502-
503-
cat > ~/bin/openclaw-shell << EOF
504-
#!/bin/bash
505-
orb -m "$VM_NAME"
506-
EOF
507-
508-
# --- Helper: load language for commands ---
509-
_LANG_LOADER='
510-
# Load language
511-
_OPENCLAW_LANG="en"
512-
if [ -f "$HOME/bin/.openclaw-lang" ]; then source "$HOME/bin/.openclaw-lang"; _OPENCLAW_LANG="${OPENCLAW_LANG:-en}"; fi
513-
_SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")")/.." && pwd)"
514-
_LANG_FILE=""
515-
for _d in "$_SCRIPT_DIR/lang" "/usr/local/share/openclaw/lang" "$(dirname "$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")")/../lang"; do
516-
if [ -f "$_d/${_OPENCLAW_LANG}.sh" ]; then _LANG_FILE="$_d/${_OPENCLAW_LANG}.sh"; break; fi
517-
done
518-
if [ -n "$_LANG_FILE" ]; then source "$_LANG_FILE"; fi
519-
'
520-
521-
# openclaw (CLI passthrough) - no language needed
522-
cat > ~/bin/openclaw << EOF
523-
#!/bin/bash
524-
set -e
525-
if [ \$# -eq 0 ]; then
526-
set -- "--help"
527-
fi
528-
ARGS=\$(printf '%q ' "\$@")
529-
orb -m "$VM_NAME" bash -c "openclaw \$ARGS"
530-
EOF
531-
532-
# openclaw-config - needs language
533-
cat > ~/bin/openclaw-config << CMDEOF
534-
#!/bin/bash
535-
$_LANG_LOADER
536-
ACTION="\${1:-edit}"
537-
538-
case "\$ACTION" in
539-
edit)
540-
echo "\$MSG_CMD_CONFIG_OPENING"
541-
orb -m $VM_NAME bash -c "nano ~/.openclaw/openclaw.json 2>/dev/null || vi ~/.openclaw/openclaw.json"
542-
echo "\$MSG_CMD_CONFIG_SAVED"
543-
;;
544-
show)
545-
orb -m $VM_NAME bash -c "cat ~/.openclaw/openclaw.json"
546-
;;
547-
backup)
548-
BACKUP="openclaw-config-\$(date +%Y%m%d-%H%M%S).json"
549-
orb -m $VM_NAME bash -c "cat ~/.openclaw/openclaw.json" > "\$BACKUP"
550-
printf "\$MSG_CMD_CONFIG_BACKED_UP\n" "\$BACKUP"
551-
;;
552-
*)
553-
echo "\$MSG_CMD_CONFIG_USAGE"
554-
;;
555-
esac
556-
CMDEOF
557-
558-
# openclaw-update - needs language
559-
cat > ~/bin/openclaw-update << CMDEOF
560-
#!/bin/bash
561-
set -e
562-
$_LANG_LOADER
563-
564-
SANDBOX=false
565-
for arg in "\$@"; do
566-
case "\$arg" in
567-
--sandbox) SANDBOX=true ;;
568-
--help|-h)
569-
echo "\$MSG_CMD_UPDATE_USAGE"
570-
echo ""
571-
echo "\$MSG_CMD_UPDATE_DESC"
572-
echo ""
573-
echo "\$MSG_CMD_UPDATE_OPTIONS"
574-
echo "\$MSG_CMD_UPDATE_SANDBOX_OPT"
575-
echo ""
576-
echo "\$MSG_CMD_UPDATE_TIP"
577-
exit 0
578-
;;
579-
esac
580-
done
581-
582-
# Auto-detect stale system-level service and self-repair
583-
if grep -q "systemctl status openclaw" ~/bin/openclaw-status 2>/dev/null; then
584-
echo "\$MSG_UPDATE_AUTO_UPGRADE"
585-
# VM: migrate from system-level to user-level service
586-
orb -m $VM_NAME bash -c "sudo systemctl stop openclaw 2>/dev/null || true"
587-
orb -m $VM_NAME bash -c "sudo systemctl disable openclaw 2>/dev/null || true"
588-
orb -m $VM_NAME bash -c "sudo rm -f /etc/systemd/system/openclaw.service && sudo systemctl daemon-reload"
589-
orb -m $VM_NAME bash -c "sudo loginctl enable-linger \\\$(whoami)"
590-
orb -m $VM_NAME bash -c "systemctl --user enable openclaw-gateway.service 2>/dev/null || true"
591-
# Mac: fix stale commands
592-
cat > ~/bin/openclaw-status << 'FIXEOF'
593-
#!/bin/bash
594-
orb -m $VM_NAME bash -c "openclaw gateway status"
595-
FIXEOF
596-
cat > ~/bin/openclaw-logs << 'FIXEOF'
597-
#!/bin/bash
598-
orb -m $VM_NAME bash -c "openclaw logs --follow"
599-
FIXEOF
600-
cat > ~/bin/openclaw-restart << 'FIXEOF'
601-
#!/bin/bash
602-
orb -m $VM_NAME bash -c "openclaw gateway restart"
603-
FIXEOF
604-
cat > ~/bin/openclaw-stop << 'FIXEOF'
605-
#!/bin/bash
606-
orb -m $VM_NAME bash -c "openclaw gateway stop"
607-
FIXEOF
608-
cat > ~/bin/openclaw-start << 'FIXEOF'
609-
#!/bin/bash
610-
orb -m $VM_NAME bash -c "openclaw gateway start"
611-
FIXEOF
612-
chmod +x ~/bin/openclaw-status ~/bin/openclaw-logs ~/bin/openclaw-restart ~/bin/openclaw-stop ~/bin/openclaw-start
613-
echo "\$MSG_UPDATE_AUTO_UPGRADE_DONE"
614-
fi
615-
616-
# Ensure .env exists with at least Bonjour vars
617-
if ! orb -m $VM_NAME bash -c 'test -f ~/.openclaw/.env' 2>/dev/null; then
618-
orb -m $VM_NAME bash -c 'mkdir -p ~/.openclaw && printf "# OpenClaw Environment Variables\nOPENCLAW_DISABLE_BONJOUR=1\nCLAWDBOT_DISABLE_BONJOUR=1\n" > ~/.openclaw/.env && chmod 600 ~/.openclaw/.env'
619-
echo " \$MSG_UPDATE_ENV_CREATED"
620-
fi
621-
622-
echo "\$MSG_CMD_UPDATE_UPDATING"
623-
624-
echo "\$MSG_CMD_UPDATE_STOPPING"
625-
orb -m $VM_NAME bash -lc "openclaw gateway stop"
626-
627-
echo "\$MSG_CMD_UPDATE_PULLING"
628-
orb -m $VM_NAME bash -lc "cd ~/openclaw && git fetch --tags"
629-
LATEST_TAG=\$(orb -m $VM_NAME bash -lc "cd ~/openclaw && git tag -l 'v*' | grep -vE '-(beta|rc|alpha)' | sort -V | tail -1")
630-
echo " -> \$LATEST_TAG"
631-
orb -m $VM_NAME bash -lc "cd ~/openclaw && git checkout '\$LATEST_TAG'"
632-
633-
# Ensure pnpm is available (npm/corepack may vanish after apt upgrade)
634-
orb -m $VM_NAME bash -lc '
635-
if ! command -v pnpm &>/dev/null; then
636-
if ! command -v npm &>/dev/null; then
637-
echo " npm missing, reinstalling Node.js package..."
638-
sudo apt-get install --reinstall -y nodejs
639-
fi
640-
sudo corepack enable 2>/dev/null || sudo npm install -g pnpm
641-
fi
642-
'
643-
644-
echo "\$MSG_CMD_UPDATE_INSTALLING"
645-
orb -m $VM_NAME bash -lc "cd ~/openclaw && pnpm install"
646-
647-
echo "\$MSG_CMD_UPDATE_BUILDING"
648-
orb -m $VM_NAME bash -lc "cd ~/openclaw && pnpm build"
649-
650-
echo "\$MSG_CMD_UPDATE_UI"
651-
orb -m $VM_NAME bash -lc "cd ~/openclaw && pnpm ui:build"
652-
653-
echo "\$MSG_CMD_UPDATE_REINSTALL"
654-
orb -m $VM_NAME bash -lc "cd ~/openclaw && sudo npm install -g ."
655-
656-
# Auto-detect sandbox Dockerfile changes
657-
OLD_HASH=\$(orb -m $VM_NAME bash -lc "cat ~/.openclaw/.sandbox-build-hash 2>/dev/null || echo none")
658-
NEW_HASH=\$(orb -m $VM_NAME bash -lc "cd ~/openclaw && cat Dockerfile.sandbox Dockerfile.sandbox-browser scripts/sandbox-setup.sh scripts/sandbox-common-setup.sh scripts/sandbox-browser-setup.sh 2>/dev/null | sha256sum | cut -d' ' -f1")
659-
if [ "\$OLD_HASH" != "\$NEW_HASH" ]; then
660-
SANDBOX=true
661-
echo "\$MSG_CMD_UPDATE_SANDBOX_CHANGED"
662-
fi
663-
664-
if [ "\$SANDBOX" = true ]; then
665-
echo "\$MSG_CMD_UPDATE_SANDBOX_REBUILD"
666-
echo "\$MSG_CMD_UPDATE_SANDBOX_BASE"
667-
orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-setup.sh'" 2>/dev/null || true
668-
echo "\$MSG_CMD_UPDATE_SANDBOX_COMMON"
669-
orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-common-setup.sh'" 2>/dev/null || true
670-
echo "\$MSG_CMD_UPDATE_SANDBOX_BROWSER"
671-
orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-browser-setup.sh'" 2>/dev/null || true
672-
echo "\$MSG_CMD_UPDATE_SANDBOX_NOTE"
673-
# Save new sandbox build hash
674-
orb -m $VM_NAME bash -lc "cd ~/openclaw && cat Dockerfile.sandbox Dockerfile.sandbox-browser scripts/sandbox-setup.sh scripts/sandbox-common-setup.sh scripts/sandbox-browser-setup.sh 2>/dev/null | sha256sum | cut -d' ' -f1 > ~/.openclaw/.sandbox-build-hash"
675-
fi
676-
677-
echo "\$MSG_CMD_UPDATE_STARTING"
678-
orb -m $VM_NAME bash -lc "openclaw gateway start"
679-
680-
echo "\$MSG_CMD_UPDATE_DONE"
681-
if [ "\$SANDBOX" = false ]; then
682-
echo "\$MSG_CMD_UPDATE_SANDBOX_HINT"
683-
fi
684-
CMDEOF
685-
686-
# openclaw-sandbox-rebuild - needs language
687-
cat > ~/bin/openclaw-sandbox-rebuild << CMDEOF
688-
#!/bin/bash
689-
set -e
690-
$_LANG_LOADER
691-
692-
echo "\$MSG_CMD_REBUILD_START"
693-
694-
echo "\$MSG_CMD_REBUILD_BASE"
695-
if orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-setup.sh'" 2>/dev/null; then
696-
echo "\$MSG_CMD_REBUILD_BASE_OK"
697-
elif orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c 'docker build -t openclaw-sandbox:bookworm-slim -f Dockerfile.sandbox .'" 2>/dev/null; then
698-
echo "\$MSG_CMD_REBUILD_BASE_OK_DF"
699-
else
700-
echo "\$MSG_CMD_REBUILD_BASE_FAIL"
701-
fi
702-
703-
echo "\$MSG_CMD_REBUILD_COMMON"
704-
if orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-common-setup.sh'" 2>/dev/null; then
705-
echo "\$MSG_CMD_REBUILD_COMMON_OK"
706-
else
707-
echo "\$MSG_CMD_REBUILD_COMMON_FAIL"
708-
fi
709-
710-
echo "\$MSG_CMD_REBUILD_BROWSER"
711-
if orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c './scripts/sandbox-browser-setup.sh'" 2>/dev/null; then
712-
echo "\$MSG_CMD_REBUILD_BROWSER_OK"
713-
elif orb -m $VM_NAME bash -lc "cd ~/openclaw && sg docker -c 'docker build -t openclaw-sandbox-browser:bookworm-slim -f Dockerfile.sandbox-browser .'" 2>/dev/null; then
714-
echo "\$MSG_CMD_REBUILD_BROWSER_OK_DF"
715-
else
716-
echo "\$MSG_CMD_REBUILD_BROWSER_FAIL"
717-
fi
718-
719-
# Save new sandbox build hash
720-
orb -m $VM_NAME bash -lc "cd ~/openclaw && cat Dockerfile.sandbox Dockerfile.sandbox-browser scripts/sandbox-setup.sh scripts/sandbox-common-setup.sh scripts/sandbox-browser-setup.sh 2>/dev/null | sha256sum | cut -d' ' -f1 > ~/.openclaw/.sandbox-build-hash"
721-
722-
echo ""
723-
echo "\$MSG_CMD_REBUILD_DONE"
724-
echo "\$MSG_CMD_REBUILD_NOTE"
725-
CMDEOF
726-
727-
# openclaw-telegram - needs language
728-
cat > ~/bin/openclaw-telegram << CMDEOF
729-
#!/bin/bash
730-
$_LANG_LOADER
731-
ACTION="\${1:-help}"
732-
733-
case "\$ACTION" in
734-
add)
735-
if [ -z "\$2" ]; then
736-
echo "\$MSG_CMD_TG_ADD_USAGE"
737-
echo "\$MSG_CMD_TG_ADD_HINT"
738-
exit 1
739-
fi
740-
orb -m $VM_NAME bash -c "openclaw channels add --channel telegram --token \$(printf '%q' "\$2")"
741-
;;
742-
approve)
743-
if [ -z "\$2" ]; then
744-
echo "\$MSG_CMD_TG_APPROVE_USAGE"
745-
echo "\$MSG_CMD_TG_APPROVE_HINT"
746-
exit 1
747-
fi
748-
orb -m $VM_NAME bash -c "openclaw pairing approve telegram \$(printf '%q' "\$2")"
749-
;;
750-
*)
751-
echo "\$MSG_CMD_TG_TITLE"
752-
echo ""
753-
echo "\$MSG_CMD_TG_USAGE"
754-
echo "\$MSG_CMD_TG_ADD_DESC"
755-
echo "\$MSG_CMD_TG_APPROVE_DESC"
756-
echo ""
757-
echo "\$MSG_CMD_TG_ALT"
758-
echo "\$MSG_CMD_TG_ALT_CMD"
759-
;;
760-
esac
761-
CMDEOF
762-
763-
cat > ~/bin/openclaw-doctor << EOF
764-
#!/bin/bash
765-
set -e
766-
ARGS=\$(printf '%q ' "\$@")
767-
orb -m "$VM_NAME" bash -c "openclaw doctor \$ARGS"
768-
EOF
769-
770-
cat > ~/bin/openclaw-whatsapp << EOF
771-
#!/bin/bash
772-
set -e
773-
orb -m "$VM_NAME" bash -c "openclaw channels login --channel whatsapp"
774-
EOF
775-
776-
chmod +x ~/bin/openclaw-*
777-
chmod +x ~/bin/openclaw
461+
# --- Create Mac convenience commands (delegate to refresh-mac-commands.sh) ---
462+
OPENCLAW_LANG="$OPENCLAW_LANG_CODE" OPENCLAW_VM_NAME="$VM_NAME" bash "$SCRIPT_DIR/scripts/refresh-mac-commands.sh"
778463
ok "$MSG_OK_COMMANDS_CREATED"
779464

780465
# --- Write default sandbox configuration ---

scripts/commands/_common.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
# Shared loader for openclaw command scripts
3+
# Sources VM name and language strings
4+
# Usage: source "$(dirname "$0")/_common.sh"
5+
6+
set -e
7+
8+
# Resolve repo root (scripts/commands/ -> repo root)
9+
OPENCLAW_REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
10+
11+
# Dynamic VM_NAME resolution (not hardcoded at generation time)
12+
OPENCLAW_VM_NAME="${OPENCLAW_VM_NAME:-openclaw-vm}"
13+
if [ -f "$HOME/bin/.openclaw-vm" ]; then
14+
# shellcheck disable=SC1091
15+
source "$HOME/bin/.openclaw-vm"
16+
OPENCLAW_VM_NAME="${OPENCLAW_VM:-$OPENCLAW_VM_NAME}"
17+
fi
18+
19+
# Language loading
20+
_OPENCLAW_LANG="en"
21+
if [ -f "$HOME/bin/.openclaw-lang" ]; then
22+
# shellcheck disable=SC1091
23+
source "$HOME/bin/.openclaw-lang"
24+
_OPENCLAW_LANG="${OPENCLAW_LANG:-en}"
25+
fi
26+
_LANG_FILE="$OPENCLAW_REPO_DIR/lang/${_OPENCLAW_LANG}.sh"
27+
if [ -f "$_LANG_FILE" ]; then
28+
# shellcheck disable=SC1090
29+
source "$_LANG_FILE"
30+
else
31+
# Fallback to English
32+
# shellcheck disable=SC1091
33+
source "$OPENCLAW_REPO_DIR/lang/en.sh"
34+
fi

scripts/commands/config.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# openclaw-config: Edit/show/backup OpenClaw configuration
3+
# Called via thin wrapper: ~/bin/openclaw-config -> this script
4+
5+
# shellcheck source=_common.sh
6+
source "$(dirname "$0")/_common.sh"
7+
8+
ACTION="${1:-edit}"
9+
10+
case "$ACTION" in
11+
edit)
12+
echo "$MSG_CMD_CONFIG_OPENING"
13+
orb -m "$OPENCLAW_VM_NAME" bash -lc "nano ~/.openclaw/openclaw.json 2>/dev/null || vi ~/.openclaw/openclaw.json"
14+
echo "$MSG_CMD_CONFIG_SAVED"
15+
;;
16+
show)
17+
orb -m "$OPENCLAW_VM_NAME" bash -lc "cat ~/.openclaw/openclaw.json"
18+
;;
19+
backup)
20+
BACKUP="openclaw-config-$(date +%Y%m%d-%H%M%S).json"
21+
orb -m "$OPENCLAW_VM_NAME" bash -lc "cat ~/.openclaw/openclaw.json" > "$BACKUP"
22+
# shellcheck disable=SC2059
23+
printf "$MSG_CMD_CONFIG_BACKED_UP\n" "$BACKUP"
24+
;;
25+
*)
26+
echo "$MSG_CMD_CONFIG_USAGE"
27+
;;
28+
esac

0 commit comments

Comments
 (0)