microsoft/TypeAgent

Public

mirrored from https://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b2831ffd45cbb4e4ea3ca955007cfb0cf4041765

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

.devcontainer/scripts/start-devcontainer.sh

150lines · modecode

1#!/usr/bin/env bash
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4
5set -euo pipefail
6
7SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
8REPO_ROOT=$(cd -- "$SCRIPT_DIR/../.." && pwd)
9DEFAULT_WORKSPACE_FOLDER="$REPO_ROOT"
10
11usage() {
12 cat <<EOF
13Usage: $(basename "$0") [options]
14
15Start the TypeAgent devcontainer. Optionally configure host SSH access.
16
17Options:
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
26Examples:
27 $(basename "$0")
28 $(basename "$0") --ssh
29 $(basename "$0") --recreate --ssh
30 $(basename "$0") --rebuild
31 $(basename "$0") --config .devcontainer/vnc/devcontainer.json
32EOF
33}
34
35log() {
36 printf '[start-devcontainer] %s\n' "$*"
37}
38
39fail() {
40 printf '[start-devcontainer] Error: %s\n' "$*" >&2
41 exit 1
42}
43
44read_git_identity() {
45 local key=$1
46 git config --global --get "$key" 2>/dev/null || true
47}
48
49WORKSPACE_FOLDER="$DEFAULT_WORKSPACE_FOLDER"
50CONFIG_PATH=""
51REMOVE_EXISTING=0
52REBUILD=0
53SETUP_SSH=0
54INSECURE_LOCAL=0
55
56while [[ $# -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
94done
95
96if ! command -v docker >/dev/null 2>&1; then
97 fail "docker is required"
98fi
99
100if command -v devcontainer >/dev/null 2>&1; then
101 DEVCONTAINER_CMD=(devcontainer)
102else
103 DEVCONTAINER_CMD=(npx -y @devcontainers/cli)
104fi
105
106HOST_GIT_USER_NAME=$(read_git_identity user.name)
107HOST_GIT_USER_EMAIL=$(read_git_identity user.email)
108
109if [[ -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"
112fi
113if [[ -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"
116fi
117
118UP_CMD=("${DEVCONTAINER_CMD[@]}" up --workspace-folder "$WORKSPACE_FOLDER")
119if [[ -n "$CONFIG_PATH" ]]; then
120 UP_CMD+=(--config "$CONFIG_PATH")
121fi
122if [[ $REMOVE_EXISTING -eq 1 ]]; then
123 UP_CMD+=(--remove-existing-container)
124fi
125if [[ $REBUILD -eq 1 ]]; then
126 UP_CMD+=(--build-no-cache)
127fi
128
129log "Starting devcontainer..."
130"${UP_CMD[@]}"
131
132if [[ $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'
146else
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"
150fi