|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +# The first parameter is the version of the release, e.g. 2.4.0 |
| 19 | +# The second parameter is the name of the asset file, e.g. ranger-2.4.0-admin.tar.gz |
| 20 | +# The third parameter is the location and name of the output file, e.g. /tmp/ranger-2.4.0-admin.tar.gz |
| 21 | + |
| 22 | +set -ex |
| 23 | + |
| 24 | +if [ $# -ne 3 ]; then |
| 25 | + echo "Usage: $0 <version> <asset-file> <output-file>" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | + |
| 30 | +TOKEN=${PRIVATE_ACCESS_TOKEN} |
| 31 | +REPO="datastrato/ranger" |
| 32 | +FILE=$2 # the name of your release asset file, e.g. build.tar.gz |
| 33 | +VERSION=$1 # tag name or the word "latest" |
| 34 | +GITHUB="https://api.github.com" |
| 35 | + |
| 36 | +alias err_echo='>&2 echo' |
| 37 | + |
| 38 | +function gh_curl() { |
| 39 | + curl -H "Authorization: token $TOKEN" \ |
| 40 | + -H "Accept: application/vnd.github.v3.raw" \ |
| 41 | + $@ |
| 42 | +} |
| 43 | + |
| 44 | +if [ "$VERSION" = "latest" ]; then |
| 45 | + # Github should return the latest release first. |
| 46 | + parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id" |
| 47 | +else |
| 48 | + parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id" |
| 49 | +fi; |
| 50 | + |
| 51 | +asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases | jq "$parser"` |
| 52 | +if [ "$asset_id" = "null" ]; then |
| 53 | + err_echo "ERROR: version not found $VERSION" |
| 54 | + exit 1 |
| 55 | +fi; |
| 56 | + |
| 57 | +echo $2 |
| 58 | + |
| 59 | +curl -sL --header "Authorization: token $TOKEN" --header 'Accept: application/octet-stream' https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id -o $3 |
0 commit comments