Skip to content

Commit

Permalink
Merge pull request #13 from RossyWhite/action
Browse files Browse the repository at this point in the history
Action
  • Loading branch information
RossyWhite authored Jan 3, 2023
2 parents 78c86fd + 86db285 commit 97b3fe4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ on:
- 'main'
tags-ignore:
- '**'

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
os: [ubuntu-latest, macOS-latest]
runs-on: ${{ matrix.os }}
name: Build & Test
steps:
Expand All @@ -28,3 +29,7 @@ jobs:
${{ runner.os }}-go-
- name: test
run: go test -race -cover ./...
- name: install test
run: ./install.sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ dockers:
image_templates:
- "ghcr.io/rossywhite/tfsubst:{{ .Tag }}"
- "ghcr.io/rossywhite/tfsubst:latest"
build_flag_templates:
- "--label=org.opencontainers.image.source=https://github.com/RossyWhite/tfsubst"

archives:
- name_template: >-
Expand Down
87 changes: 87 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/sh

set -e

get_arch() {
a=$(uname -m)
case ${a} in
"x86_64" | "amd64")
echo "x86_64"
;;
"i386")
echo "i386"
;;
"aarch64" | "arm64" | "arm")
echo "arm64"
;;
*)
echo "${NIL}"
;;
esac
}

get_os() {
uname -s | awk '{print tolower($0)}'
}

owner="RossyWhite"
repo="tfsubst"
exe_name="tfsubst"
version=""

# parse flag
for i in "$@"; do
case $i in
-v=* | --version=*)
version="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done

downloadFolder="$(mktemp -d)"
os=$(get_os)
arch=$(get_arch)
file_name="${exe_name}_${os}_${arch}.tar.gz"
downloaded_file="${downloadFolder}/${file_name}"
executable_folder="/usr/local/bin"

# if version is empty
if [ -z "$version" ]; then
version="latest"
fi

asset_id=$(curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
-sSf https://api.github.com/repos/"${owner}"/"${repo}"/releases/"${version}" \
| jq -r ".assets[] | select(.name == \"${file_name}\") | .id")

echo "[1/3] Downloading ${file_name}"
rm -f "${downloaded_file}"
curl -fsSL -H "Accept: application/octet-stream" \
-H "Authorization: Bearer ${GITHUB_TOKEN}"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/"${owner}"/"${repo}"/releases/assets/"${asset_id}" \
-o "${downloaded_file}"

echo "[2/3] Install ${exe_name} to the ${executable_folder}"
tar -xz -f "${downloaded_file}" -C ${executable_folder}
exe=${executable_folder}/${exe_name}
chmod +x "${exe}"

echo "[3/3] Set environment variables"
echo "${exe_name} was installed successfully to ${exe}"
if command -v "$exe_name" --help >/dev/null; then
echo "Run '$exe_name --help' to get started"
else
echo "Manually add the directory to your \$HOME/.bash_profile (or similar)"
echo " export PATH=${executable_folder}:\$PATH"
echo "Run '$exe_name --help' to get started"
fi

exit 0

0 comments on commit 97b3fe4

Please sign in to comment.