Skip to content

Commit 80f03db

Browse files
Merge pull request #23 from theohbrothers/refactor/update-functions-for-generating-docker-run-statements
Refactor: Update functions for generating docker run statements
2 parents feaaeda + 9715344 commit 80f03db

File tree

4 files changed

+61
-46
lines changed

4 files changed

+61
-46
lines changed

generate/templates/Dockerfile.ps1

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@ function global:Set-Checksums($k, $url) {
44
$global:CHECKSUMS[$k] = if ($global:CHECKSUMS[$k]) { $global:CHECKSUMS[$k] } else { [System.Text.Encoding]::UTF8.GetString( (Invoke-WebRequest $url).Content ) -split "`n" }
55
}
66
function global:Get-ChecksumsFile ($k, $keyword) {
7-
$global:CHECKSUMS[$k] | ? { $_ -match $keyword } | % { $_ -split "\s" } | Select-Object -Last 1 | % { $_.TrimStart('*') }
7+
$file = $global:CHECKSUMS[$k] | ? { $_ -match $keyword } | % { $_ -split "\s" } | Select-Object -Last 1 | % { $_.TrimStart('*') }
8+
if ($file) {
9+
$file
10+
}else {
11+
"No file among $k checksums matching regex: $keyword" | Write-Warning
12+
}
813
}
914
function global:Get-ChecksumsSha ($k, $keyword) {
10-
$global:CHECKSUMS[$k] | ? { $_ -match $keyword } | % { $_ -split "\s" } | Select-Object -First 1
15+
$sha = $global:CHECKSUMS[$k] | ? { $_ -match $keyword } | % { $_ -split "\s" } | Select-Object -First 1
16+
if ($sha) {
17+
$sha
18+
}else {
19+
"No sha among $k checksums matching regex: $keyword" | Write-Warning
20+
}
1121
}
1222

1323
# Global functions
@@ -18,64 +28,70 @@ function global:Generate-DownloadBinary ($o) {
1828
$checksumsUrl = "$releaseUrl/$( $o['checksums'] )"
1929
Set-Checksums $o['binary'] $checksumsUrl
2030

21-
$binaryUpper = $o['binary'].ToUpper()
31+
$shellVariable = "$( $o['binary'].ToUpper() -replace '[^A-Za-z0-9_]', '_' )_VERSION"
2232
@"
2333
# Install $( $o['binary'] )
2434
RUN set -eux; \
25-
export $( $binaryUpper )_VERSION="$( $o['version'] )"; \
35+
$shellVariable=$( $o['version'] ); \
2636
case "`$( uname -m )" in \
2737
2838
"@
39+
40+
$o['architectures'] = if ($o.Contains('architectures')) { $o['architectures'] } else { 'linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le,linux/riscv64,linux/s390x' }
2941
foreach ($a in ($o['architectures'] -split ',') ) {
3042
$split = $a -split '/'
3143
$os = $split[0]
3244
$arch = $split[1]
3345
$archv = if ($split.Count -gt 2) { $split[2] } else { '' }
3446
switch ($a) {
3547
"$os/386" {
36-
$regex = "$os[-_](i?$arch|x86)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
3748
$hardware = 'x86'
49+
$regex = "$os[-_](i?$arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
3850
}
3951
"$os/amd64" {
40-
$regex = "$os[-_]($arch|x86_64)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
4152
$hardware = 'x86_64'
53+
$regex = "$os[-_]($arch|x86(_64)?)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
4254
}
4355
"$os/arm/v6" {
44-
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$"
4556
$hardware = 'armhf'
57+
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$"
4658
}
4759
"$os/arm/v7" {
48-
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$"
4960
$hardware = 'armv7l'
61+
$regex = "$os[-_]($arch|arm)[-_]?($archv)?$( [regex]::Escape($o['archiveformat']) )$"
5062
}
5163
"$os/arm64" {
52-
$regex = "$os[-_]($arch|aarch64)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
5364
$hardware = 'aarch64'
65+
$regex = "$os[-_]($arch|aarch64)[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
5466
}
5567
"$os/ppc64le" {
56-
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
5768
$hardware = 'ppc64le'
69+
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
5870
}
5971
"$os/riscv64" {
60-
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
6172
$hardware = 'riscv64'
73+
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
6274
}
6375
"$os/s390x" {
64-
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
6576
$hardware = 's390x'
77+
$regex = "$os[-_]$arch[-_]?$archv$( [regex]::Escape($o['archiveformat']) )$"
6678
}
6779
default {
6880
throw "Unsupported architecture: $a"
6981
}
7082
}
7183

84+
$file = Get-ChecksumsFile $o['binary'] $regex
85+
if ($file) {
86+
$sha = Get-ChecksumsSha $o['binary'] $regex
7287
@"
73-
'$hardware') \
74-
URL=$releaseUrl/$( Get-ChecksumsFile $o['binary'] $regex ); \
75-
SHA256=$( Get-ChecksumsSha $o['binary'] $regex ); \
88+
'$hardware') \
89+
URL=$releaseUrl/$file; \
90+
SHA256=$sha; \
7691
;; \
7792
7893
"@
94+
}
7995
}
8096

