mirror of
https://github.com/fscotto/infra.git
synced 2026-11-25 21:29:51 +00:00
The nvm-based node feature put npm outside sudo secure_path and broke the postCreateCommand, so containers with agents now copy Node from the official docker.io/library/node image into /usr/local in every profile. Each selected agent also adds its official VS Code extension to the customizations and the generator captures every extension installed in the local VS Code through code --list-extensions, deduplicated case-insensitively and sorted at generation time.
352 lines
11 KiB
Go
352 lines
11 KiB
Go
// Command devcontainer generates project configurations sharing host Git and agent state.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
_ "embed"
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed delta.Dockerfile
|
|
var deltaDockerfile string
|
|
|
|
type profile struct {
|
|
Image, Packages string
|
|
Extensions []string
|
|
}
|
|
type agent struct {
|
|
Package string
|
|
Extension string
|
|
Paths []string
|
|
}
|
|
type mount struct {
|
|
Type string `json:"type"`
|
|
Source string `json:"source"`
|
|
Target string `json:"target"`
|
|
ReadOnly bool `json:"readOnly"`
|
|
}
|
|
|
|
func exists(path string) bool { _, err := os.Stat(path); return err == nil }
|
|
func isDir(path string) bool { info, err := os.Stat(path); return err == nil && info.IsDir() }
|
|
func quote(s string) string { return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" }
|
|
func symlink(path string) bool {
|
|
info, err := os.Lstat(path)
|
|
return err == nil && info.Mode()&os.ModeSymlink != 0
|
|
}
|
|
|
|
func selectAgents(selection, home string) ([]string, error) {
|
|
result := []string{}
|
|
candidates := strings.Split(selection, ",")
|
|
if selection == "none" {
|
|
return result, nil
|
|
}
|
|
if selection == "auto" {
|
|
candidates = nil
|
|
for name := range agents {
|
|
candidates = append(candidates, name)
|
|
}
|
|
sort.Strings(candidates)
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, name := range candidates {
|
|
name = strings.TrimSpace(name)
|
|
a, ok := agents[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown agent %q", name)
|
|
}
|
|
available := true
|
|
for _, path := range a.Paths {
|
|
if !isDir(filepath.Join(home, path)) {
|
|
available = false
|
|
}
|
|
}
|
|
if selection == "auto" && !available {
|
|
continue
|
|
}
|
|
if !seen[name] {
|
|
result = append(result, name)
|
|
seen[name] = true
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func generate(project, language, home string, selected []string, local []string, force bool) (string, error) {
|
|
p, ok := profiles[language]
|
|
if !ok {
|
|
return "", fmt.Errorf("unknown language %q", language)
|
|
}
|
|
project, err := filepath.Abs(project)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
project, err = filepath.EvalSymlinks(project)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
home, err = filepath.Abs(home)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
home, err = filepath.EvalSymlinks(home)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !isDir(project) {
|
|
return "", fmt.Errorf("project directory does not exist: %s", project)
|
|
}
|
|
if home == "/" || strings.ContainsAny(home, "\n\r$,\\\"") {
|
|
return "", errors.New("unsupported host home path")
|
|
}
|
|
target := filepath.Join(project, ".devcontainer")
|
|
if symlink(target) {
|
|
return "", errors.New("refusing a symlinked .devcontainer directory")
|
|
}
|
|
if exists(target) && !force {
|
|
return "", errors.New(".devcontainer already exists; use --force to replace generated files")
|
|
}
|
|
paths := []struct {
|
|
path string
|
|
readonly bool
|
|
}{{".gitconfig", true}, {".config/git", true}, {".gitignore_global", true}, {".config/ai", true}}
|
|
claude := false
|
|
for _, name := range selected {
|
|
a, ok := agents[name]
|
|
if !ok {
|
|
return "", fmt.Errorf("unknown agent %q", name)
|
|
}
|
|
for _, path := range a.Paths {
|
|
if !isDir(filepath.Join(home, path)) {
|
|
return "", fmt.Errorf("missing host directory for %s: %s", name, filepath.Join(home, path))
|
|
}
|
|
paths = append(paths, struct {
|
|
path string
|
|
readonly bool
|
|
}{path, false})
|
|
}
|
|
if name == "claude" {
|
|
claude = true
|
|
}
|
|
}
|
|
if claude {
|
|
paths = append(paths, struct {
|
|
path string
|
|
readonly bool
|
|
}{".claude.json", false})
|
|
}
|
|
mounts := []mount{}
|
|
for _, item := range paths {
|
|
if exists(filepath.Join(home, item.path)) {
|
|
destination := filepath.Join(home, item.path)
|
|
if item.path == ".gitconfig" {
|
|
destination = "/run/host-gitconfig"
|
|
}
|
|
mounts = append(mounts, mount{"bind", "${localEnv:HOME}/" + item.path, destination, item.readonly})
|
|
}
|
|
}
|
|
// The devcontainers node feature ships an nvm-based Node outside sudo's
|
|
// secure_path, which broke the postCreateCommand npm install. Copy the
|
|
// official Node image binaries into /usr/local instead, exactly like the
|
|
// ecaas and cdp devcontainers, so sudo npm works in every profile.
|
|
needsNode := len(selected) > 0
|
|
dockerfile := ""
|
|
if needsNode {
|
|
dockerfile += "FROM docker.io/library/node:22-bookworm-slim AS node\n"
|
|
}
|
|
dockerfile += fmt.Sprintf("FROM %s\nUSER root\nRUN apt-get update && apt-get install -y --no-install-recommends \\\n bash-completion ca-certificates curl fzf git git-lfs gnupg openssh-client ripgrep zoxide %s \\\n && rm -rf /var/lib/apt/lists/*\n", p.Image, p.Packages)
|
|
if needsNode {
|
|
dockerfile += "COPY --from=node /usr/local/bin/node /usr/local/bin/node\n"
|
|
dockerfile += "COPY --from=node /usr/local/lib/node_modules/npm/ /usr/local/lib/node_modules/npm/\n"
|
|
dockerfile += "RUN ln -s ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \\\n && ln -s ../lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx \\\n && node --version && npm --version\n"
|
|
}
|
|
dockerfile += deltaDockerfile
|
|
if home != "/home/vscode" {
|
|
dockerfile += "RUN usermod --home " + quote(home) + " --move-home vscode\n"
|
|
}
|
|
// Docker ENV uses its own quoting syntax, rather than shell quoting.
|
|
envHome, _ := json.Marshal(home)
|
|
dockerfile += "ENV HOME=" + string(envHome) + "\n"
|
|
if language == "python" {
|
|
dockerfile += "RUN python -m pip install --no-cache-dir uv ruff ty\n"
|
|
}
|
|
if language == "infra" {
|
|
dockerfile += "RUN python -m pip install --no-cache-dir ansible-core ansible-lint yamllint\n"
|
|
}
|
|
// The container installs the profile extensions, the VS Code extension of
|
|
// every selected agent and the extensions currently installed in the local
|
|
// VS Code, captured at generation time. Regenerate to refresh the local set.
|
|
extensions := append([]string{}, p.Extensions...)
|
|
addExtension := func(id string) {
|
|
if !slices.ContainsFunc(extensions, func(e string) bool { return strings.EqualFold(e, id) }) {
|
|
extensions = append(extensions, id)
|
|
}
|
|
}
|
|
for _, name := range selected {
|
|
if id := agents[name].Extension; id != "" {
|
|
addExtension(id)
|
|
}
|
|
}
|
|
for _, id := range local {
|
|
addExtension(strings.TrimSpace(id))
|
|
}
|
|
slices.Sort(extensions)
|
|
config := map[string]any{
|
|
"name": filepath.Base(project) + " — " + language,
|
|
"build": map[string]string{"dockerfile": "Dockerfile"},
|
|
"remoteUser": "vscode", "updateRemoteUserUID": true,
|
|
"containerEnv": map[string]string{"HOME": home}, "mounts": mounts,
|
|
"customizations": map[string]any{"vscode": map[string]any{"extensions": extensions}},
|
|
}
|
|
if len(selected) > 0 {
|
|
packages := []string{}
|
|
for _, name := range selected {
|
|
packages = append(packages, quote(agents[name].Package))
|
|
}
|
|
config["postCreateCommand"] = "sudo npm install --global --no-audit --no-fund " + strings.Join(packages, " ")
|
|
}
|
|
if claude {
|
|
config["remoteEnv"] = map[string]string{"ANTHROPIC_AUTH_TOKEN": "${localEnv:ANTHROPIC_AUTH_TOKEN}"}
|
|
}
|
|
dockerfile += "USER vscode\n"
|
|
if exists(filepath.Join(home, ".gitconfig")) {
|
|
dockerfile += "RUN git config --global include.path /run/host-gitconfig\n"
|
|
}
|
|
var data bytes.Buffer
|
|
encoder := json.NewEncoder(&data)
|
|
encoder.SetEscapeHTML(false)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(config); err != nil {
|
|
return "", err
|
|
}
|
|
files := []struct{ name, content string }{{"Dockerfile", dockerfile}, {"devcontainer.json", data.String()}}
|
|
// Validate every output before replacing any file.
|
|
for _, file := range files {
|
|
path := filepath.Join(target, file.name)
|
|
if info, err := os.Lstat(path); err == nil {
|
|
if !info.Mode().IsRegular() {
|
|
return "", fmt.Errorf("refusing non-regular output: %s", path)
|
|
}
|
|
} else if !os.IsNotExist(err) {
|
|
return "", err
|
|
}
|
|
}
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
|
return "", err
|
|
}
|
|
for _, file := range files {
|
|
if err := writeAtomic(filepath.Join(target, file.name), file.content); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return target, nil
|
|
}
|
|
|
|
func writeAtomic(path, content string) error {
|
|
file, err := os.CreateTemp(filepath.Dir(path), ".devcontainer-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(file.Name())
|
|
if _, err = io.WriteString(file, content); err != nil {
|
|
file.Close()
|
|
return err
|
|
}
|
|
if err = file.Chmod(0644); err != nil {
|
|
file.Close()
|
|
return err
|
|
}
|
|
if err = file.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(file.Name(), path)
|
|
}
|
|
|
|
// discoverLocalExtensions lists the extensions installed in the local VS Code.
|
|
// A missing or failing code binary is not fatal: the generated container then
|
|
// only carries the profile and agent extensions.
|
|
func discoverLocalExtensions(stderr io.Writer) []string {
|
|
path, err := exec.LookPath("code")
|
|
if err != nil {
|
|
fmt.Fprintln(stderr, "note: no local VS Code found; skipping locally installed extensions")
|
|
return nil
|
|
}
|
|
out, err := exec.Command(path, "--list-extensions").Output()
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "warning: could not list local VS Code extensions: %v\n", err)
|
|
return nil
|
|
}
|
|
extensions := []string{}
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
if id := strings.TrimSpace(line); id != "" {
|
|
extensions = append(extensions, id)
|
|
}
|
|
}
|
|
return extensions
|
|
}
|
|
|
|
func run(args []string, home string, out, stderr io.Writer) error {
|
|
flags := flag.NewFlagSet("devcontainer", flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
language := flags.String("language", "", "c-cpp, go, rust, python, node, java, infra (required)")
|
|
selection := flags.String("agents", "auto", "auto, none, or comma-separated codex,cline,opencode,claude,gemini")
|
|
force := flags.Bool("force", false, "Replace the two generated files")
|
|
flags.Usage = func() {
|
|
fmt.Fprintln(stderr, "Usage: devcontainer PROJECT --language LANGUAGE [--agents AGENTS] [--force]")
|
|
flags.PrintDefaults()
|
|
}
|
|
// Accept the project's original position before the flags as well as flags first.
|
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
args = append(append([]string{}, args[1:]...), args[0])
|
|
}
|
|
if err := flags.Parse(args); err != nil {
|
|
return err
|
|
}
|
|
if flags.NArg() != 1 || *language == "" {
|
|
flags.Usage()
|
|
return errors.New("one project and --language are required")
|
|
}
|
|
selected, err := selectAgents(*selection, home)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
local := discoverLocalExtensions(stderr)
|
|
target, err := generate(flags.Arg(0), *language, home, selected, local, *force)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
names := strings.Join(selected, ", ")
|
|
if names == "" {
|
|
names = "none"
|
|
}
|
|
fmt.Fprintf(out, "Created %s; agents: %s\n", target, names)
|
|
if len(local) > 0 {
|
|
fmt.Fprintf(out, "Included %d local VS Code extensions.\n", len(local))
|
|
}
|
|
fmt.Fprintln(out, "Open the project in VS Code and choose Dev Containers: Reopen in Container.")
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
home, err := os.UserHomeDir()
|
|
if err == nil {
|
|
err = run(os.Args[1:], home, os.Stdout, os.Stderr)
|
|
}
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return
|
|
}
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|