microsoft/TypeAgent
Publicmirrored from https://github.com/microsoft/TypeAgentAvailable
.devcontainer/scripts/start-devcontainer.sh
150lines · modecode
| 1 | #!/usr/bin/env bash |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # Licensed under the MIT License. |
| 4 | |
| 5 | set -euo pipefail |
| 6 | |
| 7 | SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) |
| 8 | REPO_ROOT=$(cd -- "$SCRIPT_DIR/../.." && pwd) |
| 9 | DEFAULT_WORKSPACE_FOLDER="$REPO_ROOT" |
| 10 | |
| 11 | usage() { |
| 12 | cat <<EOF |
| 13 | Usage: $(basename "$0") [options] |
| 14 | |
| 15 | Start the TypeAgent devcontainer. Optionally configure host SSH access. |
| 16 | |
| 17 | Options: |
| 18 | --workspace-folder PATH Workspace folder to open (default: repo root) |
| 19 | --config PATH Devcontainer config file (optional) |
| 20 | --recreate Recreate container before startup |
| 21 | --rebuild Rebuild image and recreate container before startup |
| 22 | --ssh After startup, run setup-ssh-access.sh |
| 23 | --insecure-local Pass through to setup-ssh-access.sh (implies --ssh) |
| 24 | -h, --help Show this help text |
| 25 | |
| 26 | Examples: |
| 27 | $(basename "$0") |
| 28 | $(basename "$0") --ssh |
| 29 | $(basename "$0") --recreate --ssh |
| 30 | $(basename "$0") --rebuild |
| 31 | $(basename "$0") --config .devcontainer/vnc/devcontainer.json |
| 32 | EOF |
| 33 | } |
| 34 | |
| 35 | log() { |
| 36 | printf '[start-devcontainer] %s\n' "$*" |
| 37 | } |
| 38 | |
| 39 | fail() { |
| 40 | printf '[start-devcontainer] Error: %s\n' "$*" >&2 |
| 41 | exit 1 |
| 42 | } |
| 43 | |
| 44 | read_git_identity() { |
| 45 | local key=$1 |
| 46 | git config --global --get "$key" 2>/dev/null || true |
| 47 | } |
| 48 | |
| 49 | WORKSPACE_FOLDER="$DEFAULT_WORKSPACE_FOLDER" |
| 50 | CONFIG_PATH="" |
| 51 | REMOVE_EXISTING=0 |
| 52 | REBUILD=0 |
| 53 | SETUP_SSH=0 |
| 54 | INSECURE_LOCAL=0 |
| 55 | |
| 56 | while [[ $# -gt 0 ]]; do |
| 57 | case "$1" in |
| 58 | --workspace-folder) |
| 59 | [[ $# -ge 2 ]] || fail "Missing value for $1" |
| 60 | WORKSPACE_FOLDER=$(cd -- "$2" && pwd) |
| 61 | shift 2 |
| 62 | ;; |
| 63 | --config) |
| 64 | [[ $# -ge 2 ]] || fail "Missing value for $1" |
| 65 | CONFIG_PATH=$(cd -- "$(dirname -- "$2")" && pwd)/$(basename -- "$2") |
| 66 | shift 2 |
| 67 | ;; |
| 68 | --recreate|--remove-existing-container) |
| 69 | REMOVE_EXISTING=1 |
| 70 | shift |
| 71 | ;; |
| 72 | --rebuild) |
| 73 | REMOVE_EXISTING=1 |
| 74 | REBUILD=1 |
| 75 | shift |
| 76 | ;; |
| 77 | --ssh) |
| 78 | SETUP_SSH=1 |
| 79 | shift |
| 80 | ;; |
| 81 | --insecure-local) |
| 82 | SETUP_SSH=1 |
| 83 | INSECURE_LOCAL=1 |
| 84 | shift |
| 85 | ;; |
| 86 | -h|--help) |
| 87 | usage |
| 88 | exit 0 |
| 89 | ;; |
| 90 | *) |
| 91 | fail "Unknown argument: $1" |
| 92 | ;; |
| 93 | esac |
| 94 | done |
| 95 | |
| 96 | if ! command -v docker >/dev/null 2>&1; then |
| 97 | fail "docker is required" |
| 98 | fi |
| 99 | |
| 100 | if command -v devcontainer >/dev/null 2>&1; then |
| 101 | DEVCONTAINER_CMD=(devcontainer) |
| 102 | else |
| 103 | DEVCONTAINER_CMD=(npx -y @devcontainers/cli) |
| 104 | fi |
| 105 | |
| 106 | HOST_GIT_USER_NAME=$(read_git_identity user.name) |
| 107 | HOST_GIT_USER_EMAIL=$(read_git_identity user.email) |
| 108 | |
| 109 | if [[ -n "$HOST_GIT_USER_NAME" ]]; then |
| 110 | export LOCAL_GIT_USER_NAME="$HOST_GIT_USER_NAME" |
| 111 | log "Using host git user.name from ~/.gitconfig" |
| 112 | fi |
| 113 | if [[ -n "$HOST_GIT_USER_EMAIL" ]]; then |
| 114 | export LOCAL_GIT_USER_EMAIL="$HOST_GIT_USER_EMAIL" |
| 115 | log "Using host git user.email from ~/.gitconfig" |
| 116 | fi |
| 117 | |
| 118 | UP_CMD=("${DEVCONTAINER_CMD[@]}" up --workspace-folder "$WORKSPACE_FOLDER") |
| 119 | if [[ -n "$CONFIG_PATH" ]]; then |
| 120 | UP_CMD+=(--config "$CONFIG_PATH") |
| 121 | fi |
| 122 | if [[ $REMOVE_EXISTING -eq 1 ]]; then |
| 123 | UP_CMD+=(--remove-existing-container) |
| 124 | fi |
| 125 | if [[ $REBUILD -eq 1 ]]; then |
| 126 | UP_CMD+=(--build-no-cache) |
| 127 | fi |
| 128 | |
| 129 | log "Starting devcontainer..." |
| 130 | "${UP_CMD[@]}" |
| 131 | |
| 132 | if [[ $SETUP_SSH -eq 1 ]]; then |
| 133 | SSH_SETUP_CMD=("$SCRIPT_DIR/setup-ssh-access.sh" --workspace-folder "$WORKSPACE_FOLDER") |
| 134 | if [[ -n "$CONFIG_PATH" ]]; then |
| 135 | SSH_SETUP_CMD+=(--config "$CONFIG_PATH") |
| 136 | fi |
| 137 | if [[ $INSECURE_LOCAL -eq 1 ]]; then |
| 138 | SSH_SETUP_CMD+=(--insecure-local) |
| 139 | fi |
| 140 | |
| 141 | log "Configuring SSH access..." |
| 142 | "${SSH_SETUP_CMD[@]}" |
| 143 | |
| 144 | printf '\nDone. Connect with:\n' |
| 145 | printf ' ssh typeagent-devcontainer\n' |
| 146 | else |
| 147 | printf '\nDone. Devcontainer is running.\n' |
| 148 | printf 'To set up host SSH access, re-run with --ssh, or invoke:\n' |
| 149 | printf ' %s/setup-ssh-access.sh\n' "$SCRIPT_DIR" |
| 150 | fi |