Skip to content

Commit e5a7732

Browse files
committed
add VS Code devcontainer configuration
1 parent 41f59a0 commit e5a7732

3 files changed

Lines changed: 366 additions & 0 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Use the official Go image as base
2+
FROM golang:1.25-bookworm
3+
4+
# Install system dependencies
5+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
6+
&& apt-get -y install --no-install-recommends \
7+
build-essential \
8+
ca-certificates \
9+
curl \
10+
git \
11+
gnupg \
12+
make \
13+
nano \
14+
software-properties-common \
15+
vim \
16+
wget \
17+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
18+
19+
# Install golangci-lint
20+
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.62.2
21+
22+
# Install goreleaser
23+
RUN echo 'deb [trusted=yes] https://repo.goreleaser.com/apt/ /' | tee /etc/apt/sources.list.d/goreleaser.list \
24+
&& apt-get update \
25+
&& apt-get install -y goreleaser \
26+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/*
27+
28+
# Install Hugo (for documentation)
29+
RUN wget https://github.com/gohugoio/hugo/releases/download/v0.139.4/hugo_extended_0.139.4_linux-amd64.deb \
30+
&& dpkg -i hugo_extended_0.139.4_linux-amd64.deb \
31+
&& rm hugo_extended_0.139.4_linux-amd64.deb
32+
33+
# Create a non-root user
34+
ARG USERNAME=vscode
35+
ARG USER_UID=1000
36+
ARG USER_GID=$USER_UID
37+
38+
RUN groupadd --gid $USER_GID $USERNAME \
39+
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \
40+
&& apt-get update \
41+
&& apt-get install -y sudo \
42+
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
43+
&& chmod 0440 /etc/sudoers.d/$USERNAME
44+
45+
# Set up Go workspace
46+
ENV GOPATH=/go
47+
RUN mkdir -p /go/pkg/mod && chown -R ${USERNAME}:${USERNAME} /go
48+
49+
# Switch to non-root user
50+
USER $USERNAME
51+
52+
# Install Go tools that are commonly used
53+
RUN go install golang.org/x/tools/gopls@latest \
54+
&& go install github.com/go-delve/delve/cmd/dlv@latest \
55+
&& go install honnef.co/go/tools/cmd/staticcheck@latest
56+
57+
# Set working directory
58+
WORKDIR /workspace
59+
60+
# Persist bash history across container restarts
61+
VOLUME ["/home/vscode/.bash_history"]

.devcontainer/README.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# Kosli CLI DevContainer
2+
3+
This devcontainer provides a complete development environment for the Kosli CLI project.
4+
5+
## What's Included
6+
7+
### Languages & Runtimes
8+
9+
- **Go 1.25** - Primary development language
10+
- **Node.js LTS** - For npm package management and release
11+
12+
### Development Tools
13+
14+
- **golangci-lint** - Go linting and static analysis
15+
- **goreleaser** - Release automation
16+
- **gotestsum** - Enhanced Go test runner
17+
- **Hugo Extended** - Documentation site generation
18+
- **GitHub CLI (gh)** - GitHub integration
19+
20+
### Infrastructure
21+
22+
- **Docker-in-Docker** - For integration tests (runs Kosli server)
23+
- **Make** - Build automation
24+
- **Git** - Version control
25+
26+
## Getting Started
27+
28+
1. **Open in VS Code**
29+
- Install the "Dev Containers" extension
30+
- Open this repository in VS Code
31+
- Click "Reopen in Container" when prompted (or use Command Palette: "Dev Containers: Reopen in Container")
32+
33+
2. **Wait for Setup**
34+
- The container will build (first time takes a few minutes)
35+
- Post-create commands will run automatically (`go mod download`, etc.)
36+
37+
3. **Start Developing**
38+
39+
```bash
40+
# Build the CLI
41+
make build
42+
43+
# Run tests
44+
make test_integration
45+
46+
# Run linting
47+
make lint
48+
49+
# Build documentation
50+
make hugo-local
51+
```
52+
53+
## Container Features
54+
55+
### Forwarded Ports
56+
57+
- **1515** - Hugo documentation server
58+
- **8001** - Local Kosli server (for integration tests)
59+
60+
### Volume Mounts
61+
62+
- Go module cache is persisted in a Docker volume for faster builds
63+
- Docker socket is mounted for Docker-in-Docker functionality
64+
65+
### VS Code Extensions
66+
67+
The following extensions are automatically installed:
68+
69+
- **Go** - Go language support
70+
- **Docker** - Docker file and container management
71+
- **GitLens** - Enhanced Git capabilities
72+
73+
### VS Code Settings
74+
75+
Pre-configured with:
76+
77+
- Go formatting on save
78+
- Automatic import organization
79+
- golangci-lint integration
80+
- Go language server (gopls)
81+
82+
## Common Tasks
83+
84+
### Building the CLI
85+
86+
```bash
87+
make build
88+
./kosli version
89+
```
90+
91+
### Running Tests
92+
93+
```bash
94+
# Integration tests (requires Docker)
95+
make test_integration
96+
97+
# Run all tests including slow ones
98+
make test_integration_full
99+
100+
# Run specific test suite
101+
make test_integration_single TARGET=TestAttestJunitCommandTestSuite
102+
```
103+
104+
### Working with NPM Package
105+
106+
```bash
107+
# Test npm package installation locally
108+
npm install
109+
npm test
110+
111+
# Pack for distribution
112+
npm pack
113+
114+
# Test the packed version
115+
mkdir /tmp/npm-test && cd /tmp/npm-test
116+
npm install /workspace/*.tgz
117+
npx kosli version
118+
```
119+
120+
### Documentation
121+
122+
```bash
123+
# Generate CLI documentation
124+
make cli-docs
125+
126+
# Start Hugo documentation server (accessible at localhost:1515)
127+
make hugo-local
128+
```
129+
130+
### Linting
131+
132+
```bash
133+
make lint
134+
```
135+
136+
## Environment Variables
137+
138+
The following environment variables are pre-configured:
139+
140+
- `CGO_ENABLED=0` - Disable CGO for static binaries
141+
- `GO111MODULE=on` - Use Go modules
142+
- `GOPATH=/go` - Go workspace location
143+
144+
### Required for Testing
145+
146+
You need to set `KOSLI_API_TOKEN_PROD` for integration tests:
147+
148+
```bash
149+
export KOSLI_API_TOKEN_PROD="your-token-here"
150+
```
151+
152+
## Docker Integration
153+
154+
The devcontainer uses Docker-in-Docker to run integration tests. The Kosli server runs in a container alongside your tests.
155+
156+
### Managing Test Containers
157+
158+
```bash
159+
# View server logs
160+
make logs_integration_test_server
161+
162+
# Follow server logs
163+
make follow_integration_test_server
164+
165+
# Enter server container
166+
make enter_integration_test_server
167+
```
168+
169+
## Troubleshooting
170+
171+
### Container won't start
172+
173+
- Ensure Docker Desktop is running
174+
- Check Docker has enough resources (4GB+ RAM recommended)
175+
- Try rebuilding: Command Palette → "Dev Containers: Rebuild Container"
176+
177+
### Tests failing
178+
179+
- Ensure `KOSLI_API_TOKEN_PROD` is set
180+
- Check Docker is working: `docker ps`
181+
- Verify network: `docker network ls` (should see `cli_net`)
182+
183+
### Go modules issues
184+
185+
```bash
186+
go clean -modcache
187+
go mod download
188+
go mod tidy
189+
```
190+
191+
### NPM installation issues
192+
193+
```bash
194+
rm -rf node_modules package-lock.json
195+
npm install
196+
```
197+
198+
## Customization
199+
200+
### Adding VS Code Extensions
201+
202+
Edit [.devcontainer/devcontainer.json](.devcontainer/devcontainer.json):
203+
204+
```json
205+
"extensions": [
206+
"golang.go",
207+
"your-extension-id"
208+
]
209+
```
210+
211+
### Installing Additional Tools
212+
213+
Edit [.devcontainer/Dockerfile](.devcontainer/Dockerfile) and rebuild the container.
214+
215+
### Changing Environment Variables
216+
217+
Edit the `remoteEnv` section in [.devcontainer/devcontainer.json](.devcontainer/devcontainer.json).
218+
219+
## Non-Root User
220+
221+
The container runs as the `vscode` user (non-root) for security. Use `sudo` if you need root access:
222+
223+
```bash
224+
sudo apt-get install some-package
225+
```
226+
227+
## References
228+
229+
- [Dev Containers Documentation](https://code.visualstudio.com/docs/devcontainers/containers)
230+
- [Kosli Documentation](https://docs.kosli.com/)
231+
- [Project Development Guide](../dev-guide.md)

.devcontainer/devcontainer.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"name": "Kosli CLI Development",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"context": ".."
6+
},
7+
8+
// Configure tool-specific properties
9+
"customizations": {
10+
"vscode": {
11+
"settings": {
12+
"go.gopath": "/go",
13+
"go.toolsManagement.checkForUpdates": "local",
14+
"go.useLanguageServer": true,
15+
"go.lintTool": "golangci-lint",
16+
"go.lintOnSave": "workspace",
17+
"editor.formatOnSave": true,
18+
"[go]": {
19+
"editor.defaultFormatter": "golang.go",
20+
"editor.codeActionsOnSave": {
21+
"source.organizeImports": "explicit"
22+
}
23+
}
24+
},
25+
"extensions": [
26+
"DavidAnson.vscode-markdownlint",
27+
"eamodio.gitlens",
28+
"golang.go",
29+
"ms-azuretools.vscode-docker",
30+
"ms-vscode.makefile-tools",
31+
"yzhang.markdown-all-in-one"
32+
]
33+
}
34+
},
35+
36+
// Use 'forwardPorts' to make a list of ports inside the container available locally
37+
"forwardPorts": [1515, 8001],
38+
39+
// Use 'postCreateCommand' to run commands after the container is created
40+
"postCreateCommand": "go mod download && go install gotest.tools/gotestsum@latest",
41+
42+
// Features to add to the dev container
43+
"features": {
44+
"ghcr.io/devcontainers/features/docker-in-docker:2": {
45+
"version": "latest",
46+
"moby": true,
47+
"dockerDashComposeVersion": "v2"
48+
},
49+
"ghcr.io/devcontainers/features/node:1": {
50+
"version": "lts",
51+
"nodeGypDependencies": true,
52+
"installYarnUsingApt": false
53+
},
54+
"ghcr.io/devcontainers/features/github-cli:1": {
55+
"version": "latest"
56+
}
57+
},
58+
59+
// Set environment variables
60+
"remoteEnv": {
61+
"CGO_ENABLED": "0",
62+
"GO111MODULE": "on",
63+
"GOPATH": "/go"
64+
},
65+
66+
// Run as vscode user (non-root)
67+
"remoteUser": "vscode",
68+
69+
// Mount docker socket and go module cache
70+
"mounts": [
71+
"source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind",
72+
"source=kosli-cli-gomod,target=/go/pkg/mod,type=volume"
73+
]
74+
}

0 commit comments

Comments
 (0)