From ca46590b44ad22a0b5668f938c4503c1b48d1431 Mon Sep 17 00:00:00 2001 From: Mattia Panzeri <1754457+panz3r@users.noreply.github.com> Date: Tue, 21 Jan 2025 00:08:02 +0000 Subject: [PATCH 1/4] feat: add `go-mod-cache` feature --- src/go-mod-cache/README.md | 25 ++++++++++ src/go-mod-cache/devcontainer-feature.json | 18 +++++++ src/go-mod-cache/install.sh | 56 ++++++++++++++++++++++ test/go-mod-cache/custom_user.sh | 7 +++ test/go-mod-cache/go_feature.sh | 7 +++ test/go-mod-cache/scenarios.json | 35 ++++++++++++++ test/go-mod-cache/test.sh | 52 ++++++++++++++++++++ test/go-mod-cache/with_go.sh | 7 +++ test/go-mod-cache/zsh_shell.sh | 7 +++ 9 files changed, 214 insertions(+) create mode 100644 src/go-mod-cache/README.md create mode 100644 src/go-mod-cache/devcontainer-feature.json create mode 100644 src/go-mod-cache/install.sh create mode 100644 test/go-mod-cache/custom_user.sh create mode 100644 test/go-mod-cache/go_feature.sh create mode 100644 test/go-mod-cache/scenarios.json create mode 100644 test/go-mod-cache/test.sh create mode 100644 test/go-mod-cache/with_go.sh create mode 100644 test/go-mod-cache/zsh_shell.sh diff --git a/src/go-mod-cache/README.md b/src/go-mod-cache/README.md new file mode 100644 index 0000000..d2350f6 --- /dev/null +++ b/src/go-mod-cache/README.md @@ -0,0 +1,25 @@ + +# Go Mod Cache (go-mod-cache) + +Mounts the Go Module cache (`GOMODCACHE`) to a Docker Volume to share between multiple Dev Containers. + +## Example Usage + +```json +"features": { + "ghcr.io/forwardsoftware/devcontainer-features/go-mod-cache:1": { + } +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +|-----|-----|-----|-----| + + + + +--- + +_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/devcontainers/feature-starter/blob/main/src/hello/devcontainer-feature.json). Add additional notes to a `NOTES.md`._ diff --git a/src/go-mod-cache/devcontainer-feature.json b/src/go-mod-cache/devcontainer-feature.json new file mode 100644 index 0000000..5a5eb17 --- /dev/null +++ b/src/go-mod-cache/devcontainer-feature.json @@ -0,0 +1,18 @@ +{ + "name": "Go Mod Cache", + "id": "go-mod-cache", + "version": "1.0.0", + "description": "Mounts the Go Module cache (`GOMODCACHE`) to a Docker Volume to share between multiple Dev Containers.", + "options": {}, + "mounts": [ + { + "source": "global-devcontainer-go-cache", + "target": "/mnt/go-cache", + "type": "volume" + } + ], + "installsAfter": [ + "ghcr.io/devcontainers/features/common-utils", + "ghcr.io/devcontainers/features/go" + ] +} \ No newline at end of file diff --git a/src/go-mod-cache/install.sh b/src/go-mod-cache/install.sh new file mode 100644 index 0000000..166e6eb --- /dev/null +++ b/src/go-mod-cache/install.sh @@ -0,0 +1,56 @@ +#!/bin/sh +set -e + +echo "Activating feature 'go-mod-cache'" + +USERNAME=${USERNAME:-${_REMOTE_USER}} +GOPATH=${GOPATH:-/go} + +create_cache_dir() { + local cache_dir=$1 + local username=$2 + + if [ -d "$cache_dir" ]; then + echo "Cache directory $cache_dir already exists. Skip creation..." + else + echo "Create cache directory $cache_dir..." + mkdir -p "$cache_dir" + fi + + if ! getent group golang > /dev/null; then + echo "Group 'golang' does not exist. Creating..." + groupadd golang + else + echo "Group 'golang' already exists. Skip creation..." + fi + + if [ -z "$username" ]; then + echo "No username provided. Skip chown..." + else + echo "Change owner of $cache_dir to $username..." + chown -R "$username:golang" "$cache_dir" + fi +} + +create_symlink_dir() { + local local_dir=$1 + local cache_dir=$2 + local username=$3 + + runuser -u "$username" -- mkdir -p "$(dirname "$local_dir")" + runuser -u "$username" -- mkdir -p "$cache_dir" + + # if the folder we want to symlink already exists, the ln -s command will create a folder inside the existing folder + if [ -e "$local_dir" ]; then + echo "Moving existing $local_dir folder to $local_dir-old" + mv "$local_dir" "$local_dir-old" + fi + + echo "Symlink $local_dir to $cache_dir for $username..." + runuser -u "$username" -- ln -s "$cache_dir" "$local_dir" +} + +create_cache_dir "/mnt/go-cache" "${USERNAME}" +create_symlink_dir "$GOPATH/pkg" "/mnt/go-cache" "${USERNAME}" + +echo "Finished installing 'go-mod-cache'" \ No newline at end of file diff --git a/test/go-mod-cache/custom_user.sh b/test/go-mod-cache/custom_user.sh new file mode 100644 index 0000000..9b2c250 --- /dev/null +++ b/test/go-mod-cache/custom_user.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# tests installing feature with a custom user + +./test.sh \ No newline at end of file diff --git a/test/go-mod-cache/go_feature.sh b/test/go-mod-cache/go_feature.sh new file mode 100644 index 0000000..e1f3d83 --- /dev/null +++ b/test/go-mod-cache/go_feature.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# tests installing feature when go is installed as feature + +./test.sh diff --git a/test/go-mod-cache/scenarios.json b/test/go-mod-cache/scenarios.json new file mode 100644 index 0000000..d2b5c22 --- /dev/null +++ b/test/go-mod-cache/scenarios.json @@ -0,0 +1,35 @@ +{ + "with_go": { + "image": "mcr.microsoft.com/devcontainers/go:1-1", + "features": { + "go-mod-cache": {} + } + }, + "go_feature": { + "image": "mcr.microsoft.com/devcontainers/base", + "features": { + "ghcr.io/devcontainers/features/go:1": {}, + "go-mod-cache": {} + } + }, + "zsh_shell": { + "image": "mcr.microsoft.com/devcontainers/go:1-1", + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "configureZshAsDefaultShell": true + }, + "go-mod-cache": {} + } + }, + "custom_user": { + "image": "mcr.microsoft.com/devcontainers/base", + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "username": "golang" + }, + "ghcr.io/devcontainers/features/go:1": {}, + "go-mod-cache": {} + }, + "remoteUser": "golang" + } +} \ No newline at end of file diff --git a/test/go-mod-cache/test.sh b/test/go-mod-cache/test.sh new file mode 100644 index 0000000..a70eb03 --- /dev/null +++ b/test/go-mod-cache/test.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# This test file will be executed against an auto-generated devcontainer.json that +# includes the 'hello' Feature with no options. +# +# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md +# +# Eg: +# { +# "image": "<..some-base-image...>", +# "features": { +# "hello": {} +# }, +# "remoteUser": "root" +# } +# +# Thus, the value of all options will fall back to the default value in +# the Feature's 'devcontainer-feature.json'. +# For the 'hello' feature, that means the default favorite greeting is 'hey'. +# +# These scripts are run as 'root' by default. Although that can be changed +# with the '--remote-user' flag. +# +# This test can be run with the following command: +# +# devcontainer features test \ +# --features hello \ +# --remote-user root \ +# --skip-scenarios \ +# --base-image mcr.microsoft.com/devcontainers/base:ubuntu \ +# /path/to/this/repo + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib +# Provides the 'check' and 'reportResults' commands. +source dev-container-features-test-lib + +# Feature-specific tests +# The 'check' command comes from the dev-container-features-test-lib. Syntax is... +# check