Skip to content

Commit

Permalink
base 0
Browse files Browse the repository at this point in the history
  • Loading branch information
raghu0891 committed Nov 18, 2024
0 parents commit 5593455
Show file tree
Hide file tree
Showing 16 changed files with 410 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Empty file added .env
Empty file.
28 changes: 28 additions & 0 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: "Release a tag"
on:
push:
tags:
- v*

jobs:
release:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- name: Get the version
id: get_version
run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\/v/}
- name: copy with version in name
run: cp ./bin/tool-versions-to-env ./tool-versions-to-env-${{ steps.get_version.outputs.version }}
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
tool-versions-to-env-${{ steps.get_version.outputs.version }}
LICENSE
README.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN}}
27 changes: 27 additions & 0 deletions .github/workflows/test_asdf_plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test asdf plugin
on:
push:

jobs:
test_asdf_plugin:
name: Run asdf plugin test
runs-on: ubuntu-latest
steps:
- name: test asdf plugin
uses: asdf-vm/actions/plugin-test@v1
with:
command: "tool-versions-to-env 3 shfmt_version"
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
test_script:
name: Test script returns correctly
runs-on: ubuntu-latest
steps:
- name: Checkout the Repo
uses: actions/checkout@v3
- name: Verify
run: |
OUTPUT=$(./bin/tool-versions-to-env 3 shfmt_version)
if [ "$OUTPUT" -ne "3.4.2" ]; then
exit 1
fi
3 changes: 3 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
act 0.2.26
shellcheck 0.8.0
shfmt 3.4.3
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Container image that runs your code
FROM ubuntu:latest

# Copies your code file from your action repository to the filesystem path `/` of the container
COPY bin/tool-versions-to-env /tool-versions-to-env

# Code file to execute when the docker container starts up
ENTRYPOINT ["/tool-versions-to-env"]
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
install:
asdf plugin add act https://github.com/grimoh/asdf-act.git || true
asdf plugin add shellcheck || true
asdf plugin add shfmt || true
asdf install

shellcheck:
./scripts/shellcheck

shfmt:
./scripts/shfmt

