Skip to content

CLI: Initialize template support#40195

Open
AmelBawa-msft wants to merge 15 commits intofeature/wsl-for-appsfrom
user/amelbawa/format-template
Open

CLI: Initialize template support#40195
AmelBawa-msft wants to merge 15 commits intofeature/wsl-for-appsfrom
user/amelbawa/format-template

Conversation

@AmelBawa-msft
Copy link
Copy Markdown

@AmelBawa-msft AmelBawa-msft commented Apr 15, 2026

Summary of the Pull Request

Enable using Go templates from C++

wslc ps -a --format 'Name is {{.Name}} and state is {{.State}}'
Name is demo and state is 3
Name is demo-2 and state is 3

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Copilot AI review requested due to automatic review settings April 15, 2026 23:03
@AmelBawa-msft AmelBawa-msft changed the title CLI: Initialize template support [Do not review] CLI: Initialize template support Apr 15, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds initial “template” output support to the WSLC CLI by introducing a Go-based template renderer (built as a Windows DLL) and wiring it into wslc container list --format ... so users can format container output beyond table and json.

Changes:

  • Add FormatType::Template and update container list to render per-container output via a new TemplateRenderer bridge.
  • Introduce a Go c-shared DLL (render.dll) exposing RenderGoTemplate/FreeMemory, plus CMake build steps to build/link/copy it.
  • Relax --format parsing so values other than json/table map to template mode.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/windows/wslc/tools/gotemplate/render.go Go c-shared implementation that parses JSON and executes a Go text/template.
src/windows/wslc/tools/gotemplate/render.def Export list for the Go-built DLL functions.
src/windows/wslc/tasks/ContainerTasks.cpp Adds FormatType::Template handling for container list to render templates per container.
src/windows/wslc/services/ContainerModel.h Extends FormatType enum with Template.
src/windows/wslc/core/TemplateRenderer.h Declares C++ wrapper API for template rendering.
src/windows/wslc/core/TemplateRenderer.cpp Implements the C++ ↔ Go bridge and error handling.
src/windows/wslc/commands/ContainerListCommand.cpp Removes command-specific format validation (now relies on common validation).
src/windows/wslc/commands/ContainerCommand.h Removes ValidateArgumentsInternal override declaration for container list.
src/windows/wslc/arguments/ArgumentValidation.cpp Changes format parsing to return Template for non-json/table values.
src/windows/wslc/CMakeLists.txt Adds custom build/link/copy steps to build the Go template renderer DLL and link it into wslc.

Comment thread src/windows/wslc/CMakeLists.txt Outdated
Comment thread src/windows/wslc/arguments/ArgumentValidation.cpp
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread src/windows/wslc/core/TemplateRenderer.cpp Outdated
Comment thread src/windows/wslc/core/TemplateRenderer.cpp Outdated
Comment thread src/windows/wslc/CMakeLists.txt Outdated
Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread src/windows/wslc/tools/gotemplate/render.go Outdated
Copilot AI review requested due to automatic review settings April 16, 2026 03:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread src/windows/wslc/tools/gotemplate/render.go Outdated
Copilot AI review requested due to automatic review settings April 16, 2026 04:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 11 comments.

Comment thread src/windows/wslc/arguments/ArgumentValidation.cpp Outdated
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread src/windows/wslc/core/TemplateRenderer.cpp
Comment thread src/windows/wslc/tools/gotemplate/render.go Outdated
Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread src/windows/wslc/core/TemplateRenderer.cpp
Comment thread src/windows/wslc/tools/gotemplate/render.go Outdated
Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tools/gotemplate/render.def
Copilot AI review requested due to automatic review settings April 16, 2026 14:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 8 comments.

Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ImageTasks.cpp Outdated
Comment thread src/windows/wslc/core/TemplateRenderer.h Outdated
Comment thread src/windows/wslc/tools/gotemplate/render.go
Comment thread src/windows/wslc/tools/gotemplate/render.go Outdated
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp Outdated
Comment thread test/windows/wslc/WSLCCLIArgumentUnitTests.cpp Outdated
Comment thread src/windows/wslc/arguments/ArgumentValidation.cpp
Copilot AI review requested due to automatic review settings April 16, 2026 20:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 8 comments.

Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ImageTasks.cpp
Comment thread test/windows/wslc/e2e/WSLCE2EImageListTests.cpp
Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/CMakeLists.txt
Comment thread src/windows/wslc/tasks/ContainerTasks.cpp
Comment thread test/windows/wslc/e2e/WSLCE2EContainerListTests.cpp
Comment thread test/windows/wslc/e2e/WSLCE2EContainerListTests.cpp
@AmelBawa-msft AmelBawa-msft marked this pull request as ready for review April 16, 2026 21:01
@AmelBawa-msft AmelBawa-msft requested a review from a team as a code owner April 16, 2026 21:01
@AmelBawa-msft AmelBawa-msft changed the title [Do not review] CLI: Initialize template support CLI: Initialize template support Apr 16, 2026
// On failure, *output contains the error message. The caller must free *output with FreeGoString.
//
//export TryRenderGoTemplate
func TryRenderGoTemplate(templateStr *C.char, jsonData *C.char, output **C.char) C.int {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is clever ! If we easily call into a go dll, this makes supporting templates a lot easier.

One thing I'll recommend though is that given that are most likely not going to make changes to that dll very often, would to build it separately in https://github.com/microsoft/WSL-deps and consume via a nuget package, so that we don't need to rebuild it locally on every build

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will also probably be easier to build the dll in that repo, since we can use msbuild directly and not have to go through cmake

@benhillis
Copy link
Copy Markdown
Member

benhillis commented Apr 25, 2026

FreeGoString(rawOutput) is called unconditionally without null check, and if TryRenderGoTemplate throws, rawOutput leaks. Use a wil::scope_exit for cleanup.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants