|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [ "$(gh config get extensions.codeql.debug)" = "true" ] ; then |
| 5 | + set -x |
| 6 | +fi |
| 7 | + |
| 8 | +error() { |
| 9 | + echo "ERROR: $*" 1>&2 |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +rootdir="$(dirname "$0")" |
| 14 | +channel="$(gh config get extensions.codeql.channel)" |
| 15 | + |
| 16 | +if [ -z "$1" ]; then |
| 17 | + cat <<EOF |
| 18 | +GitHub command-line wrapper for the CodeQL CLI. |
| 19 | +
|
| 20 | +Usage: |
| 21 | + gh codeql set-channel [release|nightly] # default: release |
| 22 | + gh codeql set-version [version] # default: latest |
| 23 | + gh codeql list-versions # list all available versions for current channel |
| 24 | + gh codeql list-installed # list installed versions for current channel |
| 25 | + gh codeql cleanup <version> # delete a specific downloaded version |
| 26 | + gh codeql cleanup-all # delete all installed versions for all channels |
| 27 | + gh codeql download [version] # download a specific version (default: latest) |
| 28 | + gh codeql debug [on|off] # enable/disable debug output for gh extension |
| 29 | + gh codeql <anything else> # pass arguments to CodeQL CLI |
| 30 | +
|
| 31 | +Current channel: ${channel:-not specified}. |
| 32 | +Current version: ${version:-not specified}. |
| 33 | +EOF |
| 34 | + exit 0 |
| 35 | +fi |
| 36 | + |
| 37 | +if [ -z "$channel" ] || [ "$channel" = "release" ] ; then |
| 38 | + channel="release" |
| 39 | + repo=github/codeql-cli-binaries |
| 40 | +elif [ "$channel" = "nightly" ] ; then |
| 41 | + repo=dsp-testing/codeql-cli-nightlies |
| 42 | +else |
| 43 | + error "Channel must be 'release' or 'nightly'." |
| 44 | +fi |
| 45 | + |
| 46 | +# determine platform using OSTYPE |
| 47 | +platform="$(gh config get extensions.codeql.platform)" |
| 48 | +if [[ -z "$platform" ]] ; then |
| 49 | + if [[ "$OSTYPE" == "darwin"* ]] ; then |
| 50 | + platform=osx64 |
| 51 | + elif [[ "$OSTYPE" == "linux"* ]] ; then |
| 52 | + platform=linux64 |
| 53 | + elif [[ "$OSTYPE" == "win"* ]] || [[ $OSTYPE == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] ; then |
| 54 | + platform=win64 |
| 55 | + else |
| 56 | + error "Couldn't determine platform from OSTYPE='$OSTYPE'. Consider setting extensions.codeql.platform." |
| 57 | + fi |
| 58 | +fi |
| 59 | + |
| 60 | +# Handle debug command. |
| 61 | +if [ "$1" = "debug" ] ; then |
| 62 | + if [ "$2" = "on" ] ; then |
| 63 | + gh config set extensions.codeql.debug true |
| 64 | + elif [ "$2" = "off" ] ; then |
| 65 | + gh config set extensions.codeql.debug false |
| 66 | + else |
| 67 | + error "Invalid debug command: '$2'." |
| 68 | + fi |
| 69 | + exit 0 |
| 70 | +fi |
| 71 | + |
| 72 | +# Handle list-versions command. |
| 73 | +if [ "$1" = "list-versions" ]; then |
| 74 | + gh api "repos/$repo/releases" --paginate --jq ".[].tag_name" |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 78 | +# Handle set-channel command. |
| 79 | +if [ "$1" = "set-channel" ]; then |
| 80 | + if [ "$2" != "release" ] && [ "$2" != "nightly" ]; then |
| 81 | + error "Invalid channel: '$2'." |
| 82 | + fi |
| 83 | + old_channel="$(gh config get extensions.codeql.channel)" |
| 84 | + if [ "${old_channel:-release}" != "$2" ] ; then |
| 85 | + gh config set extensions.codeql.channel "$2" |
| 86 | + gh config set extensions.codeql.version "" |
| 87 | + echo "Switched to $2 channel. Any pinned version has been unset." |
| 88 | + else |
| 89 | + echo "Channel already set to $2." |
| 90 | + fi |
| 91 | + exit 0 |
| 92 | +fi |
| 93 | + |
| 94 | +function download() { |
| 95 | + local version="$1" |
| 96 | + if [ -z "$version" ] || [ "$version" = "latest" ]; then |
| 97 | + version=$(gh api "repos/$repo/releases/latest" --jq '.tag_name') |
| 98 | + fi |
| 99 | + if [ -x "$rootdir/dist/$channel/$version/codeql" ] ; then |
| 100 | + # Already downloaded. |
| 101 | + return 0 |
| 102 | + fi |
| 103 | + id=$(gh api "repos/$repo/releases" --paginate --jq ".[] | select(.tag_name == \"$version\") | .id" | head -n1) |
| 104 | + if [ -z "$id" ]; then |
| 105 | + error "Version '$1' not found." |
| 106 | + fi |
| 107 | + mkdir -p "$rootdir/dist/$channel" |
| 108 | + tempdir="$(mktemp -d "$rootdir/dist/$channel/temp_$version.XXXXXXXX")" |
| 109 | + trap 'rm -rf "$tempdir"' EXIT |
| 110 | + echo "Downloading CodeQL CLI version $version..." |
| 111 | + gh release download -R "$repo" "$version" --pattern "codeql-$platform.zip" --dir "$tempdir" |
| 112 | + echo "Unpacking CodeQL CLI version $version..." |
| 113 | + unzip -oq "$tempdir/codeql-$platform.zip" -d "$tempdir" |
| 114 | + mv "$tempdir/codeql" "$rootdir/dist/$channel/$version" |
| 115 | + rm -rf "$tempdir" |
| 116 | +} |
| 117 | + |
| 118 | +function set_version() { |
| 119 | + local version="$1" |
| 120 | + if [ -z "$version" ]; then |
| 121 | + error "Version must be specified. Use 'latest' to automatically determine the latest version." |
| 122 | + elif [ "$version" = "latest" ]; then |
| 123 | + version="$(gh api "repos/$repo/releases/latest" --jq ".tag_name")" |
| 124 | + fi |
| 125 | + download "$version" |
| 126 | + gh config set extensions.codeql.version "$version" |
| 127 | +} |
| 128 | + |
| 129 | +# Handle the download command. |
| 130 | +if [ "$1" = "download" ]; then |
| 131 | + download "$2" |
| 132 | + exit 0 |
| 133 | +fi |
| 134 | + |
| 135 | +# Handle the set-version command. |
| 136 | +if [ "$1" = "set-version" ]; then |
| 137 | + set_version "$2" |
| 138 | + version="$(gh config get extensions.codeql.version)" |
| 139 | + exec "$rootdir/dist/$channel/$version/codeql" version |
| 140 | +fi |
| 141 | + |
| 142 | +# Handle the list-installed command. |
| 143 | +if [ "$1" = "list-installed" ]; then |
| 144 | + ( cd "$rootdir/dist/$channel" ; find . -depth 1 -type d | cut -c3- ; ) |
| 145 | + exit 0 |
| 146 | +fi |
| 147 | + |
| 148 | +# Handle the cleanup command. |
| 149 | +if [ "$1" = "cleanup" ]; then |
| 150 | + if [ -z "$2" ]; then |
| 151 | + error "Version must be specified." |
| 152 | + elif [ ! -d "$rootdir/dist/$channel/$2" ]; then |
| 153 | + error "Version '$2' not found." |
| 154 | + fi |
| 155 | + rm -rf "$rootdir/dist/$channel/$2" |
| 156 | + exit 0 |
| 157 | +fi |
| 158 | + |
| 159 | +# Handle the cleanup-all command |
| 160 | +if [ "$1" = "cleanup-all" ]; then |
| 161 | + rm -rf "$rootdir/dist" |
| 162 | + exit 0 |
| 163 | +fi |
| 164 | + |
| 165 | +version="$(gh config get extensions.codeql.version)" |
| 166 | +if [ -z "$version" ]; then |
| 167 | + set_version latest |
| 168 | + version="$(gh config get extensions.codeql.version)" |
| 169 | +fi |
| 170 | +download "$version" |
| 171 | +exec "$rootdir/dist/$channel/$version/codeql" "$@" |
0 commit comments