Skip to content

Commit

Permalink
Export alt values (YADM_*) when running hooks and bootstrap
Browse files Browse the repository at this point in the history
Fixes #291.
  • Loading branch information
erijo committed Jan 17, 2021
1 parent c834977 commit 2a66ccb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions test/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_bootstrap(
paths.bootstrap.write(
'#!/bin/bash\n'
f'echo {expect}\n'
f'[[ $(id -un) = $YADM_USER ]] && echo "user is set"\n'
f'exit {code}\n'
)
paths.bootstrap.chmod(0o775)
Expand All @@ -30,6 +31,7 @@ def test_bootstrap(
if exists and executable:
assert run.err == ''
assert expect in run.out
assert 'user is set' in run.out
else:
assert expect in run.err
assert run.out == ''
4 changes: 2 additions & 2 deletions test/test_unit_set_alt_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_set_alt_values(
YADM_TEST=1 source {yadm} &&
set_operating_system &&
YADM_DIR={paths.yadm} YADM_DATA={paths.data} configure_paths &&
set_alt_values
ALT_VALUES_SET=0 set_alt_values
echo "class='$YADM_CLASS'"
echo "os='$YADM_OS'"
echo "host='$YADM_HOSTNAME'"
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_distro(runner, yadm):
YADM_TEST=1 source {yadm}
function config() {{ echo "$1"; }}
function query_distro() {{ echo "testdistro"; }}
set_alt_values
ALT_VALUES_SET=0 set_alt_values
echo "distro='$YADM_DISTRO'"
"""
run = runner(command=['bash'], inp=script)
Expand Down
8 changes: 8 additions & 0 deletions yadm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ENCRYPT_INCLUDE_FILES="unparsed"

LEGACY_WARNING_ISSUED=0
INVALID_ALT=()
ALT_VALUES_SET="${YADM_TEST:-0}"

GPG_OPTS=()
OPENSSL_OPTS=()
Expand Down Expand Up @@ -584,6 +585,9 @@ function remove_stale_links() {

function set_alt_values() {

[[ $ALT_VALUES_SET = 1 ]] && return
ALT_VALUES_SET=1

export YADM_CLASS
export YADM_OS
export YADM_HOSTNAME
Expand Down Expand Up @@ -680,6 +684,8 @@ function bootstrap() {
# GIT_DIR should not be set for user's bootstrap code
unset GIT_DIR

set_alt_values

echo "Executing $YADM_BOOTSTRAP"
exec "$YADM_BOOTSTRAP"

Expand Down Expand Up @@ -1784,6 +1790,8 @@ function invoke_hook() {
export -f unix_path
export -f mixed_path

set_alt_values

"$hook_command"
hook_status=$?

Expand Down
9 changes: 7 additions & 2 deletions yadm.1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ to "false".
.B bootstrap
Execute
.I $HOME/.config/yadm/bootstrap
if it exists.
if it exists. The variables that are set when processing Jinja or ESH templates
(see the TEMPLATES section) are set in the environment (except for
.IR YADM_SOURCE ).
.TP
.BI clone " url
Clone a remote repository for tracking dotfiles.
Expand Down Expand Up @@ -891,7 +893,10 @@ will never be run. This allows one to "short-circuit" any operation using a
.I pre_
hook.

Hooks have the following environment variables available to them at runtime:
Hooks have the following variables, plus the variables set when processing
Jinja or ESH templates (see the TEMPLATES section), available to them as
environment variables at runtime (except for
.IR YADM_SOURCE ).
.TP
.B YADM_HOOK_COMMAND
The command which triggered the hook
Expand Down

0 comments on commit 2a66ccb

Please sign in to comment.