Skip to content

Commit

Permalink
Fix #14 remove redundancy in devcontainer and feat: Auto-detect local…
Browse files Browse the repository at this point in the history
… locale (PR#19)

* fix: remove unused VSCode setting file
* chore: update module
* feat: Auto-detect local locale
  • Loading branch information
KEINOS authored Feb 10, 2021
1 parent 9475d97 commit aae3ba5
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 127 deletions.
120 changes: 67 additions & 53 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
# [Choice] Go version: 1, 1.15, 1.14
ARG VARIANT="1.15"
# =============================================================================
# Pre Defined Variables
# =============================================================================
# User provided Go version of Docker image
ARG VARIANT
# Default Go version. Choices: 1, 1.15, 1.14
ARG VARIANT_DEFAULT="1"
# Default Language
ARG LC_ALL_DEFAULT='en_US.utf8'
ARG LANG_DEFAULT='en_US.utf8'
# Default Node.js settings
ARG INSTALL_NODE_DEFAULT="false"
ARG NODE_VERSION_DEFAULT="lts/*"

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.145.1/containers/go/.devcontainer/base.Dockerfile
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
# =============================================================================
# Define Dockerfile.
#
# For image contents see:
# https://github.com/microsoft/vscode-dev-containers/tree/v0.145.1/containers/go/.devcontainer/base.Dockerfile
# =============================================================================
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT:-$VARIANT_DEFAULT}

# Declare user args to receive while building an image.
ARG LANG
ARG LC_ALL
ARG INSTALL_NODE
ARG NODE_VERSION

# Language settings (Provide the below ENV variables via Docker env args while building)
ARG LC_ALL='en_US.utf8'
ARG LANG='en_US.utf8'
ARG LANG_CHARMAP='UTF-8'
ARG LANG_LOCALNAME
ARG LANG_LOCALDEF
ARG LANG_LOCALE
ENV \
# Enforce module mode
GO111MODULE=on \
LANG="${LANG:-$LANG_DEFAULT}" \
LC_ALL="${LC_ALL:-$LC_ALL_DEFAULT}" \
PATH="/usr/local/go/bin:${PATH}" \
# Enforce go module mode
GO111MODULE='on' \
# Fix: https://github.com/microsoft/vscode-dev-containers/issues/51
SHELL="/bin/bash" \
LC_ALL="${LANG_LOCALE:-$LC_ALL}" \
LANG="${LANG_LOCALE:-$LANG}" \
PATH="/usr/local/go/bin:${PATH}"
RUN localedef -f "${LANG_CHARMAP:-'UTF-8'}" -i "${LANG_LOCALDEF:-en_US}" "${LANG_LOCALNAME:-en_US}"
SHELL="/bin/bash"

# [Option] Install Node.js
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
#RUN localedef -f UTF-8

# [Option] Install Node.js
ARG INSTALL_NODE="${INSTALL_NODE:-INSTALL_NODE_DEFAULT}"
ARG NODE_VERSION="${NODE_VERSION:-NODE_VERSION_DEFAULT}"
RUN if [ "${INSTALL_NODE}" = "true" ]; then \
echo 'Installing Node.js'; \
su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; \
fi

# The below copied files will be placed into the proper dir after the container
# is up.
# Bash script file to run right after the container was created.
# See: "postCreateCommand" section in "devcontainer.json" and "postCreateCommand.sh"
# file.
COPY postCreateCommand.sh /postCreateCommand.sh
COPY settings.vscode.json /settings.vscode.json

# Install additional OS packages.
RUN export DEBIAN_FRONTEND=noninteractive \
Expand All @@ -44,7 +60,9 @@ RUN export DEBIAN_FRONTEND=noninteractive \
# Directory tree viewer for documentation.
tree \
# xz to untar xz compressed files
xz-utils
xz-utils \
# missing locales
locales

# ShellCheck - Static analyzer and formatter for shell script
# Note: Install the latest shellcheck. See: https://github.com/koalaman/shellcheck/issues/704
Expand All @@ -65,34 +83,30 @@ RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/i

# Install anything else needed to go get
RUN go get -u -v \
# gopls (language server)
# `gopls` will install the packages in the URL below as well.
# https://github.com/golang/vscode-go/blob/master/docs/tools.md#table-of-contents
"golang.org/x/tools/gopls@latest" \
# gotests (Test generator)
"github.com/cweill/gotests/..." \
# go-carpet (Cover area checker)
"github.com/msoap/go-carpet" \
# shfmt (shellformat) is a shell script linter
"mvdan.cc/sh/v3/cmd/shfmt" \
# gp enables to share go files over Go Playground
"github.com/tenntenn/goplayground/cmd/gp" \
# gomarkdoc generates documentation in markdown
"github.com/princjef/gomarkdoc/cmd/gomarkdoc" \
# Other requirements of VSCode extensions
"golang.org/x/lint/golint" \
"github.com/ramya-rao-a/go-outline" \
"github.com/rogpeppe/godef" \
"github.com/uudashr/gopkgs/v2/cmd/gopkgs" \
# irregular install
# gopls (language server)
# `gopls` will install the packages in the URL below as well.
# https://github.com/golang/vscode-go/blob/master/docs/tools.md#table-of-contents
"golang.org/x/tools/gopls@latest" \
# go-carpet (Cover area checker)
"github.com/msoap/go-carpet" \
# shfmt (shellformat) is a shell script linter
"mvdan.cc/sh/v3/cmd/shfmt" \
# gp enables to share go files over Go Playground
"github.com/tenntenn/goplayground/cmd/gp" \
# gomarkdoc generates documentation in markdown
"github.com/princjef/gomarkdoc/cmd/gomarkdoc" \
#"github.com/rogpeppe/godef" \
# -------------------------------------------------------------------------
# irregular install (Download source and build)
# -------------------------------------------------------------------------
&& cd "${GOPATH}/pkg/mod" \
&& go get -v -d \
# gocode and gocode-modules is no-good to install as module
"github.com/stamblerre/gocode" \
# cobra (sub-command creation)
"github.com/spf13/cobra" \
# gomock
"github.com/golang/mock/gomock" \
# gocode and gocode-modules is no-good to install as module
"github.com/stamblerre/gocode" \
# cobra (sub-command creation)
"github.com/spf13/cobra" \
# gomock
"github.com/golang/mock/gomock" \
# gocode and gocode-modules
&& cd ${GOPATH}/pkg/mod/github.com/stamblerre/gocode* \
&& go build -o "${GOPATH}/bin/gocode-gomod" . \
Expand All @@ -107,7 +121,7 @@ RUN go get -u -v \