8197
@"
@@ -119,14 +135,13 @@ RUN set -eux; \
119135
gzip -d "`$FILE"; \
120136
121137
"@
122-
}else {
123-
throw "Invalid 'archiveformat'. Supported formats: .tar.gz, .tgz, .bz2, .gz"
124138
}
125139

140+
$destination = if ($o.Contains('destination')) { $o['destination'] } else { "/usr/local/bin/$( $o['binary'] )" }
126141
@"
127-
mv -v $( $o['binary'] ) /usr/local/bin/$( $o['binary'] ); \
128-
chmod +x /usr/local/bin/$( $o['binary'] ); \
129-
$( $o['binary'] ) $( $o['versionSubcommand'] ); \
142+
mv -v $( $o['binary'] ) $destination; \
143+
chmod +x $destination; \
144+
$( $o['testCommand'] ); \
130145
131146
"@
132147

@@ -192,7 +207,7 @@ foreach ($c in $VARIANT['_metadata']['components']) {
192207
)
193208
checksums = 'pingme_checksums.txt'
194209
architectures = $VARIANT['_metadata']['platforms']
195-
versionSubcommand = '--version'
210+
testCommand = 'pingme --version'
196211
}
197212
}
198213

@@ -204,7 +219,7 @@ foreach ($c in $VARIANT['_metadata']['components']) {
204219
archiveformat = '.bz2'
205220
checksums = 'SHA256SUMS'
206221
architectures = $VARIANT['_metadata']['platforms']
207-
versionSubcommand = 'version'
222+
testCommand = 'restic version'
208223
}
209224
}
210225
}

variants/1.4.4-pingme/Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ RUN set -eux; \
2828

2929
# Install pingme
3030
RUN set -eux; \
31-
export PINGME_VERSION="v0.2.5"; \
31+
PINGME_VERSION=v0.2.5; \
3232
case "$( uname -m )" in \
33-
'x86') \
34-
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_i386.tar.gz; \
33+
'x86') \
34+
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_x86_64.tar.gz; \
3535
SHA256=5a14e80693800284f11daf7d5ba71a7cbe78e18948579584f36069d7a2f31d4a; \
3636
;; \
37-
'x86_64') \
37+
'x86_64') \
3838
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_x86_64.tar.gz; \
3939
SHA256=93133b9c978d5a579526261255c2a7a9ca6dfc5ab42ef65e1de4fab15d8ac808; \
4040
;; \
41-
'armv7l') \
41+
'armv7l') \
4242
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_armv7.tar.gz; \
4343
SHA256=6f26a3926e6ed038ca132b4d1985cd2f6c0ccf037fbc78f710bdc2cc76b3fc5a; \
4444
;; \
45-
'aarch64') \
45+
'aarch64') \
4646
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_arm64.tar.gz; \
4747
SHA256=496bb93402611d5710bc66b26f64f13fc0f888d0b3cc1f4d7960c7c631860dd3; \
4848
;; \

variants/1.4.4-restic-pingme/Dockerfile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ RUN set -eux; \
2828

