Skip to content

Commit d6b3d36

Browse files
committed
Implement stripe-cli feature.
1 parent 3bc124b commit d6b3d36

20 files changed

+142
-286
lines changed

.github/workflows/test.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
features:
16-
- color
17-
- hello
16+
- stripe-cli
1817
baseImage:
1918
- debian:latest
2019
- ubuntu:latest
@@ -34,8 +33,7 @@ jobs:
3433
strategy:
3534
matrix:
3635
features:
37-
- color
38-
- hello
36+
- stripe-cli
3937
steps:
4038
- uses: actions/checkout@v3
4139

src/color/README.md

-26
This file was deleted.

src/color/devcontainer-feature.json

-21
This file was deleted.

src/color/install.sh

-26
This file was deleted.

src/hello/devcontainer-feature.json

-22
This file was deleted.

src/hello/install.sh

-29
This file was deleted.
File renamed without changes.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Stripe CLI",
3+
"id": "stripe-cli",
4+
"version": "1.0.0",
5+
"description": "A hello world feature",
6+
"options": {
7+
"version": {
8+
"type": "string",
9+
"proposals": [
10+
"latest"
11+
],
12+
"default": "latest",
13+
"description": "Select version of the Stripe CLI, if not latest."
14+
}
15+
},
16+
"installsAfter": [
17+
"ghcr.io/devcontainers/features/common-utils",
18+
"ghcr.io/devcontainers/features/git"
19+
]
20+
}

src/stripe-cli/install.sh

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env bash
2+
3+
CLI_VERSION=${VERSION:-"latest"}
4+
5+
set -e
6+
7+
# Clean up
8+
rm -rf /var/lib/apt/lists/*
9+
10+
if [ "$(id -u)" -ne 0 ]; then
11+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
12+
exit 1
13+
fi
14+
15+
apt_get_update()
16+
{
17+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
18+
echo "Running apt-get update..."
19+
apt-get update -y
20+
fi
21+
}
22+
23+
# Checks if packages are installed and installs them if not
24+
check_packages() {
25+
if ! dpkg -s "$@" > /dev/null 2>&1; then
26+
apt_get_update
27+
apt-get -y install --no-install-recommends "$@"
28+
fi
29+
}
30+
31+
# Figure out correct version of a three part version number is not passed
32+
find_version_from_git_tags() {
33+
local variable_name=$1
34+
local requested_version=${!variable_name}
35+
if [ "${requested_version}" = "none" ]; then return; fi
36+
local repository=$2
37+
local prefix=${3:-"tags/v"}
38+
local separator=${4:-"."}
39+
local last_part_optional=${5:-"false"}
40+
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
41+
local escaped_separator=${separator//./\\.}
42+
local last_part
43+
if [ "${last_part_optional}" = "true" ]; then
44+
last_part="(${escaped_separator}[0-9]+)?"
45+
else
46+
last_part="${escaped_separator}[0-9]+"
47+
fi
48+
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
49+
local version_list="$(git ls-remote --tags ${repository} | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
50+
if [ "${requested_version}" = "latest" ]; then
51+
declare -g ${variable_name}="$(echo "${version_list}" | head -n 1)"
52+
else
53+
set +e
54+
declare -g ${variable_name}="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
55+
set -e
56+
fi
57+
fi
58+
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" > /dev/null 2>&1; then
59+
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
60+
exit 1
61+
fi
62+
echo "${variable_name}=${!variable_name}"
63+
}
64+
65+
export DEBIAN_FRONTEND=noninteractive
66+
67+
# Install curl, apt-transport-https, curl, gpg, or dirmngr
68+
check_packages curl ca-certificates apt-transport-https dirmngr gnupg2
69+
if ! type git > /dev/null 2>&1; then
70+
check_packages git
71+
fi
72+
73+
# Soft version matching
74+
if [ "${CLI_VERSION}" != "latest" ]; then
75+
find_version_from_git_tags CLI_VERSION "https://github.com/stripe/stripe-cli"
76+
version_suffix="=${CLI_VERSION}"
77+
else
78+
version_suffix=""
79+
fi
80+
81+
# Install the Stripe CLI
82+
echo "Downloading Stripe CLI..."
83+
84+
. /etc/os-release
85+
curl -s https://packages.stripe.dev/api/security/keypair/stripe-cli-gpg/public | gpg --dearmor | sudo tee /usr/share/keyrings/stripe.gpg
86+
echo "deb [signed-by=/usr/share/keyrings/stripe.gpg] https://packages.stripe.dev/stripe-cli-debian-local stable main" | sudo tee -a /etc/apt/sources.list.d/stripe.list
87+
apt-get update
88+
apt-get -y install "stripe${version_suffix}"
89+
echo "Done!"
90+
91+
# Clean up
92+
rm -rf /var/lib/apt/lists/*

test/_global/color_and_hello.sh renamed to test/_global/all_the_clis.sh

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# The 'test/_global' folder is a special test folder that is not tied to a single feature.
44
#
55
# This test file is executed against a running container constructed
6-
# from the value of 'color_and_hello' in the tests/_global/scenarios.json file.
6+
# from the value of 'all_the_clis' in the tests/_global/scenarios.json file.
77
#
88
# The value of a scenarios element is any properties available in the 'devcontainer.json'.
99
# Scenarios are useful for testing specific options in a feature, or to test a combination of features.
@@ -16,16 +16,9 @@ set -e
1616
# Optional: Import test library bundled with the devcontainer CLI
1717
source dev-container-features-test-lib
1818

19-
echo -e "The result of the 'color' command will be:\n"
20-
color
21-
echo -e "The result of the 'hello' command will be:\n"
22-
hello
23-
echo -e "\n"
24-
2519
# Feature-specific tests
2620
# The 'check' command comes from the dev-container-features-test-lib.
27-
check "check purple is my favorite color" bash -c "color | grep 'my favorite color is purple'"
28-
check "check I am greeting with 'Greetings'" bash -c "hello | grep 'Greetings, $(whoami)'"
21+
check "check for stripe" stripe version
2922

3023

3124
# Report result

test/_global/scenarios.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
{
2-
"color_and_hello": {
2+
"all_the_clis": {
33
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
44
"features": {
5-
"color": {
6-
"favorite": "purple"
7-
},
8-
"hello": {
9-
"greeting": "Greetings"
5+
"stripe-cli": {
6+
"version": "latest"
107
}
118
}
129
}

test/color/green.sh

-17
This file was deleted.

test/color/my_favorite_color_is_green.sh

-31
This file was deleted.

test/color/scenarios.json

-33
This file was deleted.

0 commit comments

Comments
 (0)