Skip to content
This repository was archived by the owner on Jan 12, 2025. It is now read-only.

Commit 53e255a

Browse files
style: standardize feature installation code (#568)
1 parent b4a8660 commit 53e255a

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

src/devcontainers-cli/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

2-
# devcontainers CLI (devcontainers-cli)
2+
# devcontainers CLI (via npm) (devcontainers-cli)
33

4-
The [devcontainers CLI](https://github.com/devcontainers/cli), which configures devcontainers from `devcontainer.json`. Requires the Docker client and access to a container engine daemon socket (e.g. by mounting the host's Docker socket into the container)
4+
The devcontainers CLI, which configures devcontainers from devcontainer.json. Requires the Docker client and access to a Docker engine socket (e.g. by mounting the host's Docker socket into the container)
55

6-
## Example devcontainers CLI Usage
6+
## Example DevContainer Usage
77

88
```json
99
"features": {
10-
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
1110
"ghcr.io/devcontainers-contrib/features/devcontainers-cli:1": {}
1211
}
1312
```
@@ -16,4 +15,6 @@ The [devcontainers CLI](https://github.com/devcontainers/cli), which configures
1615

1716
| Options Id | Description | Type | Default Value |
1817
|-----|-----|-----|-----|
19-
| version | Specify the version to install | string | latest |
18+
| version | Select the version to install. | string | latest |
19+
20+
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"id": "devcontainers-cli",
33
"version": "1.0.0",
4-
"name": "devcontainers CLI",
4+
"name": "devcontainers CLI (via npm)",
55
"documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/devcontainers-cli",
66
"description": "The devcontainers CLI, which configures devcontainers from devcontainer.json. Requires the Docker client and access to a Docker engine socket (e.g. by mounting the host's Docker socket into the container)",
77
"options": {
88
"version": {
99
"default": "latest",
10-
"description": "Specify the version to install",
10+
"description": "Select the version to install.",
1111
"proposals": [
1212
"latest"
1313
],
@@ -16,6 +16,7 @@
1616
},
1717
"installsAfter": [
1818
"ghcr.io/devcontainers-contrib/features/npm-package",
19+
"ghcr.io/devcontainers/features/node",
1920
"ghcr.io/devcontainers/features/docker-outside-of-docker"
2021
]
2122
}

src/devcontainers-cli/install.sh

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#!/bin/bash -i
21

32
set -e
43

5-
source ./library_scripts.sh
4+
. ./library_scripts.sh
65

76
# nanolayer is a cli utility which keeps container layers as small as possible
87
# source code: https://github.com/devcontainers-contrib/nanolayer
98
# `ensure_nanolayer` is a bash function that will find any existing nanolayer installations,
109
# and if missing - will download a temporary copy that automatically get deleted at the end
1110
# of the script
12-
ensure_nanolayer nanolayer_location "v0.4.39"
11+
ensure_nanolayer nanolayer_location "v0.5.5"
12+
1313

1414
$nanolayer_location \
1515
install \

src/devcontainers-cli/library_scripts.sh

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/bin/bash -i
21

32

43
clean_download() {
@@ -10,13 +9,13 @@ clean_download() {
109
# The above steps will minimize the leftovers being created while installing the downloader
1110
# Supported distros:
1211
# debian/ubuntu/alpine
13-
12+
1413
url=$1
1514
output_location=$2
1615
tempdir=$(mktemp -d)
1716
downloader_installed=""
1817

19-
function _apt_get_install() {
18+
_apt_get_install() {
2019
tempdir=$1
2120

2221
# copy current state of apt list - in order to revert back later (minimize contianer layer size)
@@ -25,7 +24,7 @@ clean_download() {
2524
apt-get -y install --no-install-recommends wget ca-certificates
2625
}
2726

28-
function _apt_get_cleanup() {
27+
_apt_get_cleanup() {
2928
tempdir=$1
3029

3130
echo "removing wget"
@@ -36,15 +35,15 @@ clean_download() {
3635
rm -r /var/lib/apt/lists && mv $tempdir/lists /var/lib/apt/lists
3736
}
3837

39-
function _apk_install() {
38+
_apk_install() {
4039
tempdir=$1
4140
# copy current state of apk cache - in order to revert back later (minimize contianer layer size)
4241
cp -p -R /var/cache/apk $tempdir
4342

4443
apk add --no-cache wget
4544
}
4645

47-
function _apk_cleanup() {
46+
_apk_cleanup() {
4847
tempdir=$1
4948

5049
echo "removing wget"
@@ -100,45 +99,39 @@ ensure_nanolayer() {
10099
local variable_name=$1
101100

102101
local required_version=$2
103-
# normalize version
104-
if ! [[ $required_version == v* ]]; then
105-
required_version=v$required_version
106-
fi
107102

108-
local nanolayer_location=""
103+
local __nanolayer_location=""
109104

110105
# If possible - try to use an already installed nanolayer
111-
if [[ -z "${NANOLAYER_FORCE_CLI_INSTALLATION}" ]]; then
112-
if [[ -z "${NANOLAYER_CLI_LOCATION}" ]]; then
106+
if [ -z "${NANOLAYER_FORCE_CLI_INSTALLATION}" ]; then
107+
if [ -z "${NANOLAYER_CLI_LOCATION}" ]; then
113108
if type nanolayer >/dev/null 2>&1; then
114109
echo "Found a pre-existing nanolayer in PATH"
115-
nanolayer_location=nanolayer
110+
__nanolayer_location=nanolayer
116111
fi
117112
elif [ -f "${NANOLAYER_CLI_LOCATION}" ] && [ -x "${NANOLAYER_CLI_LOCATION}" ] ; then
118-
nanolayer_location=${NANOLAYER_CLI_LOCATION}
119-
echo "Found a pre-existing nanolayer which were given in env variable: $nanolayer_location"
113+
__nanolayer_location=${NANOLAYER_CLI_LOCATION}
114+
echo "Found a pre-existing nanolayer which were given in env variable: $__nanolayer_location"
120115
fi
121116

122117
# make sure its of the required version
123-
if ! [[ -z "${nanolayer_location}" ]]; then
118+
if ! [ -z "${__nanolayer_location}" ]; then
124119
local current_version
125-
current_version=$($nanolayer_location --version)
126-
if ! [[ $current_version == v* ]]; then
127-
current_version=v$current_version
128-
fi
120+
current_version=$($__nanolayer_location --version)
121+
129122

130123
if ! [ $current_version == $required_version ]; then
131124
echo "skipping usage of pre-existing nanolayer. (required version $required_version does not match existing version $current_version)"
132-
nanolayer_location=""
125+
__nanolayer_location=""
133126
fi
134127
fi
135128

136129
fi
137130

138131
# If not previuse installation found, download it temporarly and delete at the end of the script
139-
if [[ -z "${nanolayer_location}" ]]; then
132+
if [ -z "${__nanolayer_location}" ]; then
140133

141-
if [ "$(uname -sm)" == "Linux x86_64" ] || [ "$(uname -sm)" == "Linux aarch64" ]; then
134+
if [ "$(uname -sm)" = 'Linux x86_64' ] || [ "$(uname -sm)" = "Linux aarch64" ]; then
142135
tmp_dir=$(mktemp -d -t nanolayer-XXXXXXXXXX)
143136

144137
clean_up () {
@@ -162,7 +155,7 @@ ensure_nanolayer() {
162155

163156
tar xfzv $tmp_dir/$tar_filename -C "$tmp_dir"
164157
chmod a+x $tmp_dir/nanolayer
165-
nanolayer_location=$tmp_dir/nanolayer
158+
__nanolayer_location=$tmp_dir/nanolayer
166159

167160

168161
else
@@ -172,7 +165,7 @@ ensure_nanolayer() {
172165
fi
173166

174167
# Expose outside the resolved location
175-
declare -g ${variable_name}=$nanolayer_location
168+
export ${variable_name}=$__nanolayer_location
176169

177170
}
178171

test/devcontainers-cli/test.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)