Export alt values (YADM_*) when running hooks and bootstrap

Fixes #291.
This commit is contained in:
Erik Flodin 2021-01-17 22:32:29 +01:00 committed by Erik Flodin
parent 650644cfb8
commit 87e5cb690d
No known key found for this signature in database
GPG Key ID: 420A7C865EE3F85F
4 changed files with 19 additions and 4 deletions

View File

@ -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)
@ -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 == ''

View File

@ -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'"
@ -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)

8
yadm
View File

@ -63,6 +63,7 @@ ENCRYPT_INCLUDE_FILES="unparsed"
LEGACY_WARNING_ISSUED=0
INVALID_ALT=()
ALT_VALUES_SET="${YADM_TEST:-0}"
GPG_OPTS=()
OPENSSL_OPTS=()
@ -596,6 +597,9 @@ function remove_stale_links() {
function set_alt_values() {
[[ $ALT_VALUES_SET = 1 ]] && return
ALT_VALUES_SET=1
export YADM_CLASS
export YADM_ARCH
export YADM_OS
@ -698,6 +702,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"
@ -1770,6 +1776,8 @@ function invoke_hook() {
export -f unix_path
export -f mixed_path
set_alt_values
"$hook_command"
hook_status=$?

9
yadm.1
View File

@ -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.
@ -876,7 +878,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