lint: shellcheck shfmt
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# tool-versions-to-env
![GitHub Actions Status](https://github.com/goplugin/tool-versions-to-env-action/workflows/test%20asdf%20plugin/badge.svg?branch=main)


Is a tool to convert the variables in an asdf .tool-versions file into usable environment variables. It can grab a single variable to be used inline. It can dump the variables out to a .env file. It can dump the variables into a github action output. It outputs each library and version in the form of <library_name>_version=<version_number> for example the library in the .tool-versions file `golang 1.18` will be converted to the equivalent environment variable `golang_version=1.18`.

This is useful when you want to hold versions for certain tools and libraries in one place but use the contained version elsewhere.

# This repo contains:
- tool-versions-to-env shell script to convert .tool-versions into environment variables
- The github action workflow to use this tool in a github action
- The asdf plugin scripts to add the tool using asdf.

## Docs for the tool itself
Requires at least 1 argument
Argument #1: Action options
1 = print to .env file
Argument #2: Optional. The path to the .tool-versions file
Argument #3: Optional. The path to the .env file to create
2 = sent to github action output
Argument #2: Optional. The path to the .tool-versions file
3 = only print given variable to stdout
Argument #2: Required. The tool version to output to stdout
Argument #3: Optional. The path to the .tool-versions file

Example for 1 or 2: tool-versions-to-env 1
Example for 3: tool-versions-to-env 3 golang

## Docs for the Action
### This action sets .tool-versions as variables for other actions to use in the format of
<library_name>_version=<version_number>

### Inputs

### `path_to_tool_versions`

**Required** The path to the .tool-versions file to read

### Outputs

The variables in provided .tool-versions file as variables in the form of <library_name>_version, example: golang_version

### Example usage

uses: goplugin/[email protected]
with:
path_to_tool_versions: "./.tool-versions"

# Docs for the asdf plugin

[tool-versions-to-env](https://github.com/goplugin/tool-versions-to-env-action) plugin for [asdf](https://github.com/asdf-vm/asdf) version manager.

## Install

```shell
asdf plugin add tool-versions-to-env https://github.com/goplugin/tool-versions-to-env-action.git
```

## Usage

Check [asdf](https://github.com/asdf-vm/asdf) readme for instructions on how to install & manage versions of helmenv.
17 changes: 17 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# action.yml
name: 'tool-versions-to-env'
description: 'convert .tool-versions to environment variables in or in outputs'
inputs:
path_to_tool_versions:
description: "Path to the .tool-versions file"
required: true
default: ".tool-versions"
outputs:
generic: # output will be available to future steps
description: 'This command will have generic output variables based on .env'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- "2"
- ${{ inputs.path_to_tool_versions }}
61 changes: 61 additions & 0 deletions bin/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bash

# Heavily based on https://github.com/hypnoglow/asdf-golangci-lint/blob/master/bin/install

set \
-o nounset \
-o pipefail \
-o errexit

app_name="tool-versions-to-env"

install_tool() {
# local -r install_type=${1} #Unused
local -r version=${2}
local -r install_path=${3}

local -r bin_install_path="${install_path}/bin"
local -r binary_path="${bin_install_path}/${app_name}"

local tmp_download_dir
if [[ -n "${TMPDIR:-}" ]]; then
tmp_download_dir=${TMPDIR}
else
tmp_download_dir=$(mktemp -d -t ${app_name}_XXXXXX)
fi

local -r filename="$(get_filename "${version}")"
local -r download_url="$(get_download_url "${version}")"
local -r download_path="${tmp_download_dir}/${filename}"

echo "Downloading ${app_name} from ${download_url} to ${download_path}"
curl -sSLo "${download_path}" "${download_url}"

echo "Creating bin directory"
mkdir -p "${bin_install_path}"

echo "Cleaning previous binaries"
rm -f "${binary_path}" 2>/dev/null || true

echo "Copying binary"
cp "${download_path}" "${binary_path}"
chmod +x "${binary_path}"

echo "Cleaning up downloads"
rm -f "${download_path}"
}

get_filename() {
local -r version="${1}"

echo "${app_name}-${version}"
}

get_download_url() {
local -r version="${1}"
local -r filename="$(get_filename "${version}")"

echo "https://github.com/goplugin/tool-versions-to-env-action/releases/download/v${version}/${filename}"
}

install_tool "${ASDF_INSTALL_TYPE}" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
22 changes: 22 additions & 0 deletions bin/list-all
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set \
-o nounset \
-o pipefail \
-o errexit

releases_path=https://api.github.com/repos/goplugin/tool-versions-to-env-action/releases
cmd="curl -sSL"
if [ -n "${GITHUB_API_TOKEN:-}" ]; then
cmd="$cmd -H 'Authorization: token $GITHUB_API_TOKEN'"
fi
cmd="$cmd $releases_path"

# stolen from https://github.com/rbenv/ruby-build/pull/631/files#diff-fdcfb8a18714b33b07529b7d02b54f1dR942
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}'
}

# Fetch all tag names, and get only second column. Then remove all unnecesary characters.
versions=$(eval "$cmd" | grep -oE "tag_name\": *\".{1,15}\"," | sed 's/tag_name\": *\"v//;s/\",//' | sort_versions)
echo "$versions"
105 changes: 105 additions & 0 deletions bin/tool-versions-to-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bash

set -e

# Script to read .tool-versions library versions into environment variables
# in the form of <library_name>_version=<version_number>

# Create the functions

print_help() {
echo "Requires at least 1 argument"
echo "Argument #1: Action options"
echo " 1 = print to .env file"
echo " Argument #2: Optional. The path to the .tool-versions file"
echo " Argument #3: Optional. The path to the .env file to create"
echo " 2 = sent to github action output"
echo " Argument #2: Optional. The path to the .tool-versions file"
echo " 3 = only print given variable to stdout"
echo " Argument #2: Required. The tool version to output to stdout"
echo " Argument #3: Optional. The path to the .tool-versions file"
echo ""
echo "Example for 1 or 2: tool-versions-to-env 1"
echo "Example for 3: tool-versions-to-env 3 golang"
}

# First argument is a boolean of 0 = false or 1 = true to echo the variable.
# Second argument is the string to echo to std out.
to_echo() {
if [ "$1" -eq 1 ]; then
echo "$2"
fi
}

# First argument is the output option,
# 1 to .env
# 2 to github action output
# 3 none
# Second arument is the .tool-versions location
# Third argument is the .env location
# Fourth argument is whether it is safe to echo to stdout
read_tool_versions_write_to_env() {
local -r how_to_echo="$1"
local -r tool_versions_file="$2"
local -r env_file="$3"
local -r safe_to_echo="$4"

# clear the env file before writing to it later
if [ "$how_to_echo" -eq 1 ]; then
echo "" >"${env_file}"
fi
# loop over each line of the .tool-versions file
while read -r line; do
to_echo "$safe_to_echo" "Original line: $line"

# split the line into a bash array using the default space delimeter
IFS=" " read -r -a lineArray <<<"$line"

# get the key and value from the array, set the key to all uppercase
key="${lineArray[0],,}"
value="${lineArray[1]}"

# ignore comments, comments always start with #
if [[ ${key:0:1} != "#" ]]; then
full_key="${key}_version"
to_echo "${safe_to_echo}" "Parsed line: ${full_key}=${value}"
# echo the variable to the .env file
if [ "$how_to_echo" -eq 1 ]; then
echo "${full_key}=${value}" >>"${env_file}"
elif [ "$how_to_echo" -eq 2 ]; then
echo "::set-output name=${full_key}::$value"
elif [ "$how_to_echo" -eq 3 ]; then
echo "$value"
break
fi
fi
done <"$tool_versions_file"
}

# Run the code

if [ $# -eq 0 ]; then
print_help
exit 1
fi

# Action option
ACTION_OPTION=${1}
# path to the .tool-versions file
TOOL_VERSIONS_FILE=${2:-"./.tool-versions"}
# path to the .env file
ENV_FILE=${3:-"./.env"}

if [ "$ACTION_OPTION" -eq 1 ]; then
# print to .env
read_tool_versions_write_to_env 1 "$TOOL_VERSIONS_FILE" "$ENV_FILE" 1
elif [ "$ACTION_OPTION" -eq 2 ]; then
# print to github action
read_tool_versions_write_to_env 2 "$TOOL_VERSIONS_FILE" "$ENV_FILE" 1
elif [ "$ACTION_OPTION" -eq 3 ]; then
TOOL_VERSIONS_FILE=${3:-"./.tool-versions"}
# print single to variable to stdout
read_tool_versions_write_to_env 3 "$TOOL_VERSIONS_FILE" "" 0
else
echo "First argument was not of option 1, 2, or 3"
fi
Loading

0 comments on commit 5593455

Please sign in to comment.