diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 3d993bd..b8888e8 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,8 +8,8 @@ "source=go-dev-container-bashhistory,target=/commandhistory,type=volume", "source=go-dev-container-plugins,target=/home/vscode/.vscode-server/extensions,type=volume", ///// The following should be the same for all dev containers using this image. The post_start_command.sh relies on these. - // Your host should have a .docker directory set up and you should be logged into any Artifactory - // repositories you need access to in the dev container. Logging into a repo inside the dev container will not stick. + // Your host should have a .docker directory set up and you should be logged into any repos you need access to. + // Logging into a repo inside the dev container will not stick. "source=${localEnv:HOME}/.docker,target=/home/vscode/.docker-localhost,type=bind", // If you will be working with Kubernetes you should have a configured kube config file in your .kube directory. "source=${localEnv:HOME}/.kube,target=/home/vscode/.kube-localhost,type=bind", diff --git a/.dockerignore b/.dockerignore index 1f57d97..eb159ca 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,4 +4,5 @@ cspell.sh dev.sh Dockerfile README.md +images/** run.sh \ No newline at end of file diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 03f9df8..7c74ab7 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -18,7 +18,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden Runner - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf + uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 with: disable-sudo: true egress-policy: block @@ -31,4 +31,4 @@ jobs: uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 - name: 'Dependency Review' - uses: actions/dependency-review-action@ce3cf9537a52e8119d91fd484ab5b8a807627bf8 + uses: actions/dependency-review-action@da24556b548a50705dd671f47852072ea4c105d9 diff --git a/.github/workflows/release-weekly.yml b/.github/workflows/release-weekly.yml index db0a29d..43ebab1 100644 --- a/.github/workflows/release-weekly.yml +++ b/.github/workflows/release-weekly.yml @@ -28,7 +28,7 @@ jobs: packages: write steps: - name: Harden Runner - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf + uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 with: disable-sudo: true egress-policy: audit @@ -56,7 +56,7 @@ jobs: - name: Build and Push Docker image id: build - uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 with: push: true tags: ${{ env.TAG_MAJOR }},${{ env.TAG_MINOR }},${{ env.TAG_PATCH }},${{ env.TAG_LATEST }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 01bf340..fb6a33e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,7 +21,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf + uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 with: disable-sudo: true egress-policy: audit @@ -51,7 +51,7 @@ jobs: type=semver,pattern={{major}} - name: Build and Push Docker Image - uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 with: context: . push: true diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 58848c2..48cea53 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -34,7 +34,7 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf + uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 with: disable-sudo: true egress-policy: block @@ -56,7 +56,7 @@ jobs: persist-credentials: false - name: "Run Analysis" - uses: ossf/scorecard-action@f49aabe0b5af0936a0987cfb85d86b75731b0186 + uses: ossf/scorecard-action@05b42c624433fc40578a4040d5cf5e36ddca8cde with: results_file: results.sarif results_format: sarif @@ -87,6 +87,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard (optional). # Commenting out will disable upload of results to your repo's Code Scanning dashboard - name: "Upload to Code-Scanning" - uses: github/codeql-action/upload-sarif@45775bd8235c68ba998cffa5171334d58593da47 + uses: github/codeql-action/upload-sarif@181d5eefc20863364f96762470ba6f862bdef56b with: sarif_file: results.sarif diff --git a/.github/workflows/trivy.yml b/.github/workflows/trivy.yml index 8bcf358..76dccff 100644 --- a/.github/workflows/trivy.yml +++ b/.github/workflows/trivy.yml @@ -41,7 +41,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden Runner - uses: step-security/harden-runner@c6295a65d1254861815972266d5933fd6e532bdf + uses: step-security/harden-runner@6c439dc8bdf85cadbbce9ed30d1c7b959517bc49 with: disable-sudo: true egress-policy: audit @@ -57,7 +57,7 @@ jobs: uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 - name: Build Docker Image - uses: docker/build-push-action@471d1dc4e07e5cdedd4c2171150001c434f0b7a4 + uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 with: push: false tags: ${{ env.REGISTRY }}/${{ env.REPOSITORY }}:${{ github.sha }} @@ -75,7 +75,7 @@ jobs: TRIVY_SKIP_JAVA_DB_UPDATE: true - name: Upload Trivy Results - uses: github/codeql-action/upload-sarif@45775bd8235c68ba998cffa5171334d58593da47 + uses: github/codeql-action/upload-sarif@181d5eefc20863364f96762470ba6f862bdef56b with: sarif_file: 'trivy-results.sarif' diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d8562a4..36fbcee 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/gitleaks/gitleaks - rev: v8.16.3 + rev: v8.27.2 hooks: - id: gitleaks - repo: https://github.com/jumanjihouse/pre-commit-hooks @@ -8,7 +8,7 @@ repos: hooks: - id: shellcheck - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v5.0.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace diff --git a/Dockerfile b/Dockerfile index 4c80225..6525347 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,10 @@ # https://hub.docker.com/r/jdxcode/mise/tags # This is latest. -FROM jdxcode/mise@sha256:f6a96c629ed75c908540acda407194e45544d10e982744b1f02fe78892aaa804 AS mise +FROM jdxcode/mise@sha256:5a4eb0b7b6687f2f42ad24d37a50e605b6064d4cd051edb3286a30311539715c AS mise # https://hub.docker.com/r/rockylinux/rockylinux/tags -FROM rockylinux/rockylinux:9-ubil@sha256:a475955a742ea1e3f83d5765aec206804a714c9b654d17b691c9c17a5da02c8f AS final +FROM rockylinux/rockylinux:10-ubi@sha256:eca03145dd5e0b2a281eef164d391e4758b4a5962d29b688d15a72cef712fbb4 AS final LABEL org.opencontainers.image.source=https://github.com/sarg3nt/go-dev-container diff --git a/README.md b/README.md index 5243a44..fa965e6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![Dependabot Updates](https://github.com/sarg3nt/go-dev-container/actions/workflows/dependabot/dependabot-updates/badge.svg)](https://github.com/sarg3nt/go-dev-container/actions/workflows/dependabot/dependabot-updates) [![Dependency Review](https://github.com/sarg3nt/go-dev-container/actions/workflows/dependency-review.yml/badge.svg)](https://github.com/sarg3nt/go-dev-container/actions/workflows/dependency-review.yml) **** -A Go Dev Container using `mise` to install Go and other convenient tools. +A Go Dev Container using [mise](https://mise.jdx.dev/) to install Go and other convenient tools. `mise` can then be used to install various other Go versions as needed. - [Using the Go Dev Container](#using-the-go-dev-container) @@ -60,33 +60,37 @@ See the base `mise` config file at `home/vscode/.config/mise/config.toml` for al ## Using `mise` to Manage Go Versions +[mise](https://mise.jdx.dev/) is a development environment setup tool based on `asdf` but much faster. +See the [mise about](https://mise.jdx.dev/about.html) page for more information. + 1. Copy the `.mise.toml` file from the root of this repo to your project's root and modify it as needed. 1. The provided `.devcontainer` will automatically call `mise install` to install the custom versions of the applications. 1. After the container is started and you exec into it, `mise` will automatically install the app versions listed in the `.mise.toml` file. 1. Other applications can also be installed by editing the `.mise.toml` file. - Run `mise help` for examples. + Run `mise help` for examples. ## Included `.devcontainer` Config This repository also includes an example on how to use the built Go dev container. -Do the following to use this example. +Do the following to use this example: -1. Clone down the repository. +1. Clone the repository: `git clone https://github.com/sarg3nt/go-dev-container.git` -2. Ensure your target project does not already have a `.devcontainer` directory. If it does, you will either need to rename it for testing or delete it. +2. Ensure your target project does not already have a `.devcontainer` directory. If it does, you will either need to rename it for testing or delete it. 3. Copy the `.devcontainer` directory to your project. 4. Copy the following files to the root of your project. - These are optional but encouraged. - - `.mise.toml`: The config file for managing specific versions of tooling you need for your Go project. - - `cspell.json`: The cspell config for spell checking in your project, edit to add any specific words that your project needs. - - `dev.sh`: Helps launch VSCode and exec into the dev container. This file needs some modification to use in your repository. See [dev.sh](#devsh) for instructions. + These are optional but encouraged: + - `.mise.toml`: The config file for managing specific versions of tooling you need for your Go project. + - `cspell.json`: The cspell config for spell checking in your project; edit to add any specific words that your project needs. + - `dev.sh`: Helps launch VSCode and exec into the dev container. This file needs some modification to use in your repository. See [`dev.sh`](#devsh) for instructions. -We've included an `install.sh` script to automate the process of copying the above files into your project directory. +We've included an `install.sh` script to automate the process of copying the above files into your project directory. -The script must be ran from the root of the `go-dev-container` project. +The script must be run from the root of the `go-dev-container` project. -**Example:** `./install.sh ~/src/my-go-project` +> **Example:** +> `./install.sh ~/src/my-go-project` ### Dev Container Setup @@ -95,31 +99,32 @@ Once you've followed the above instructions to copy the needed files to your pro Edit the `devcontainer.json` file to make the following changes. - Change `name` from `go-dev-container` to the name of your project. -- Change `--name` in `runargs` from `go-dev-container` to the name of your project. +- Change `--name` in `runargs` from `go-dev-container` to the name of your project. - Change `source=go-dev-container-bashhistory` to `source=-bashhistory` - Change `source=go-dev-container-plugins` to `source=-plugins` -> **NOTE:** You may be tempted to find and replace `go-dev-container` with the name of your project, however the `image` for the container is called `go-dev-container` so that would break things. If you want to do a controlled replacement you can, just be careful not to replace that one line. +> [!NOTE] +> You may be tempted to find and replace `go-dev-container` with the name of your project; however, the `image` for the container is called `go-dev-container` so that would break things. If you want to do a controlled replacement you can, just be careful not to replace that one line. ### `dev.sh` -This script is used to easily start VSCode and exec into the Dev Container from the terminal that it is ran from. This frees the developer from having to use the VSCode integrated terminal. +This script is used to easily start VSCode and exec into the Dev Container from the terminal from which it is run. +This frees the developer from having to use the VSCode integrated terminal. You can run `dev.sh` in multiple terminals once VSCode is running and the container has started to easily exec into it. -- Open the `dev.sh` file and set a `docker_exec_command` if desired, this is optional but if this repo is used a lot, it is a nice to have. This will create a command in the users `.bashrc` and `.zshrc` to quickly exec into this running dev container. +- Open the `dev.sh` file and set a `docker_exec_command` if desired; this is optional but if this repo is used frequently, it is a nice convenience. This will create a command in the user's `.bashrc` and `.zshrc` to quickly exec into this running dev container. - Change `project_name` to match the name of the repository. - **Example:** If your root project repository is called `my-go-project` then set `project_name` to `my-go-project` - -To use the `./dev.sh` script, simply run it, then when VS Code opens, there should be a prompt at the bottom right of the editor saying "Folder contains a Dev Container . . .". Click the "Reopen in Container" button and VS Code will open the dev container and attach to it. + **Example:** If your root project repository is called `my-go-project`, then set `project_name` to `my-go-project`. -Reopen in Container +To use the `./dev.sh` script, simply run it. It will automatically open the dev container once VSCode starts. -> **NOTE:** If you have not opened the dev container before or if it has been updated it will download the container from Github, which can take a while. +> [!NOTE] +> If you have not opened the dev container before or if it has been updated, it will download the container from GitHub, which can take a while. -## Starship +## Starship -Starship is a custom Power Line command prompt we include. +Starship is a custom PowerLine command prompt we include. ![Starship Prompt](images/starship-prompt.png) @@ -129,55 +134,56 @@ See [Initial Workstation Setup](#initial-workstation-setup) for instructions on ## Initial Workstation Setup -Instructions to set up your workstation. -For more information on Dev Containers check out the [official docs](https://code.visualstudio.com/docs/devcontainers/containers). +Instructions to set up your workstation. +For more information on Dev Containers, check out the [official docs](https://code.visualstudio.com/docs/devcontainers/containers). ### WSL on Windows -1. If you will be building Docker containers in Windows, then install Docker Desktop for Windows following [Docker's instructions](https://docs.docker.com/desktop/install/windows-install/). If you do not need Docker for Windows support then you can [directly install Docker inside of Ubuntu](https://docs.docker.com/engine/install/ubuntu/) **AFTER** you install WSL and Ubuntu in the following steps. +1. If you will be building Docker containers in Windows, then install Docker Desktop for Windows following [Docker's instructions](https://docs.docker.com/desktop/install/windows-install/). If you do not need Docker for Windows support, then you can [directly install Docker inside of Ubuntu](https://docs.docker.com/engine/install/ubuntu/) **AFTER** you install WSL and Ubuntu in the following steps. 1. Install VS Code from the [Visual Studio Code website](https://code.visualstudio.com/download) or from the Microsoft Store. -1. Open VS Code and click on the "Extensions" button to the left. +1. Open VS Code and click on the "Extensions" button on the left. 1. Search for "Dev Containers" and install it. 1. Search for "WSL" and install it. -1. WSL is the Windows Subsystem for Linux and facilitates the use of a Linux distruction on Windows. -Follow the [Microsoft insructions](https://learn.microsoft.com/en-us/windows/wsl/install) to install WSL and a Linux distribution. Ubuntu is probably the most commong Linux OS used in WSL, but feel free to choose what you want. Note that the dev container itself uses Rocky Linux but this shouldn't matter to you during use. +1. WSL is the Windows Subsystem for Linux and facilitates the use of a Linux distribution on Windows. + Follow the [Microsoft instructions](https://learn.microsoft.com/en-us/windows/wsl/install) to install WSL and a Linux distribution. Ubuntu is probably the most common Linux OS used in WSL, but feel free to choose what you want. Note that the dev container itself uses Rocky Linux, but this shouldn't matter to you during use. ### Windows Font Install -To get the full functionality of font ligatures and icons for the Starship prompt you will need to install a [Nerd Font](https://www.nerdfonts.com/) from [Nerd Fonts Downloads](https://www.nerdfonts.com/font-downloads). If you skip this step the Dev Container terminal command line will look weird and not have icons thus making it harder to read. +To get the full functionality of font ligatures and icons for the Starship prompt, you will need to install a [Nerd Font](https://www.nerdfonts.com/) from [Nerd Fonts Downloads](https://www.nerdfonts.com/font-downloads). If you skip this step, the Dev Container terminal command line will look weird and not have icons, thus making it harder to read. -Many of us use [FiraCode Nerd Font](https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraCode.zip) or [FiraMono Nerd Font](https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraMono.zip) but you can [preview](https://www.programmingfonts.org/#firacode) any of the fonts and choose which one is best for you. +Many of us use [FiraCode Nerd Font](https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraCode.zip) or [FiraMono Nerd Font](https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/FiraMono.zip), but you can [preview](https://www.programmingfonts.org/#firacode) any of the fonts and choose which one is best for you. -Download your chosen font and [install it in Windows](https://support.microsoft.com/en-us/office/add-a-font-b7c5f17c-4426-4b53-967f-455339c564c1) then proceed to the next step. +Download your chosen font and [install it in Windows](https://support.microsoft.com/en-us/office/add-a-font-b7c5f17c-4426-4b53-967f-455339c564c1), then proceed to the next step. #### Windows Terminal Font Setup -1. Open Windows Terminal, select the menu chevron to the right of the last tab and select settings. -1. On the left select `Profiles` --> `Defaults` -1. Under `Additional Settings` select `Appearance` -1. Under `Font Face` select the name of the font you downloaded, for example if you chose the "Firacode Nerd Font" then you'd choose `Firacode NF` You may need to check `Show all items` or restart Windows Terminal to see the new fonts. +1. Open Windows Terminal, select the menu chevron to the right of the last tab, and select settings. +1. On the left, select `Profiles` --> `Defaults` +1. Under `Additional Settings`, select `Appearance` +1. Under `Font Face`, select the name of the font you downloaded. For example, if you chose the "Firacode Nerd Font", then you'd choose `Firacode NF`. You may need to check `Show all items` or restart Windows Terminal to see the new fonts. ### Linux Font Install +This is not necessary when running VSCode in WSL on Windows, however if you are running things natively in Linux you will need to install the fonts in Linux. + See [How to Install New (or Nerd) Fonts on Linux (Ultimate Guide)](https://linuxtldr.com/install-fonts-on-linux/) for instructions. ### Visual Studio Code Font Setup -The fonts must be installed in your operating system to be used in VSCode. If you are running VSCode on Windows and using it in Linux inside of WSL you would still install the fonts in Windows. +The fonts must be installed in your operating system to be used in VSCode. If you are running VSCode on Windows and using it in Linux inside of WSL, you would still install the fonts in Windows. 1. Select `File` --> `Preferences` --> `Settings` 1. Expand `Text Editor` --> select `Font` -1. In the `Font Family` text box paste the following: - - > **NOTE:** This assumes you chose "FiraCode NF", if not, replace the first font name with the name of the font you installed in Windows. - +1. In the `Font Family` text box, paste the following: ``` 'FiraCode NF', 'CaskaydiaCove NF', Consolas, 'Courier New', monospace ``` +> [!NOTE] +> This assumes you chose "FiraCode NF". If not, replace the first font name with the name of the font you installed in Windows. ## Contributions -Contributions are welcome via the standard Github Fork --> PR process. +Contributions are welcome via the standard GitHub Fork --> PR process. ## Author diff --git a/dev.sh b/dev.sh index a642766..e20b3c5 100755 --- a/dev.sh +++ b/dev.sh @@ -2,79 +2,109 @@ # # This script will start the dev container and open an interactive prompt into it. # +# cSpell:ignore set -euo pipefail IFS=$'\n\t' +# What quick two letter command do we want to use for this dev container / project. +# If this is empty, no command will be installed. +# Example: qq +docker_exec_command="" +# Name of the project folder +project_name="go-dev-container" +# Name of the container +container_name="${project_name}" +# User being created in the container +container_user="vscode" + +colors_sourced=false +# shellcheck source=/dev/null +if [ -f "usr/bin/lib/sh/colors.sh" ]; then + source "usr/bin/lib/sh/colors.sh" + colors_sourced=true +fi # shellcheck source=/dev/null -source "usr/bin/lib/sh/colors.sh" +if [ -f "lib/sh/colors.sh" ]; then + source "lib/sh/colors.sh" + colors_sourced=true +fi -# Check if user is using Docker Deskop for Windows or the native Docker Engine. -main() { - # What quick two letter command do we want to use for this dev container / project. - # If this is empty, no command will be installed. - # Example: qq - local docker_exec_command="gdc" - # Name of the project folder - local project_name="go-dev-container" - # Name of the container. This assumes you are using the same name as the project folder in the devcontainer.json file 'runargs' section. - local container_name="${project_name}" - # User being created in the container - local container_user="vscode" - - local is_windows_docker=true - if [[ $(docker version) != *"Server: Docker Desktop"* ]]; then - is_windows_docker=false - fi +if [ "$colors_sourced" = false ]; then + echo "Error: colors.sh not found. Please ensure it is availing in either the usr/bin/lib/sh or lib/sh directory." + exit 1 +fi +add_docker_exec_command() { if [[ -n "$docker_exec_command" ]]; then # Add ${docker_exec_command} command to open the dev container to a users .bashrc file if it is not already there. if ! grep -F "${docker_exec_command} ()" "${HOME}/.bashrc" >/dev/null 2>&1 && [ -f "${HOME}/.bashrc" ]; then - echo -e "${docker_exec_command} (){\n\ + echo -e "\n${docker_exec_command} (){\n\ docker exec -it -u ${container_user} -w /workspaces/${project_name} ${container_name} zsh\n\ }" >>"${HOME}/.bashrc" echo -e "${GREEN}Created \"${docker_exec_command}\" command in your ${HOME}/.bashrc file.${NC}" - # shellcheck source=/dev/null - source "${HOME}/.bashrc" + # Source .bashrc in a subshell to retain current environment variables + bash -c "source '${HOME}/.bashrc'" fi # Add ${docker_exec_command} command to open the dev container to a users .zshrc file if it is not already there. if ! grep -F "${docker_exec_command} ()" "${HOME}/.zshrc" >/dev/null 2>&1 && [ -f "${HOME}/.zshrc" ]; then - echo -e "${docker_exec_command} (){\n\ + echo -e "\n${docker_exec_command} (){\n\ docker exec -it -u ${container_user} -w /workspaces/${project_name} ${container_name} zsh\n\ }" >>"${HOME}/.zshrc" echo -e "${GREEN}Created \"${docker_exec_command}\" command in your ${HOME}/.zshrc file.${NC}" - # shellcheck source=/dev/null - source "${HOME}/.zshrc" + + # Source .zshrc in a subshell to retain current environment variables + zsh -c "source '${HOME}/.zshrc'" fi fi +} - # If the dev container is running, we assume VSCode is also running. If VSCode is not running, then open it. - if ! docker ps | grep ${container_name}; then - # Check if we are using "Docker for Windows". `devcontainer open` only works with "Docker for Windows". - if [[ "${is_windows_docker}" = true ]]; then - # Check if devcontainer cli is installed, if not let the user know and open VSCode with `code .`. - if ! which devcontainer; then - echo -e "${YELLOW}The 'devcontainer' command was not found." - echo "" - echo "Opening VS Code outside of the dev container." - echo "Once VS Code is open, press F1 or ctrl+shift+p to open the command pallet." - echo "Type 'devcontainer' then select 'Remote Containers: Install devcontainer CLI' from the list." - echo -e "Windows users will need to restart their computer for this to take effect.${NC}" - - code . - else - # Open the dev container in another shell so it doesn't hang on the command line for 45 seconds for some unknown reason. - (devcontainer open &) >/dev/null 2>&1 - fi - fi +open_vs_code() { + # check for dependencies + if ! command -v xxd &>/dev/null; then + echo "xxd command not found, install with" + echo "sudo apt install xxd" + exit 1 + fi + + DEVCONTAINER_JSON="$PWD/.devcontainer/devcontainer.json" + CODE_WS_FILE="$PWD/workspace.code-workspace" - # Looks like we are using native Docker on linux, so just do a `code .` as `devcontainer open` is not supported. - if [[ "${is_windows_docker}" = false ]]; then + if [ ! -f "$DEVCONTAINER_JSON" ]; then + # open code without container + if [ -f "$CODE_WS_FILE" ]; then + echo "Opening vscode workspace from $CODE_WS_FILE" + code $CODE_WS_FILE + else + echo "Opening vscode in current directory" code . fi + exit 0 + fi + + # open devcontainer + HOST_PATH=$(echo $(wslpath -w $PWD) | sed -e 's,\\,\\\\,g') + WORKSPACE="/workspaces/$(basename $PWD)" + + URI_SUFFIX= + if [ -f "$CODE_WS_FILE" ]; then + # open workspace file + URI_TYPE="--file-uri" + URI_SUFFIX="$WORKSPACE/$(basename $CODE_WS_FILE)" + echo "Opening vscode workspace file within devcontainer" + else + URI_TYPE="--folder-uri" + URI_SUFFIX="$WORKSPACE" + echo "Opening vscode within devcontainer" fi + URI="{\"hostPath\":\"$HOST_PATH\",\"configFile\":{\"\$mid\":1,\"path\":\"$DEVCONTAINER_JSON\",\"scheme\":\"vscode-fileHost\"}}" + URI_HEX=$(echo "${URI}" | xxd -c 0 -p) + code ${URI_TYPE}="vscode-remote://dev-container%2B${URI_HEX}${URI_SUFFIX}" & +} + +exec_into_container() { # Here we check if the dev container has started yet. # Wait a max of 600 seconds (10 minutes). local max_wait=600 @@ -115,9 +145,22 @@ main() { if [[ -n "$docker_exec_command" ]]; then echo -e "${BLUE}You can use the \"${docker_exec_command}\" command to exec into the dev container from another terminal.${NC}" fi + docker exec -u "${container_user}" -w /workspaces/${project_name} -it ${container_name} zsh } +# Check if user is using Docker Deskop for Windows or the native Docker Engine. +main() { + add_docker_exec_command + + # If the dev container is running, we assume VSCode is also running. If VSCode is not running, then open it. + if ! docker ps | grep ${container_name}; then + open_vs_code + fi + + exec_into_container +} + if ! (return 0 2>/dev/null); then (main "$@") fi diff --git a/images/reopen-in-container.png b/images/reopen-in-container.png deleted file mode 100644 index 231b27e..0000000 Binary files a/images/reopen-in-container.png and /dev/null differ diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..97d5264 --- /dev/null +++ b/run.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# +# This script will start the dev container outside of VSCode. +# +# cSpell:ignore + +set -euo pipefail +IFS=$'\n\t' + +main() { + local tag="$1" + if [ -z "$tag" ]; then + tag="latest" + fi + + # Name of the container + local container_name="ghcr.io/sarg3nt/go-dev-container:${tag}" + + # User being created in the container + local container_user="vscode" + + docker run --mount type=bind,source="$(pwd)",target=/workspaces/working \ + -w /workspaces/working -it --rm -u "${container_user}" \ + "${container_name}" zsh + +} + +if ! (return 0 2>/dev/null); then + (main "$@") +fi