Files
git.taskfile.build/patterns.md
T

6.8 KiB

layout
layout
layouts/base.njk

Patterns

The task file from the specification is very basic. Commands can also have parameters and call each other.

The following is a collection of more complex task file patterns.

Set default parameter

Fallback to a default value for a parameter.

build() {
    local platform="amd64"
    if [[ -n "$1" ]]; then
        platform="$1"
    fi

Ensure parameter is not empty

Check the first param and exit if it is empty.

deploy() {
    if [[ -z "$1" ]]; then echo "\$1 is empty."; exit; fi

Prompt for input

Use read to ask for inputs.

if [[ -z "$2" ]]; then
	read -p 'Enter the task description: ' task_description
else
	task_description="$2"
fi

Static env vars

These vars are exported from the script and used in external tools.

CONFIGURATION_FILE=${CONFIGURATION_FILE:=file.conf}
GIT_BRANCH=$(git symbolic-ref --short -q HEAD)

Updateable vars

This variables can be configured from the environment, but is used only internally.

container_engine=${CONTAINER_ENGINE:=docker}
compose_command=${COMPOSE_COMMAND:=docker compose}

Conditional vars

Depending on the value of another var, this vars value changes.

if [[ "$container_engine" == "podman" ]]; then
    compose_command="podman-compose"
fi

Template with env vars

Create a parameterized file from a template. Requires envsubst.

template-with-env() {
    echo "Template $CONFIGURATION_FILE"

    export CONFIGURATION_1
    export CONFIGURATION_2=${CONFIGURATION_2:=value