Skip to content

Commit

Permalink
New ENV command for environment variables
Browse files Browse the repository at this point in the history
ENV can be either used to pass environment variables to be used within
the image or be substituted beforehand. This allows another kind of
variables within a Pifile.
  • Loading branch information
oxzi committed Dec 7, 2021
1 parent 28850e0 commit f9c137d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- New `ENV` command for environment variables.

## [0.4.4] - 2021-11-09
### Added
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ The optionally permission mode (*chmod*) can be set as the first parameter.
#### `WORKDIR /my/guest/path`
`WORKDIR` sets the working directory within the image.

#### `ENV KEY [VALUE]`
`ENV` either sets or unsets an environment variable to be used within the image.
If two parameters are given, the first is the key and the second the value.
If one parameter is given, the environment variable will be removed.

An environment variable can be either used via `$VAR` within another sub-shell (`sh -c 'echo $VAR'`) or substituted beforehand via `@@VAR@@`.

```
ENV FOO BAR
RUN sh -c 'echo FOO = $FOO' # FOO = BAR - substituted within a sh in the image
RUN echo FOO = @@FOO@@ # FOO = BAR - substituted beforehand via pimod
ENV FOO
```

#### `RUN CMD [PARAMS...]`
`RUN` executes a command in the chrooted image based on QEMU user emulation.

Expand Down
10 changes: 10 additions & 0 deletions examples/RPi-RaspberryPiOSLite.Pifile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ RUN bash -c "
echo hello
echo world
"

# test environment vars via ENV
ENV FOO BAR
RUN sh -c 'echo FOO = $FOO'
RUN echo FOO = @@FOO@@

# unset FOO again
ENV FOO
RUN sh -c 'echo FOO = $FOO'
RUN echo FOO = @@FOO@@
44 changes: 44 additions & 0 deletions modules/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ENV_VARS is an associative array of environment variables, set via ENV.
declare -A ENV_VARS=()

# env_vars_set saves an environment variable mapping.
# Usage: env_vars_set KEY VALUE
env_vars_set() {
ENV_VARS["${1}"]="${2}"
}

# env_vars_del removes an environment variable mapping.
# Usage: env_vars_del KEY
env_vars_del() {
unset ENV_VARS["${1}"]
}

# env_vars_export_cmd creates a single "export K1=V1 K2=V2 ...;" output. If no
# values are present, an empty output is generated.
# Usage: env_vars_export_cmd
env_vars_export_cmd() {
if [[ "${#ENV_VARS[@]}" -eq "0" ]]; then
echo ""
return
fi

declare -a pairs

for key in "${!ENV_VARS[@]}"; do
pairs+=("${key}=${ENV_VARS["$key"]}")
done

echo "export ${pairs[*]};"
}

# env_vars_subst replaces all previously defined environment variables in the
# form of "@@ENV@@" by its value.
# Usage: env_vars_subst echo hello @@USER_NAME@@
env_vars_subst() {
for part in "${@}"; do
for key in "${!ENV_VARS[@]}"; do
part="${part/"@@${key}@@"/${ENV_VARS["$key"]}}"
done
printf '%s ' "$part"
done
}
1 change: 1 addition & 0 deletions pimod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -euE
pushd "$(dirname "$0")" > /dev/null

. ./modules/chroot.sh
. ./modules/env.sh
. ./modules/error.sh
. ./modules/esceval.sh
. ./modules/from_remote.sh
Expand Down
4 changes: 4 additions & 0 deletions stages/00-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ WORKDIR() {
:
}

ENV() {
:
}

RUN() {
:
}
Expand Down
39 changes: 38 additions & 1 deletion stages/30-chroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ WORKDIR() {
echo -e "\033[0;32m### WORKDIR ${WORKDIR_PATH}\033[0m"
}

# ENV either sets or unsets an environment variable to be used within the image.
# If two parameters are given, the first is the key and the second the value.
# If one parameter is given, the environment variable will be removed.
#
# An environment variable can be either used via $VAR within another sub-shell
# (sh -c 'echo $VAR') or substituted beforehand via @@VAR@@.
#
# Usage: ENV KEY [VALUE]
ENV() {
local key=""
local value=""

case "$#" in
"1")
key="$1"
env_vars_del "$key"
;;

"2")
key="$1"
value="$2"
env_vars_set "$key" "$value"
;;

*)
echo -e "\033[0;31m### Error: ENV KEY [VALUE]\033[0m"
return 1
;;
esac

echo -e "\033[0;32m### ENV ${key}=${value}\033[0m"
}

# RUN executes a command in the chrooted image based on QEMU user emulation.
#
# Caveat: because the Pifile is just a Bash script, pipes do not work as one
Expand All @@ -69,8 +102,12 @@ WORKDIR() {
# Usage: RUN CMD PARAMS...
RUN() {
echo -e "\033[0;32m### RUN ${*}\033[0m"

local cmd_esceval="$(esceval "$@")"
local cmd_env_subst="$(env_vars_subst "$cmd_esceval")"

PATH=${GUEST_PATH} chroot "${CHROOT_MOUNT}" \
/bin/sh -c "cd ${WORKDIR_PATH}; $(esceval "$@")"
/bin/sh -c "cd ${WORKDIR_PATH}; $(env_vars_export_cmd) ${cmd_env_subst}"
}

# HOST executed a command on the local host and can be used to prepare files,
Expand Down

0 comments on commit f9c137d

Please sign in to comment.