2929
# Install restic
3030
RUN set -eux; \
31-
export RESTIC_VERSION="v0.15.1"; \
31+
RESTIC_VERSION=v0.15.1; \
3232
case "$( uname -m )" in \
33-
'x86') \
33+
'x86') \
3434
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_386.bz2; \
3535
SHA256=50362f6c4c2d91cf0edc750c578b73605fdbb79443874110cc0a64913553f76b; \
3636
;; \
37-
'x86_64') \
37+
'x86_64') \
3838
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_amd64.bz2; \
3939
SHA256=3631e3c3833c84ba71f22ea3df20381676abc7476a7f6d14424d9abfada91414; \
4040
;; \
41-
'armv7l') \
41+
'armv7l') \
4242
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_arm.bz2; \
4343
SHA256=1a5c1d2a3b17aa381c318b3f3919f7cfc4cd430c3a2c3053ba055fb4ccf38c97; \
4444
;; \
45-
'aarch64') \
45+
'aarch64') \
4646
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_arm64.bz2; \
4747
SHA256=bf6e09743df6899a02f0647d899fb008932760ea872667287bbc47b42091a3b0; \
4848
;; \
@@ -62,21 +62,21 @@ RUN set -eux; \
6262

6363
# Install pingme
6464
RUN set -eux; \
65-
export PINGME_VERSION="v0.2.5"; \
65+
PINGME_VERSION=v0.2.5; \
6666
case "$( uname -m )" in \
67-
'x86') \
68-
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_i386.tar.gz; \
67+
'x86') \
68+
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_x86_64.tar.gz; \
6969
SHA256=5a14e80693800284f11daf7d5ba71a7cbe78e18948579584f36069d7a2f31d4a; \
7070
;; \
71-
'x86_64') \
71+
'x86_64') \
7272
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_x86_64.tar.gz; \
7373
SHA256=93133b9c978d5a579526261255c2a7a9ca6dfc5ab42ef65e1de4fab15d8ac808; \
7474
;; \
75-
'armv7l') \
75+
'armv7l') \
7676
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_armv7.tar.gz; \
7777
SHA256=6f26a3926e6ed038ca132b4d1985cd2f6c0ccf037fbc78f710bdc2cc76b3fc5a; \
7878
;; \
79-
'aarch64') \
79+
'aarch64') \
8080
URL=https://github.com/kha7iq/pingme/releases/download/v0.2.5/pingme_Linux_arm64.tar.gz; \
8181
SHA256=496bb93402611d5710bc66b26f64f13fc0f888d0b3cc1f4d7960c7c631860dd3; \
8282
;; \

variants/1.4.4-restic/Dockerfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ RUN set -eux; \
2828

2929
# Install restic
3030
RUN set -eux; \
31-
export RESTIC_VERSION="v0.15.1"; \
31+
RESTIC_VERSION=v0.15.1; \
3232
case "$( uname -m )" in \
33-
'x86') \
33+
'x86') \
3434
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_386.bz2; \
3535
SHA256=50362f6c4c2d91cf0edc750c578b73605fdbb79443874110cc0a64913553f76b; \
3636
;; \
37-
'x86_64') \
37+
'x86_64') \
3838
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_amd64.bz2; \
3939
SHA256=3631e3c3833c84ba71f22ea3df20381676abc7476a7f6d14424d9abfada91414; \
4040
;; \
41-
'armhf') \
41+
'armhf') \
4242
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_arm.bz2; \
4343
SHA256=1a5c1d2a3b17aa381c318b3f3919f7cfc4cd430c3a2c3053ba055fb4ccf38c97; \
4444
;; \
45-
'armv7l') \
45+
'armv7l') \
4646
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_arm.bz2; \
4747
SHA256=1a5c1d2a3b17aa381c318b3f3919f7cfc4cd430c3a2c3053ba055fb4ccf38c97; \
4848
;; \
49-
'aarch64') \
49+
'aarch64') \
5050
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_arm64.bz2; \
5151
SHA256=bf6e09743df6899a02f0647d899fb008932760ea872667287bbc47b42091a3b0; \
5252
;; \
53-
's390x') \
53+
's390x') \
5454
URL=https://github.com/restic/restic/releases/download/v0.15.1/restic_0.15.1_linux_s390x.bz2; \
5555
SHA256=1ed7632518a86fa468f5823d6da4826d1787845cc0969a46da110c98139a3db4; \
5656
;; \

0 commit comments

Comments
 (0)