1- #! /bin/bash -i
1+
22
33clean_download () {
44 # The purpose of this function is to download a file with minimal impact on container layer size
5- # this means if no valid downloader is found (curl or wget) then we install a downloader (currently wget) in a
6- # temporary manner, and making sure to
5+ # this means if no valid downloader is found (curl or wget) then we install a downloader (currently wget) in a
6+ # temporary manner, and making sure to
77 # 1. uninstall the downloader at the return of the function
88 # 2. revert back any changes to the package installer database/cache (for example apt-get lists)
9- # The above steps will minimize the leftovers being created while installing the downloader
9+ # The above steps will minimize the leftovers being created while installing the downloader
1010 # Supported distros:
1111 # debian/ubuntu/alpine
1212
@@ -15,16 +15,16 @@ clean_download() {
1515 tempdir=$( mktemp -d)
1616 downloader_installed=" "
1717
18- function _apt_get_install() {
18+ _apt_get_install () {
1919 tempdir=$1
2020
21- # copy current state of apt list - in order to revert back later (minimize contianer layer size)
22- cp -p -R /var/lib/apt/lists $tempdir
21+ # copy current state of apt list - in order to revert back later (minimize contianer layer size)
22+ cp -p -R /var/lib/apt/lists $tempdir
2323 apt-get update -y
2424 apt-get -y install --no-install-recommends wget ca-certificates
2525 }
2626
27- function _apt_get_cleanup() {
27+ _apt_get_cleanup () {
2828 tempdir=$1
2929
3030 echo " removing wget"
@@ -35,21 +35,20 @@ clean_download() {
3535 rm -r /var/lib/apt/lists && mv $tempdir /lists /var/lib/apt/lists
3636 }
3737
38- function _apk_install() {
38+ _apk_install () {
3939 tempdir=$1
40- # copy current state of apk cache - in order to revert back later (minimize contianer layer size)
41- cp -p -R /var/cache/apk $tempdir
40+ # copy current state of apk cache - in order to revert back later (minimize contianer layer size)
41+ cp -p -R /var/cache/apk $tempdir
4242
4343 apk add --no-cache wget
4444 }
4545
46- function _apk_cleanup() {
46+ _apk_cleanup () {
4747 tempdir=$1
4848
4949 echo " removing wget"
50- apk del wget
50+ apk del wget
5151 }
52-
5352 # try to use either wget or curl if one of them already installer
5453 if type curl > /dev/null 2>&1 ; then
5554 downloader=curl
@@ -76,7 +75,7 @@ clean_download() {
7675 if [ $downloader = " wget" ] ; then
7776 wget -q $url -O $output_location
7877 else
79- curl -sfL $url -o $output_location
78+ curl -sfL $url -o $output_location
8079 fi
8180
8281 # NOTE: the cleanup procedure was not implemented using `trap X RETURN` only because
@@ -90,54 +89,49 @@ clean_download() {
9089 echo " distro not supported"
9190 exit 1
9291 fi
93- fi
92+ fi
9493
9594}
9695
96+
9797ensure_nanolayer () {
9898 # Ensure existance of the nanolayer cli program
9999 local variable_name=$1
100100
101101 local required_version=$2
102- # normalize version
103- if ! [[ $required_version == v* ]]; then
104- required_version=v$required_version
105- fi
106102
107- local nanolayer_location =" "
103+ local __nanolayer_location =" "
108104
109105 # If possible - try to use an already installed nanolayer
110- if [[ -z " ${NANOLAYER_FORCE_CLI_INSTALLATION} " ] ]; then
111- if [[ -z " ${NANOLAYER_CLI_LOCATION} " ] ]; then
106+ if [ -z " ${NANOLAYER_FORCE_CLI_INSTALLATION} " ]; then
107+ if [ -z " ${NANOLAYER_CLI_LOCATION} " ]; then
112108 if type nanolayer > /dev/null 2>&1 ; then
113109 echo " Found a pre-existing nanolayer in PATH"
114- nanolayer_location =nanolayer
110+ __nanolayer_location =nanolayer
115111 fi
116112 elif [ -f " ${NANOLAYER_CLI_LOCATION} " ] && [ -x " ${NANOLAYER_CLI_LOCATION} " ] ; then
117- nanolayer_location =${NANOLAYER_CLI_LOCATION}
118- 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 "
119115 fi
120116
121117 # make sure its of the required version
122- if ! [[ -z " ${nanolayer_location } " ] ]; then
118+ if ! [ -z " ${__nanolayer_location } " ]; then
123119 local current_version
124- current_version=$( $nanolayer_location --version)
125- if ! [[ $current_version == v* ]]; then
126- current_version=v$current_version
127- fi
120+ current_version=$( $__nanolayer_location --version)
121+
128122
129123 if ! [ $current_version == $required_version ]; then
130124 echo " skipping usage of pre-existing nanolayer. (required version $required_version does not match existing version $current_version )"
131- nanolayer_location =" "
125+ __nanolayer_location =" "
132126 fi
133127 fi
134128
135129 fi
136130
137- # If not previuse installation found, download it temporarly and delete at the end of the script
138- if [[ -z " ${nanolayer_location } " ] ]; then
131+ # If not previuse installation found, download it temporarly and delete at the end of the script
132+ if [ -z " ${__nanolayer_location } " ]; then
139133
140- if [ " $( uname -sm) " == " Linux x86_64" ] || [ " $( uname -sm) " = = " Linux aarch64" ]; then
134+ if [ " $( uname -sm) " = ' Linux x86_64' ] || [ " $( uname -sm) " = " Linux aarch64" ]; then
141135 tmp_dir=$( mktemp -d -t nanolayer-XXXXXXXXXX)
142136
143137 clean_up () {
@@ -147,7 +141,7 @@ ensure_nanolayer() {
147141 }
148142 trap clean_up EXIT
149143
150-
144+
151145 if [ -x " /sbin/apk" ] ; then
152146 clib_type=musl
153147 else
@@ -158,11 +152,11 @@ ensure_nanolayer() {
158152
159153 # clean download will minimize leftover in case a downloaderlike wget or curl need to be installed
160154 clean_download https://github.com/devcontainers-contrib/cli/releases/download/$required_version /$tar_filename $tmp_dir /$tar_filename
161-
155+
162156 tar xfzv $tmp_dir /$tar_filename -C " $tmp_dir "
163157 chmod a+x $tmp_dir /nanolayer
164- nanolayer_location =$tmp_dir /nanolayer
165-
158+ __nanolayer_location =$tmp_dir /nanolayer
159+
166160
167161 else
168162 echo " No binaries compiled for non-x86-linux architectures yet: $( uname -m) "
@@ -171,5 +165,8 @@ ensure_nanolayer() {
171165 fi
172166
173167 # Expose outside the resolved location
174- declare -g ${variable_name} =$nanolayer_location
168+ export ${variable_name} =$__nanolayer_location
169+
175170}
171+
172+
0 commit comments