From 48fc6b0db73469e196ad60c6affb8f5ac2ef35a0 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Tue, 6 Aug 2019 08:19:45 -0500 Subject: [PATCH] Support XDG base directory specification https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html --- completion/yadm.zsh_completion | 2 +- test/test_unit_configure_paths.py | 3 +- test/test_unit_issue_legacy_path_warning.py | 34 ++++++++ test/test_unit_set_yadm_dir.py | 35 ++++++++ yadm | 95 +++++++++++++++++++-- yadm.1 | 34 ++++---- 6 files changed, 178 insertions(+), 25 deletions(-) create mode 100644 test/test_unit_issue_legacy_path_warning.py create mode 100644 test/test_unit_set_yadm_dir.py diff --git a/completion/yadm.zsh_completion b/completion/yadm.zsh_completion index 517d7be..fa79c01 100644 --- a/completion/yadm.zsh_completion +++ b/completion/yadm.zsh_completion @@ -7,7 +7,7 @@ _yadm(){ 'config:Configure a setting' 'list:List tracked files' 'alt:Create links for alternates' - 'bootstrap:Execute $HOME/.yadm/bootstrap' + 'bootstrap:Execute $HOME/.config/yadm/bootstrap' 'encrypt:Encrypt files' 'decrypt:Decrypt files' 'perms:Fix perms for private files' diff --git a/test/test_unit_configure_paths.py b/test/test_unit_configure_paths.py index 094ff6b..332277d 100644 --- a/test/test_unit_configure_paths.py +++ b/test/test_unit_configure_paths.py @@ -8,7 +8,7 @@ CONFIG = 'config' ENCRYPT = 'encrypt' HOME = '/testhome' REPO = 'repo.git' -YDIR = '.yadm' +YDIR = '.config/yadm' @pytest.mark.parametrize( @@ -70,6 +70,7 @@ def run_test(runner, paths, args, expected_matches, expected_code=0): script = f""" YADM_TEST=1 HOME="{HOME}" source {paths.pgm} process_global_args {argstring} + HOME="{HOME}" set_yadm_dir configure_paths declare -p | grep -E '(YADM|GIT)_' """ diff --git a/test/test_unit_issue_legacy_path_warning.py b/test/test_unit_issue_legacy_path_warning.py new file mode 100644 index 0000000..e4bed27 --- /dev/null +++ b/test/test_unit_issue_legacy_path_warning.py @@ -0,0 +1,34 @@ +"""Unit tests: issue_legacy_path_warning""" +import pytest + + +@pytest.mark.parametrize( + 'legacy_path', [ + None, + 'repo.git', + 'config', + 'encrypt', + 'files.gpg', + 'bootstrap', + 'hooks', + ], + ) +def test_legacy_warning(tmpdir, runner, yadm, legacy_path): + """Use issue_legacy_path_warning""" + home = tmpdir.mkdir('home') + + if legacy_path: + home.mkdir(f'.yadm').mkdir(legacy_path) + + script = f""" + HOME={home} + YADM_TEST=1 source {yadm} + issue_legacy_path_warning + """ + run = runner(command=['bash'], inp=script) + assert run.success + assert run.err == '' + if legacy_path: + assert 'Legacy configuration paths have been detected' in run.out + else: + assert run.out.rstrip() == '' diff --git a/test/test_unit_set_yadm_dir.py b/test/test_unit_set_yadm_dir.py new file mode 100644 index 0000000..65459f8 --- /dev/null +++ b/test/test_unit_set_yadm_dir.py @@ -0,0 +1,35 @@ +"""Unit tests: set_yadm_dir""" +import pytest + + +@pytest.mark.parametrize( + 'condition', + ['basic', 'override', 'xdg_config_home', 'legacy'], + ) +def test_set_yadm_dir(runner, yadm, condition): + """Test set_yadm_dir""" + setup = '' + if condition == 'override': + setup = 'YADM_DIR=/override' + elif condition == 'xdg_config_home': + setup = 'XDG_CONFIG_HOME=/xdg' + elif condition == 'legacy': + setup = 'YADM_COMPATIBILITY=1' + script = f""" + HOME=/testhome + YADM_TEST=1 source {yadm} + {setup} + set_yadm_dir + echo "$YADM_DIR" + """ + run = runner(command=['bash'], inp=script) + assert run.success + assert run.err == '' + if condition == 'basic': + assert run.out.rstrip() == '/testhome/.config/yadm' + elif condition == 'override': + assert run.out.rstrip() == '/override' + elif condition == 'xdg_config_home': + assert run.out.rstrip() == '/xdg/yadm' + elif condition == 'legacy': + assert run.out.rstrip() == '/testhome/.yadm' diff --git a/yadm b/yadm index 09f9d87..5e07709 100755 --- a/yadm +++ b/yadm @@ -23,8 +23,10 @@ fi VERSION=1.12.0 YADM_WORK="$HOME" -YADM_DIR="$HOME/.yadm" +YADM_DIR= +YADM_LEGACY_DIR="${HOME}/.yadm" +# these are the default paths relative to YADM_DIR YADM_REPO="repo.git" YADM_CONFIG="config" YADM_ENCRYPT="encrypt" @@ -558,16 +560,16 @@ Commands: yadm config - Configure a setting yadm list [-a] - List tracked files yadm alt - Create links for alternates - yadm bootstrap - Execute \$HOME/.yadm/bootstrap + yadm bootstrap - Execute \$HOME/.config/yadm/bootstrap yadm encrypt - Encrypt files yadm decrypt [-l] - Decrypt files yadm perms - Fix perms for private files Files: - \$HOME/.yadm/config - yadm's configuration file - \$HOME/.yadm/repo.git - yadm's Git repository - \$HOME/.yadm/encrypt - List of globs used for encrypt/decrypt - \$HOME/.yadm/files.gpg - Encrypted data stored here + \$HOME/.config/yadm/config - yadm's configuration file + \$HOME/.config/yadm/repo.git - yadm's Git repository + \$HOME/.config/yadm/encrypt - List of globs used for encrypt/decrypt + \$HOME/.config/yadm/files.gpg - Encrypted data stored here Use "man yadm" for complete documentation. EOF @@ -782,6 +784,86 @@ function process_global_args() { } +function set_yadm_dir() { + + # only resolve YADM_DIR if it hasn't been provided already + [ -n "$YADM_DIR" ] && return + + # compatibility with major version 1 ignores XDG_CONFIG_HOME + if [ "$YADM_COMPATIBILITY" = "1" ]; then + YADM_DIR="$YADM_LEGACY_DIR" + return + fi + + local base_yadm_dir + base_yadm_dir="$XDG_CONFIG_HOME" + if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then + base_yadm_dir="${HOME}/.config" + fi + YADM_DIR="${base_yadm_dir}/yadm" + + issue_legacy_path_warning + +} + +function issue_legacy_path_warning() { + + # no warnings if YADM_DIR is resolved as the leacy path + [ "$YADM_DIR" = "$YADM_LEGACY_DIR" ] && return + + # no warnings if the legacy directory doesn't exist + [ ! -d "$YADM_LEGACY_DIR" ] && return + + # test for legacy paths + local legacy_found + legacy_found=() + # this is ordered by importance + for legacy_path in \ + "$YADM_LEGACY_DIR/$YADM_REPO" \ + "$YADM_LEGACY_DIR/$YADM_CONFIG" \ + "$YADM_LEGACY_DIR/$YADM_ENCRYPT" \ + "$YADM_LEGACY_DIR/$YADM_ARCHIVE" \ + "$YADM_LEGACY_DIR/$YADM_BOOTSTRAP" \ + "$YADM_LEGACY_DIR/$YADM_HOOKS" \ + ; \ + do + [ -e "$legacy_path" ] && legacy_found+=("$legacy_path") + done + + [ ${#legacy_found[@]} -eq 0 ] && return + + local path_list + for legacy_path in "${legacy_found[@]}"; do + path_list="$path_list * $legacy_path"$'\n' + done + + cat <> $HOME/.yadm/encrypt +.B echo ".ssh/*.key" >> $HOME/.config/yadm/encrypt Add a new pattern to the list of encrypted files .TP -.B yadm encrypt ; yadm add ~/.yadm/files.gpg ; yadm commit +.B yadm encrypt ; yadm add ~/.config/yadm/files.gpg ; yadm commit Commit a new set of encrypted files .SH REPORTING BUGS Report issues or create pull requests at GitHub: @@ -779,4 +779,4 @@ Tim Byrne .BR git (1), .BR gpg (1) -https://thelocehiliosan.github.io/yadm/ +https://yadm.io/