# Miscellaneous
RUN \
# Set path
# Set path for go
echo 'export PATH="/go/bin:/usr/local/go/bin:${PATH}"' >> "${HOME}/.bashrc"

# [Optional] Uncomment this line to install global node packages.
Expand Down
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# =============================================================================
# devcontainer.env defines ENV variables when run.
#
# Same as: `docker run --env-file $(pwd)/devcontainer.env`
# Note that you need to rebuild the container if you change the values.
# =============================================================================

# -----------------------------------------------------------------------------
# Host's Env Variables
# -----------------------------------------------------------------------------
# Language detection
# - Sample: Japanese
# LANG=ja_JP.UTF-8
# LC_ALL=ja_JP.UTF-8
# - Sample: Spanish
# LANG=es_MX.UTF-8
# LC_ALL=es_MX.UTF-8
# - Sample: Use local locale
# LANG
# LC_ALL
LANG
LC_ALL

# -----------------------------------------------------------------------------
# User Defined Variables.
# -----------------------------------------------------------------------------
34 changes: 18 additions & 16 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,37 @@
"build": {
"dockerfile": "Dockerfile",
"args": {
// Language
"LANG_LOCALNAME": "",
"LANG_LOCALE": "",
// Update the VARIANT arg to pick a version of Go: 1, 1.15, 1.14
// Go version: 1, 1.15, 1.14
"VARIANT": "1.15",
// Options
"INSTALL_NODE": "true",
"INSTALL_NODE": "false",
"NODE_VERSION": "lts/*"
}
},
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

// Set *default* container specific settings.json values on container create.
"runArgs": [
"--cap-add=SYS_PTRACE",
"--security-opt", "seccomp=unconfined",
"--env-file",".devcontainer/devcontainer.env"
],
// Set VSCode settings
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": ["-l"],
"go.toolsManagement.checkForUpdates": "proxy",
"go.useLanguageServer": true,
"go.gopath": "/go",
"go.goroot": "/usr/local/go",
"go.toolsGopath": "/go/bin"
"go.lintFlags": ["--enable-all", "--new"],
"go.toolsGopath": "/go/bin",
"go.toolsManagement.checkForUpdates": "proxy",
"go.useLanguageServer": true,
"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": ["-l"]
},

// Add the IDs of extensions you want to be installed when the container is created.
// VSCode extension ID to be installed
"extensions": [
"golang.Go",
"editorconfig.editorconfig",
"foxundermoon.shell-format",
"ms-ceintl.vscode-language-pack-ja",
"ms-ceintl.vscode-language-pack-es",
"ms-azuretools.vscode-docker",
"ms-vsonline.vsonline",
"github.github-vscode-theme",
Expand All @@ -40,8 +42,8 @@
],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "/bin/bash /postCreateCommand.sh;",
"postCreateCommand": "/bin/bash /postCreateCommand.sh;"

// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
//"remoteUser": "vscode"
}
13 changes: 12 additions & 1 deletion .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/bin/bash
# Place any script here which you want to run after creating the container.
echo '==============================================================================='
echo ' Post Create Command'
echo '==============================================================================='

set -eu

# Sim-llink Welcome message for bash
# Sim-link Welcome message for bash
ln -s "$(pwd)/.devcontainer/welcome.sh" "${HOME}/.welcome.sh"

# Single quotes are intensional. So, not to expand expressions.
Expand All @@ -15,3 +18,11 @@ ln -s "$(pwd)/.devcontainer/cobra.yaml" "${HOME}/.cobra.yaml"

# Make sure go.mod matches the source code in the module.
go mod tidy

# Set language
LANGUAGE="${LANG//\./:}" # <-- echo $LANG | sed "s/\./:/"
echo "export LANGUAGE=${LANGUAGE}" >>"${HOME}/.bashrc"

echo "${LC_ALL} UTF-8" >>/etc/locale.gen
locale-gen "$LC_ALL"
update-locale LANG="$LANG"
53 changes: 0 additions & 53 deletions .devcontainer/settings.vscode.json

This file was deleted.

6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ go 1.15
require (
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
github.com/mattn/go-shellwords v1.0.11
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0
github.com/spf13/cobra v1.1.2
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
)

0 comments on commit aae3ba5

Please sign in to comment.