From fc66b6b71b12421e172cbb6034ab028487ba81b8 Mon Sep 17 00:00:00 2001 From: Tim Byrne Date: Sun, 15 Nov 2020 14:23:49 -0600 Subject: [PATCH] Support XDG_DATA_HOME (#208) * Create a YADM_DATA base (that is the source for repo & archive) * Add --yadm-data to override * Default YADM_DATA will be determined by XDG_DATA_HOME --- test/conftest.py | 1 + test/test_unit_configure_paths.py | 2 +- test/test_unit_encryption.py | 10 +++--- test/test_unit_set_yadm_dir.py | 8 ++--- yadm | 58 ++++++++++++++++++++++--------- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/test/conftest.py b/test/conftest.py index 8fe5349..5703a5d 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -140,6 +140,7 @@ def supported_switches(): '--yadm-archive', '--yadm-bootstrap', '--yadm-config', + '--yadm-data', '--yadm-dir', '--yadm-encrypt', '--yadm-repo', diff --git a/test/test_unit_configure_paths.py b/test/test_unit_configure_paths.py index 5fe291c..d0bf9f3 100644 --- a/test/test_unit_configure_paths.py +++ b/test/test_unit_configure_paths.py @@ -70,7 +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 + HOME="{HOME}" set_yadm_dirs configure_paths declare -p | grep -E '(YADM|GIT)_' """ diff --git a/test/test_unit_encryption.py b/test/test_unit_encryption.py index b2033a9..5d3d32c 100644 --- a/test/test_unit_encryption.py +++ b/test/test_unit_encryption.py @@ -13,7 +13,7 @@ def test_get_cipher(runner, paths, condition): script = f""" YADM_TEST=1 source {paths.pgm} YADM_DIR="{paths.yadm}" - set_yadm_dir + set_yadm_dirs configure_paths _get_cipher test-archive echo "output_archive:$output_archive" @@ -37,7 +37,7 @@ def test_encrypt_decrypt(runner, paths, cipher, mode): script = f""" YADM_TEST=1 source {paths.pgm} YADM_DIR="{paths.yadm}" - set_yadm_dir + set_yadm_dirs configure_paths function mock_openssl() {{ echo openssl $*; }} function mock_gpg() {{ echo gpg $*; }} @@ -71,7 +71,7 @@ def test_get_openssl_ciphername(runner, paths, condition): script = f""" YADM_TEST=1 source {paths.pgm} YADM_DIR="{paths.yadm}" - set_yadm_dir + set_yadm_dirs configure_paths result=$(_get_openssl_ciphername) echo "result:$result" @@ -95,7 +95,7 @@ def test_set_openssl_options(runner, paths, condition): script = f""" YADM_TEST=1 source {paths.pgm} YADM_DIR="{paths.yadm}" - set_yadm_dir + set_yadm_dirs configure_paths function _get_openssl_ciphername() {{ echo "testcipher"; }} _set_openssl_options @@ -119,7 +119,7 @@ def test_set_gpg_options(runner, paths, recipient): script = f""" YADM_TEST=1 source {paths.pgm} YADM_DIR="{paths.yadm}" - set_yadm_dir + set_yadm_dirs configure_paths _set_gpg_options echo "result:${{GPG_OPTS[@]}}" diff --git a/test/test_unit_set_yadm_dir.py b/test/test_unit_set_yadm_dir.py index c7e414c..0133023 100644 --- a/test/test_unit_set_yadm_dir.py +++ b/test/test_unit_set_yadm_dir.py @@ -1,4 +1,4 @@ -"""Unit tests: set_yadm_dir""" +"""Unit tests: set_yadm_dirs""" import pytest @@ -6,8 +6,8 @@ import pytest 'condition', ['basic', 'override', 'xdg_config_home'], ) -def test_set_yadm_dir(runner, yadm, condition): - """Test set_yadm_dir""" +def test_set_yadm_dirs(runner, yadm, condition): + """Test set_yadm_dirs""" setup = '' if condition == 'override': setup = 'YADM_DIR=/override' @@ -17,7 +17,7 @@ def test_set_yadm_dir(runner, yadm, condition): HOME=/testhome YADM_TEST=1 source {yadm} {setup} - set_yadm_dir + set_yadm_dirs echo "$YADM_DIR" """ run = runner(command=['bash'], inp=script) diff --git a/yadm b/yadm index d0ab8a3..ab986a9 100755 --- a/yadm +++ b/yadm @@ -24,17 +24,20 @@ VERSION=2.5.0 YADM_WORK="$HOME" YADM_DIR= +YADM_DATA= YADM_LEGACY_DIR="${HOME}/.yadm" # these are the default paths relative to YADM_DIR -YADM_REPO="repo.git" YADM_CONFIG="config" YADM_ENCRYPT="encrypt" -YADM_ARCHIVE="archive" YADM_BOOTSTRAP="bootstrap" YADM_HOOKS="hooks" YADM_ALT="alt" +# these are the default paths relative to YADM_DATA +YADM_REPO="repo.git" +YADM_ARCHIVE="archive" + HOOK_COMMAND="" FULL_COMMAND="" @@ -87,8 +90,9 @@ function main() { done FULL_COMMAND="${_fc[*]}" - # create the YADM_DIR if it doesn't exist yet - [ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR" + # create the YADM_DIR & YADM_DATA if they doesn't exist yet + [ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR" + [ -d "$YADM_DATA" ] || mkdir -p "$YADM_DATA" # parse command line arguments local retval=0 @@ -1280,6 +1284,7 @@ function introspect_switches() { --yadm-archive --yadm-bootstrap --yadm-config +--yadm-data --yadm-dir --yadm-encrypt --yadm-repo @@ -1505,6 +1510,13 @@ function process_global_args() { YADM_DIR="$2" shift ;; + --yadm-data) # override the standard YADM_DATA + if [[ ! "$2" =~ ^/ ]] ; then + error_out "You must specify a fully qualified yadm data directory" + fi + YADM_DATA="$2" + shift + ;; --yadm-repo) # override the standard YADM_REPO if [[ ! "$2" =~ ^/ ]] ; then error_out "You must specify a fully qualified repo path" @@ -1549,17 +1561,27 @@ function process_global_args() { } -function set_yadm_dir() { +function set_yadm_dirs() { + + # only resolve YADM_DATA if it hasn't been provided already + if [ -z "$YADM_DATA" ]; then + local base_yadm_data + base_yadm_data="$XDG_DATA_HOME" + if [[ ! "$base_yadm_data" =~ ^/ ]] ; then + base_yadm_data="${HOME}/.local/share" + fi + YADM_DATA="${base_yadm_data}/yadm" + fi # only resolve YADM_DIR if it hasn't been provided already - [ -n "$YADM_DIR" ] && return - - local base_yadm_dir - base_yadm_dir="$XDG_CONFIG_HOME" - if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then - base_yadm_dir="${HOME}/.config" + if [ -z "$YADM_DIR" ]; then + 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" fi - YADM_DIR="${base_yadm_dir}/yadm" issue_legacy_path_warning @@ -1629,15 +1651,17 @@ LEGACY_WARNING_ISSUED=1 function configure_paths() { - # change all paths to be relative to YADM_DIR - YADM_REPO="$YADM_DIR/$YADM_REPO" + # change paths to be relative to YADM_DIR YADM_CONFIG="$YADM_DIR/$YADM_CONFIG" YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT" - YADM_ARCHIVE="$YADM_DIR/$YADM_ARCHIVE" YADM_BOOTSTRAP="$YADM_DIR/$YADM_BOOTSTRAP" YADM_HOOKS="$YADM_DIR/$YADM_HOOKS" YADM_ALT="$YADM_DIR/$YADM_ALT" + # change paths to be relative to YADM_DATA + YADM_REPO="$YADM_DATA/$YADM_REPO" + YADM_ARCHIVE="$YADM_DATA/$YADM_ARCHIVE" + # independent overrides for paths if [ -n "$YADM_OVERRIDE_REPO" ]; then YADM_REPO="$YADM_OVERRIDE_REPO" @@ -1757,6 +1781,7 @@ function invoke_hook() { # expose some internal data to all hooks YADM_HOOK_COMMAND=$HOOK_COMMAND YADM_HOOK_DIR=$YADM_DIR + YADM_HOOK_DATA=$YADM_DATA YADM_HOOK_EXIT=$exit_status YADM_HOOK_FULL_COMMAND=$FULL_COMMAND YADM_HOOK_REPO=$YADM_REPO @@ -1768,6 +1793,7 @@ function invoke_hook() { export YADM_HOOK_COMMAND export YADM_HOOK_DIR + export YADM_HOOK_DATA export YADM_HOOK_EXIT export YADM_HOOK_FULL_COMMAND export YADM_HOOK_REPO @@ -2182,7 +2208,7 @@ if [ "$YADM_TEST" != 1 ] ; then process_global_args "$@" set_operating_system set_awk - set_yadm_dir + set_yadm_dirs configure_paths main "${MAIN_ARGS[@]}" fi