Skip to content

Commit

Permalink
Add fetch script (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 9, 2020
1 parent 97b1cb0 commit 8f3e546
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
52 changes: 52 additions & 0 deletions scripts/fetch-canonicaldatasyncer
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

set -eo pipefail

readonly LATEST='https://api.github.com/repos/exercism/canonical-data-syncer/releases/latest'

case "$(uname)" in
(Darwin*) OS='mac' ;;
(Linux*) OS='linux' ;;
(Windows*) OS='windows' ;;
(MINGW*) OS='windows' ;;
(MSYS_NT-*) OS='windows' ;;
(*) OS='linux' ;;
esac

case "$OS" in
(windows*) EXT='zip' ;;
(*) EXT='tgz' ;;
esac

case "$(uname -m)" in
(*64*) ARCH='64bit' ;;
(*686*) ARCH='32bit' ;;
(*386*) ARCH='32bit' ;;
(*) ARCH='64bit' ;;
esac

if [ -z "${GITHUB_TOKEN}" ]
then
HEADER=''
else
HEADER="authorization: Bearer ${GITHUB_TOKEN}"
fi

FILENAME="canonicaldatasyncer-${OS}-${ARCH}.${EXT}"

get_url () {
curl --header "$HEADER" -s "$LATEST" |
awk -v filename=$FILENAME '$1 ~ /browser_download_url/ && $2 ~ filename { print $2 }' |
tr -d '"'
}

URL=$(get_url)

case "$EXT" in
(*zip)
curl --header "$HEADER" -s --location "$URL" -o bin/latest-canonicaldatasyncer.zip
unzip bin/latest-canonicaldatasyncer.zip -d bin/
rm bin/latest-canonicaldatasyncer.zip
;;
(*) curl --header "$HEADER" -s --location "$URL" | tar xz -C bin/ ;;
esac
24 changes: 24 additions & 0 deletions scripts/fetch-canonicaldatasyncer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Function DownloadUrl ([string] $FileName, $Headers) {
$latestUrl = "https://api.github.com/repos/exercism/canonical-data-syncer/releases/latest"
$json = Invoke-RestMethod -Headers $Headers -Uri $latestUrl
$json.assets | Where-Object { $_.browser_download_url -match $FileName } | Select-Object -ExpandProperty browser_download_url
}

Function Headers {
If ($GITHUB_TOKEN) { @{ Authorization = "Bearer ${GITHUB_TOKEN}" } } Else { @{ } }
}

Function Arch {
If ([Environment]::Is64BitOperatingSystem) { "64bit" } Else { "32bit" }
}

$arch = Arch
$headers = Headers
$fileName = "canonicaldatasyncer-windows-$arch.zip"
$outputDirectory = "bin"
$outputFile = Join-Path -Path $outputDirectory -ChildPath $fileName
$zipUrl = DownloadUrl -FileName $fileName -Headers $headers

Invoke-WebRequest -Headers $headers -Uri $zipUrl -OutFile $outputFile
Expand-Archive $outputFile -DestinationPath $outputDirectory -Force
Remove-Item -Path $outputFile

0 comments on commit 8f3e546

Please sign in to comment.