Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring and GITHUB_API_TOKEN variable support #2

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2

[bin/**]
indent_style = tab
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: CI

permissions: {}

on:
workflow_dispatch:
workflow_dispatch: {}
pull_request:
paths-ignore:
- "**.md"
Expand All @@ -18,11 +20,11 @@ jobs:
matrix:
include:
- container: alpine:latest
preinstall: apk add bash coreutils curl git
preinstall: apk add bash coreutils curl git jq
- container: centos:latest
preinstall: dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos && dnf -y install git
preinstall: dnf -y --disablerepo '*' --enablerepo=extras swap centos-linux-repos centos-stream-repos && dnf -y install git jq
- container: ubuntu:latest
preinstall: apt-get -y update && apt-get -y install curl git
preinstall: apt-get -y update && apt-get -y install curl git jq

runs-on: ubuntu-latest

Expand All @@ -37,12 +39,19 @@ jobs:
uses: asdf-vm/actions/plugin-test@v1
with:
command: infracost --version
env:
GITHUB_TOKEN: ${{ github.token }}

macos:
runs-on: macos-latest

steps:
- name: Preinstall
run: brew install jq

- name: Test plugin
uses: asdf-vm/actions/plugin-test@v1
with:
command: infracost --version
env:
GITHUB_TOKEN: ${{ github.token }}
4 changes: 3 additions & 1 deletion .github/workflows/trunk.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Trunk

permissions: {}

on:
workflow_dispatch:
workflow_dispatch: {}
pull_request:
paths-ignore:
- LICENSE
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
?.*
.vscode
7 changes: 6 additions & 1 deletion .trunk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*out
*logs
external
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
2 changes: 1 addition & 1 deletion .trunk/actions
2 changes: 1 addition & 1 deletion .trunk/notifications
35 changes: 26 additions & 9 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
version: 0.1
cli:
version: 1.0.1
version: 1.16.2
lint:
disabled:
- checkov
- trivy
- trufflehog
enabled:
- git-diff-check
- actionlint@1.6.21
- gitleaks@8.15.0
- markdownlint@0.32.2
- prettier@2.7.1
- shellcheck@0.8.0
- shfmt@3.5.0
- actionlint@1.6.26
- gitleaks@8.18.0
- markdownlint@0.37.0
- prettier@3.0.3
- shellcheck@0.9.0
- shfmt@3.6.0
- yamllint@1.32.0
runtimes:
enabled:
- go@1.18.3
- node@16.14.2
- python@3.10.8
- go@1.21.0
- node@18.12.1
plugins:
sources:
- id: trunk
ref: v1.2.5
uri: https://github.com/trunk-io/plugins
actions:
enabled:
- trunk-announce
- trunk-check-pre-push
- trunk-fmt-pre-commit
- trunk-upgrade-available
10 changes: 10 additions & 0 deletions .yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
57 changes: 45 additions & 12 deletions bin/install
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#!/usr/bin/env bash

set -euo pipefail
shopt -s inherit_errexit 2>/dev/null || true

[[ -z ${ASDF_INSTALL_TYPE+x} ]] && echo "ASDF_INSTALL_TYPE is required" && exit 1
[[ -z ${ASDF_INSTALL_VERSION+x} ]] && echo "ASDF_INSTALL_VERSION is required" && exit 1
[[ -z ${ASDF_INSTALL_PATH+x} ]] && echo "ASDF_INSTALL_PATH is required" && exit 1

install() {
function gh_curl() {
if [[ -n ${GITHUB_TOKEN-} ]]; then
curl -fLsS -H "Authorization: token ${GITHUB_TOKEN}" "$@"
elif [[ -n ${GITHUB_API_TOKEN-} ]]; then
curl -fLsS -H "Authorization: token ${GITHUB_API_TOKEN}" "$@"
else
curl -fLsS "$@"
fi
}

function install() {
# set -x
local install_type=$1
[[ ${install_type} != "version" ]] && echo "intall type, ${install_type}, is not supported" && exit 1
[[ ${install_type} != "version" ]] && echo "install type, ${install_type}, is not supported" && exit 1

local prefix="v"
local version=$2
local install_path=$3

local tmp_download_dir

if [[ -z ${TMPDIR:-} ]]; then
if [[ -z ${TMPDIR-} ]]; then
tmp_download_dir=$(mktemp -d)
else
tmp_download_dir=${TMPDIR%/}
Expand All @@ -34,28 +47,48 @@ install() {
esac

local download_url
download_url="https://github.com/infracost/infracost/releases/download/v${version}/infracost-${platform}-${arch}.tar.gz"

local checksum_url
checksum_url="${download_url}.sha256"
local repo="infracost/infracost"
local download_filename="infracost-${platform}-${arch}.tar.gz"
local checksum_filename="${download_filename}.sha256"

if [[ -n ${GITHUB_TOKEN-} ]]; then
local asset_url
asset_url="https://api.github.com/repos/${repo}/releases/tags/${prefix}${version}"
download_url=$(gh_curl -H "Accept: application/vnd.github.v3.raw" "${asset_url}" | jq -r ".assets | map(select(.name == \"${download_filename}\"))[0].url")
if [[ ${download_url} == null ]]; then
echo "Asset ${download_filename} not found in a release ${prefix}${version}."
exit 1
fi
checksum_url=$(gh_curl -H "Accept: application/vnd.github.v3.raw" "${asset_url}" | jq -r ".assets | map(select(.name == \"${checksum_filename}\"))[0].url")
if [[ ${checksum_url} == null ]]; then
echo "Asset ${checksum_filename} not found in a release ${prefix}${version}."
exit 1
fi
else
download_url="https://github.com/${repo}/releases/download/${prefix}${version}/${download_filename}"
checksum_url="https://github.com/${repo}/releases/download/${prefix}${version}/${checksum_filename}"
fi

echo "Downloading ${download_filename} from release ${prefix}${version} from ${download_url}"

echo "Downloading infracost from ${download_url}"
mkdir -p "${tmp_download_dir}"

pushd "${tmp_download_dir}" >/dev/null

curl -sLO "${download_url}"
gh_curl -H "Accept: application/octet-stream" -o "${download_filename}" "${download_url}"

if command -v shasum >/dev/null; then
curl -sL "${checksum_url}" | shasum -a 256 -c
gh_curl -H "Accept: application/octet-stream" "${checksum_url}" | awk "{printf \"%s %s\\n\", \$1, \"${download_filename}\"}" | shasum -a 256 -c
elif command -v sha256sum; then
curl -sL "${checksum_url}" | sha256sum -c
gh_curl -H "Accept: application/octet-stream" "${checksum_url}" | awk "{printf \"%s %s\\n\", \$1, \"${download_filename}\"}" | sha256sum -c
fi

tar zxf "infracost-${platform}-${arch}.tar.gz"
tar zxf "${download_filename}"

mkdir -p "${bin_install_path}"

cp "infracost-${platform}-${arch}" "${bin_install_path}/infracost"
cp -fp "infracost-${platform}-${arch}" "${bin_install_path}/infracost"
chmod +x "${bin_install_path}/infracost"

popd >/dev/null
Expand Down
24 changes: 16 additions & 8 deletions bin/list-all
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#!/usr/bin/env bash

releases_path=https://api.github.com/repos/infracost/infracost/releases
cmd="curl -s"
if [[ -n ${GITHUB_API_TOKEN} ]]; then
cmd="${cmd} -H 'Authorization: token ${GITHUB_API_TOKEN}'"
fi
set -euo pipefail
shopt -s inherit_errexit 2>/dev/null || true

cmd="${cmd} ${releases_path}"
function gh_curl() {
if [[ -n ${GITHUB_TOKEN-} ]]; then
curl -s -H "Authorization: token ${GITHUB_TOKEN}" "$@"
elif [[ -n ${GITHUB_API_TOKEN-} ]]; then
curl -s -H "Authorization: token ${GITHUB_API_TOKEN}" "$@"
else
curl -s "$@"
fi
}

function filter_versions() {
grep -vE '^(0\.[12]\.|0\.3\.0$)'
grep -E '^[0-9]'
}

function sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z\1/; s/$/.z/; G; s/\n/ /' |
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
}

echo $(eval "${cmd}" | grep -oE 'tag_name": ".{1,15}",' | sed 's/tag_name\": \"v//;s/\",//' | filter_versions | sort_versions)
repo="infracost/infracost"
releases_path=https://api.github.com/repos/${repo}/releases

gh_curl "${releases_path}" | grep -oE 'tag_name": "v.{1,15}",' | sed 's/tag_name\": \"v//;s/\",//' | filter_versions | sort_versions | xargs echo
Loading