From 6b1edb47234a2e7792bf6def3b647137ccd2ab78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 3 Mar 2025 17:17:59 +0100 Subject: [PATCH 1/3] chore: clone the OSS git repos into the build-base image --- Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Dockerfile b/Dockerfile index cfe29a65fbf..825f8ef38af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,9 +33,16 @@ WORKDIR /out ADD https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz . RUN tar xvf hugo_extended_${HUGO_VERSION}_linux-${TARGETARCH}.tar.gz +# git-src clones the OSS projects in order to include +# code snippets into the docs. +FROM base AS git-src-oss +WORKDIR /git-src +RUN git clone https://github.com/testcontainers/testcontainers-go.git + # build-base is the base stage used for building the site FROM base AS build-base WORKDIR /project +COPY --from=git-src-oss /git-src /project/git-src COPY --from=hugo /out/hugo /bin/hugo COPY --from=npm /out/node_modules node_modules COPY . . From 0c03caec2f31571d81cc319cd38f9e0474c0ec92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 3 Mar 2025 17:18:23 +0100 Subject: [PATCH 2/3] feat: add an embedded shortcode for code snippets --- layouts/shortcodes/embedded.html | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 layouts/shortcodes/embedded.html diff --git a/layouts/shortcodes/embedded.html b/layouts/shortcodes/embedded.html new file mode 100644 index 00000000000..405b0b8c293 --- /dev/null +++ b/layouts/shortcodes/embedded.html @@ -0,0 +1,65 @@ +{{ $language := .Get "language" }} +{{ $source := .Get "source" }} +{{ $options := .Get "options" }} +{{ $id := .Get "id" }} +{{ $startTag := printf "START %s" $id }} +{{ $endTag := printf "END %s" $id }} + +{{ if and $source (strings.Contains $source "testcontainers-go") }} + {{/* + If the source is a testcontainers-go file, we need to use a different tag. In example: + + // runRedisContainer { + // ... + // } + */}} + {{ $startTag = printf "// %s {" $id }} + {{ $endTag = printf "// }" }} +{{ end }} + +{{ with $source | readFile }} + {{ $snippet := . }} + + {{ if $id }} + {{ $lines := split $snippet "\n" }} + + {{ $startl := -1 }} + {{ $endl := -1 }} + + {{/* Find the lines that ends with the start and end tags. */}} + {{ range $index, $line := $lines }} + {{ if hasSuffix (strings.TrimSpace $line) $startTag }} + {{ $startl = $index }} + {{ else if hasSuffix (strings.TrimSpace $line) $endTag }} + {{ $endl = $index }} + {{ end }} + {{ end }} + + {{/* Let's add some basic assertions. */}} + {{ if lt $startl 0 }} + {{ errorf "Named snippet '%s' is missing START tag (searched %d lines)" $id (len $lines) }} + {{ end }} + + {{ if lt $endl 0 }} + {{ errorf "Named snippet '%s' is missing END tag (searched %d lines)" $id (len $lines) }} + {{ end }} + + {{ if le $endl $startl }} + {{ errorf "Named snippet '%s': END tag (line %d) must come after START tag (line %d)" $id $endl $startl }} + {{ end }} + + {{/* Size of the snippet in number of lines. */}} + {{ $snippetLen := sub (sub $endl $startl) 1 }} + + {{/* Create slice with only the lines between the tags. */}} + {{ $includedLines := first $snippetLen (after (add $startl 1) $lines) }} + + {{/* Join the lines into the final snippet. */}} + {{ $snippet = delimit $includedLines "\n" }} + {{ $markdown := printf "```%s\n%s\n```" $language $snippet }} + {{ $markdown | markdownify }} + {{ else }} + {{ $markdown := printf "```%s\n%s\n```" $language . }} + {{ $markdown | markdownify }} + {{ end }} +{{ end }} From ca0901bbb8c1230f0f04ff2f648a3a37cfc279bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 3 Mar 2025 17:18:42 +0100 Subject: [PATCH 3/3] chore: example for testcontainers-go --- content/manuals/testcontainers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/content/manuals/testcontainers.md b/content/manuals/testcontainers.md index 73b538a2750..4b00c2e5605 100644 --- a/content/manuals/testcontainers.md +++ b/content/manuals/testcontainers.md @@ -42,6 +42,8 @@ Testcontainers provide support for the most popular languages, and Docker sponso The rest are community-driven and maintained by independent contributors. +{{< embedded language="go" source="/git-src/testcontainers-go/modules/redis/examples_test.go" id="runRedisContainer" >}} + ### Prerequisites Testcontainers requires a Docker-API compatible container runtime.