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/*
0 commit comments