Skip to content

Instantly share code, notes, and snippets.

@jungaretti
Last active July 20, 2022 23:57
Show Gist options
  • Save jungaretti/2bc3fa9f8f95fb197035f20c3c1b9fdd to your computer and use it in GitHub Desktop.
Save jungaretti/2bc3fa9f8f95fb197035f20c3c1b9fdd to your computer and use it in GitHub Desktop.
Configure your Codespaces settings with ease
ms_alias=""
cs_settings_file="$HOME/codespaces-settings.json"
# Uncomment the next two lines to load bash completion functions
# autoload -Uz +X compinit && compinit
# autoload -Uz +X bashcompinit && bashcompinit
cs_export() {
export VSCS_TARGET="$(jq -r '.vscsTarget // ""' $cs_settings_file)"
export VSCS_LOCATION="$(jq -r '.vscsRegion // ""' $cs_settings_file)"
# VSCS_TARGET_URL must be empty unless VSCS_TARGET is 'local'
export VSCS_TARGET_URL=""
if [ "$VSCS_TARGET" = 'local' ]; then
VSCS_TARGET_URL="$(jq -r '.vscsTargetUrl // ""' $cs_settings_file)"
fi
}
cs_print() {
local target="prod"
local location="default"
# VSCS_TARGET and VSCS_LOCATION are empty when using default settings
if [ -n "$VSCS_TARGET" ]; then
target="$VSCS_TARGET"
fi
if [ -n "$VSCS_LOCATION" ]; then
location="$VSCS_LOCATION"
fi
echo "Codespaces target: $target ($location)"
}
cs_update() {
local target="${1:-prod}"
if [ -z "$ms_alias" ]; then
echo "You must set your Microsoft alias before updating Codespaces settings"
return 1
fi
if [ ! -e "$cs_settings_file" ]; then
jq -n '{isInternal: true, logTelemetry: true}' >"$cs_settings_file"
echo "Created new settings file: $cs_settings_file"
fi
local vscs_target=""
local vscs_target_url=""
local vscs_region=""
case $target in
"local")
vscs_target="local"
vscs_target_url="https://codespaces.servicebus.windows.net/$ms_alias"
vscs_region="WestUs2"
;;
"dev")
vscs_target="development"
vscs_target_url="https://online.dev.core.vsengsaas.visualstudio.com"
vscs_region="WestUs2"
;;
"ppe")
vscs_target="ppe"
vscs_target_url="https://online-ppe.core.vsengsaas.visualstudio.com"
vscs_region="WestUs2"
;;
"prod")
# Use empty strings for default settings
;;
*)
echo "Unknown target: $target"
return 1
;;
esac
# Update Codespaces config file
local temp="$(mktemp)"
jq --arg target "$vscs_target" --arg targetUrl "$vscs_target_url" --arg region "$vscs_region" '. += {vscsTarget: $target, vscsTargetUrl: $targetUrl, vscsRegion: $region}' "$cs_settings_file" >"$temp"
mv "$temp" "$cs_settings_file"
# Update VSCS environment variables
cs_export
cat "$cs_settings_file"
}
export cs_update
if command -v complete &>/dev/null; then
complete -W "local dev ppe prod" cs_update
fi
alias config_cs="cs_update"
# Read existing Codespaces settings
if [ -e "$cs_settings_file" ]; then
cs_export && cs_print
fi

Getting Started

config_cs updates your Codespaces settings for both VS Code and the GitHub CLI. It takes one argument (local, dev, ppe, or prod), updates your ~/codespaces-settings.json file, and updates some environment variables. You can install config_cs by adding some code to your ~/.zshrc file.

config_cs officially supports zsh, the default shell on macOS. It appears to work with bash on macOS, but your mileage may vary.

Installation

To install config_cs for zsh on macOS, follow these steps:

  1. Append the contents of codespaces.sh to your ~/.zshrc
  2. Update ms_alias to match your Microsoft alias
  3. source ~/.zshrc or open a new terminal window

Install config_cs

Autocomplete

config_cs supports tab autocomplete! You need to install Oh My Zsh or uncomment two lines in your ~/.zshrc to enable autocomplete:

# Uncomment the next two lines to load bash completion functions
autoload -Uz +X compinit && compinit
autoload -Uz +X bashcompinit && bashcompinit

Usage

# Configure settings for your devstamp
config_cs local

# Configure settings for Dev
config_cs dev

# Configure settings for PPE
config_cs ppe

# Reset settings to defaults for Prod
config_cs

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment