Skip to content

Commit

Permalink
Add vscode remote container development files
Browse files Browse the repository at this point in the history
Why:
The vscode Golang plugin enforces style, identifies errors and
has a remote debugger.
Developing in a container isolates the dev environment from the host machine.
  • Loading branch information
laurb9 committed Oct 1, 2020
1 parent 99d3cef commit e849886
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# From https://github.com/microsoft/vscode-dev-containers/blob/master/containers/go/.devcontainer/Dockerfile
ARG VARIANT=1
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}

# [Optional] Install a version of Node.js using nvm for front end dev
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends vim

# [Optional] Uncomment the next line to use go get to install anything else you need
# RUN go get -x <your-dependency-or-tool>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
45 changes: 45 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/go
{
"name": "Go",
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.15, 1.14
"VARIANT": "1.14",
// Options
"INSTALL_NODE": "false",
"NODE_VERSION": "lts/*",
}
},
"containerEnv": {
"GOPRIVATE": "${localEnv:GOPRIVATE}",
},
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"go.useGoProxyToCheckForToolUpdates": false,
"go.gopath": "/go",
//"go.toolsGopath": "/tmp/go",
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go",
"ms-azuretools.vscode-docker",
"redhat.vscode-xml",
"redhat.vscode-yaml",
"eamodio.gitlens",
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8000,8001,6060],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "mkdir ~/.ssh; ssh-keyscan github.com > ~/.ssh/known_hosts",

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ _obj
_test
.cover/
.idea/
.vscode/

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down Expand Up @@ -46,6 +45,7 @@ analytics/filesystem/testFiles/
# static/version.txt

.idea/
.vscode/

# autogenerated mac file

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ Want to [add an adapter](https://docs.prebid.org/prebid-server/developers/add-ne
Report bugs, request features, and suggest improvements [on Github](https://github.com/prebid/prebid-server/issues).

Or better yet, [open a pull request](https://github.com/prebid/prebid-server/compare) with the changes you'd like to see.

## IDE Setup for PBS-Go development

The quickest way to start developing PBS-Go in a reproducible environment isolated from your host OS
is by using this [VScode Remote Container Setup](devcontainer.md)
64 changes: 64 additions & 0 deletions devcontainer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

### vscode in-container development

The quickest way to get up and running with PBS-Go development in a reproducible environment isolated
from your host OS is by loading the repository in a [Docker](https://docs.docker.com/get-docker/)
container under [Visual Studio Code](https://code.visualstudio.com/).

This covers installing Go and necessary IDE plugins for Golang editing and debugging, and is
all automated via the VSCode [.devcontainer](.devcontainer/) configuration. See
[VSCode Remote Containers](https://code.visualstudio.com/docs/remote/containers) for more
details about how to customize.

#### Setup

Install:

- [Docker](https://docs.docker.com/get-docker/)
- [Visual Studio Code](https://code.visualstudio.com/)
- [VSCode Remote Development Extension Pack](https://aka.ms/vscode-remote/download/extension)

Then:

- start VSCode and open repository
- accept VSCode suggestion to _reopen in container_
- VSCode will build a new container, install the IDE support in it and

Optionally, to use your github ssh key for accessing non-public GitHub repositories:
- Method 1: add your github ssh key to agent. This is needed after each OS restart.
```sh
ssh-add ~/.ssh/id_rsa # or your ssh key for github
```
- Method 2: map your ~/.ssh (or just the key) as a docker volume in .devcontainer.json

#### Starting PBS-Go

- `Shift`-`Cmd`-`D` or `Run` icon brings up the `Launch prebid-server` panel with interactive
debugger. Breakpoints can be set to stop execution.
- CTRL-`\`` opens the terminal to start prebid-server non-interactively
```sh
go run main.go --alsologtostderr
```
- Create a pbs.yaml file if neccessary, with configuration overrides.

#### Testing

- Open any `*_test.go` file in editor
- Individual test functions can be run directly by clicking the `run test`/`debug test` annotation
above each test function.
- At the top of the file you can see `run package tests | run file tests`
- TIP: use `run package tests` at the top of the test file to quickly check code coverage:
the open editors for files in the tested package will have lines highlighted in green (covered)
and red (not covered)
- CTRL-`\`` opens the terminal to run the test suite
```sh
./validate.sh
```

#### Editing
- Style, imports are automatically updated on save.
- Editor can suggest correct names and

- Remote container commands popup by clicking on _Dev Container: Go_ at bottom left
- `F1` -> type _rebuild container_ to restart with a fresh container
- `F1` -> `^`-`\`` to toggle terminal

0 comments on commit e849886

Please sign in to comment.