|
| 1 | +GITHUB_COORDINATES="jbox-web/netbox-extractor" |
| 2 | +TOOL_NAME="netbox-extractor" |
| 3 | + |
| 4 | +function install_tool() { |
| 5 | + local version=${2} |
| 6 | + local install_path=${3} |
| 7 | + local tmp_download_dir=${4} |
| 8 | + local binary_name=${5} |
| 9 | + |
| 10 | + local bin_install_path="${install_path}/bin" |
| 11 | + local binary_path="${bin_install_path}/${binary_name}" |
| 12 | + |
| 13 | + local platform |
| 14 | + local architecture |
| 15 | + local download_url |
| 16 | + local download_path |
| 17 | + |
| 18 | + platform="$(get_platform)" |
| 19 | + architecture="$(get_architecture)" |
| 20 | + filename=$(get_filename "${version}" "${platform}" "${architecture}" "${binary_name}") |
| 21 | + download_url=$(get_download_url "${version}" "${platform}" "${architecture}" "${binary_name}") |
| 22 | + download_path="${tmp_download_dir}/${filename}" |
| 23 | + |
| 24 | + echo "Downloading [${binary_name}] (from ${download_url} to ${download_path})" |
| 25 | + curl -Lo "${download_path}" "${download_url}" |
| 26 | + |
| 27 | + echo "Creating bin directory (${bin_install_path})" |
| 28 | + mkdir -p "${bin_install_path}" |
| 29 | + |
| 30 | + echo "Cleaning previous binaries (${binary_path})" |
| 31 | + rm -f "${binary_path}" 2>/dev/null || true |
| 32 | + |
| 33 | + echo "Copying binary" |
| 34 | + cp "${download_path}" "${binary_path}" |
| 35 | + chmod +x "${binary_path}" |
| 36 | +} |
| 37 | + |
| 38 | +function get_filename() { |
| 39 | + local version="${1}" |
| 40 | + local platform="${2}" |
| 41 | + local architecture="${3}" |
| 42 | + local binary_name="${4}" |
| 43 | + |
| 44 | + echo "${binary_name}-${platform}-${architecture}" |
| 45 | +} |
| 46 | + |
| 47 | +function get_download_url() { |
| 48 | + local version="${1}" |
| 49 | + local platform="${2}" |
| 50 | + local architecture="${3}" |
| 51 | + local binary_name="${4}" |
| 52 | + |
| 53 | + local filename |
| 54 | + filename="$(get_filename "${version}" "${platform}" "${architecture}" "${binary_name}")" |
| 55 | + |
| 56 | + echo "https://github.com/${GITHUB_COORDINATES}/releases/download/v${version}/${filename}" |
| 57 | +} |
| 58 | + |
| 59 | +function get_platform() { |
| 60 | + local platform |
| 61 | + |
| 62 | + case "${OSTYPE}" in |
| 63 | + darwin*) platform="darwin" ;; |
| 64 | + linux*) platform="linux" ;; |
| 65 | + *) fail "Unsupported platform" ;; |
| 66 | + esac |
| 67 | + |
| 68 | + echo "${platform}" |
| 69 | +} |
| 70 | + |
| 71 | +function get_architecture() { |
| 72 | + local architecture |
| 73 | + architecture="$(uname -m)" |
| 74 | + |
| 75 | + case "${architecture}" in |
| 76 | + x86_64) architecture="amd64" ;; |
| 77 | + aarch64) architecture="arm64" ;; |
| 78 | + arm64) architecture="arm64" ;; |
| 79 | + *) fail "Unsupported architecture" ;; |
| 80 | + esac |
| 81 | + |
| 82 | + echo "${architecture}" |
| 83 | +} |
| 84 | + |
| 85 | +# stolen from https://github.com/rbenv/ruby-build/pull/631/files#diff-fdcfb8a18714b33b07529b7d02b54f1dR942 |
| 86 | +function sort_versions() { |
| 87 | + sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' | \ |
| 88 | + LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}' |
| 89 | +} |
| 90 | + |
| 91 | +function list_all_versions() { |
| 92 | + local releases_path |
| 93 | + releases_path="https://api.github.com/repos/${GITHUB_COORDINATES}/releases" |
| 94 | + |
| 95 | + local cmd |
| 96 | + cmd="curl -s" |
| 97 | + |
| 98 | + if [ -n "${GITHUB_API_TOKEN:-}" ]; then |
| 99 | + cmd="${cmd} -H 'Authorization: token ${GITHUB_API_TOKEN}'" |
| 100 | + fi |
| 101 | + |
| 102 | + cmd="${cmd} ${releases_path}" |
| 103 | + |
| 104 | + # Fetch all tag names, and get only second column. Then remove all unnecesary characters. |
| 105 | + versions=$(eval ${cmd} | grep -oE "tag_name\": *\".{1,15}\"," | sed 's/tag_name\": *\"v//;s/\",//' | sort_versions) |
| 106 | + echo "${versions}" |
| 107 | +} |
0 commit comments