1
0
Fork 0
mirror of synced 2024-12-22 06:31:07 -05:00

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
This commit is contained in:
Tim Byrne 2020-11-15 14:23:49 -06:00
parent c79a67208c
commit fc66b6b71b
No known key found for this signature in database
GPG key ID: 14DB4FC2465A4B12
5 changed files with 53 additions and 26 deletions

View file

@ -140,6 +140,7 @@ def supported_switches():
'--yadm-archive', '--yadm-archive',
'--yadm-bootstrap', '--yadm-bootstrap',
'--yadm-config', '--yadm-config',
'--yadm-data',
'--yadm-dir', '--yadm-dir',
'--yadm-encrypt', '--yadm-encrypt',
'--yadm-repo', '--yadm-repo',

View file

@ -70,7 +70,7 @@ def run_test(runner, paths, args, expected_matches, expected_code=0):
script = f""" script = f"""
YADM_TEST=1 HOME="{HOME}" source {paths.pgm} YADM_TEST=1 HOME="{HOME}" source {paths.pgm}
process_global_args {argstring} process_global_args {argstring}
HOME="{HOME}" set_yadm_dir HOME="{HOME}" set_yadm_dirs
configure_paths configure_paths
declare -p | grep -E '(YADM|GIT)_' declare -p | grep -E '(YADM|GIT)_'
""" """

View file

@ -13,7 +13,7 @@ def test_get_cipher(runner, paths, condition):
script = f""" script = f"""
YADM_TEST=1 source {paths.pgm} YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}" YADM_DIR="{paths.yadm}"
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
_get_cipher test-archive _get_cipher test-archive
echo "output_archive:$output_archive" echo "output_archive:$output_archive"
@ -37,7 +37,7 @@ def test_encrypt_decrypt(runner, paths, cipher, mode):
script = f""" script = f"""
YADM_TEST=1 source {paths.pgm} YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}" YADM_DIR="{paths.yadm}"
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
function mock_openssl() {{ echo openssl $*; }} function mock_openssl() {{ echo openssl $*; }}
function mock_gpg() {{ echo gpg $*; }} function mock_gpg() {{ echo gpg $*; }}
@ -71,7 +71,7 @@ def test_get_openssl_ciphername(runner, paths, condition):
script = f""" script = f"""
YADM_TEST=1 source {paths.pgm} YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}" YADM_DIR="{paths.yadm}"
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
result=$(_get_openssl_ciphername) result=$(_get_openssl_ciphername)
echo "result:$result" echo "result:$result"
@ -95,7 +95,7 @@ def test_set_openssl_options(runner, paths, condition):
script = f""" script = f"""
YADM_TEST=1 source {paths.pgm} YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}" YADM_DIR="{paths.yadm}"
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
function _get_openssl_ciphername() {{ echo "testcipher"; }} function _get_openssl_ciphername() {{ echo "testcipher"; }}
_set_openssl_options _set_openssl_options
@ -119,7 +119,7 @@ def test_set_gpg_options(runner, paths, recipient):
script = f""" script = f"""
YADM_TEST=1 source {paths.pgm} YADM_TEST=1 source {paths.pgm}
YADM_DIR="{paths.yadm}" YADM_DIR="{paths.yadm}"
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
_set_gpg_options _set_gpg_options
echo "result:${{GPG_OPTS[@]}}" echo "result:${{GPG_OPTS[@]}}"

View file

@ -1,4 +1,4 @@
"""Unit tests: set_yadm_dir""" """Unit tests: set_yadm_dirs"""
import pytest import pytest
@ -6,8 +6,8 @@ import pytest
'condition', 'condition',
['basic', 'override', 'xdg_config_home'], ['basic', 'override', 'xdg_config_home'],
) )
def test_set_yadm_dir(runner, yadm, condition): def test_set_yadm_dirs(runner, yadm, condition):
"""Test set_yadm_dir""" """Test set_yadm_dirs"""
setup = '' setup = ''
if condition == 'override': if condition == 'override':
setup = 'YADM_DIR=/override' setup = 'YADM_DIR=/override'
@ -17,7 +17,7 @@ def test_set_yadm_dir(runner, yadm, condition):
HOME=/testhome HOME=/testhome
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{setup} {setup}
set_yadm_dir set_yadm_dirs
echo "$YADM_DIR" echo "$YADM_DIR"
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)

58
yadm
View file

@ -24,17 +24,20 @@ VERSION=2.5.0
YADM_WORK="$HOME" YADM_WORK="$HOME"
YADM_DIR= YADM_DIR=
YADM_DATA=
YADM_LEGACY_DIR="${HOME}/.yadm" YADM_LEGACY_DIR="${HOME}/.yadm"
# these are the default paths relative to YADM_DIR # these are the default paths relative to YADM_DIR
YADM_REPO="repo.git"
YADM_CONFIG="config" YADM_CONFIG="config"
YADM_ENCRYPT="encrypt" YADM_ENCRYPT="encrypt"
YADM_ARCHIVE="archive"
YADM_BOOTSTRAP="bootstrap" YADM_BOOTSTRAP="bootstrap"
YADM_HOOKS="hooks" YADM_HOOKS="hooks"
YADM_ALT="alt" YADM_ALT="alt"
# these are the default paths relative to YADM_DATA
YADM_REPO="repo.git"
YADM_ARCHIVE="archive"
HOOK_COMMAND="" HOOK_COMMAND=""
FULL_COMMAND="" FULL_COMMAND=""
@ -87,8 +90,9 @@ function main() {
done done
FULL_COMMAND="${_fc[*]}" FULL_COMMAND="${_fc[*]}"
# create the YADM_DIR if it doesn't exist yet # create the YADM_DIR & YADM_DATA if they doesn't exist yet
[ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR" [ -d "$YADM_DIR" ] || mkdir -p "$YADM_DIR"
[ -d "$YADM_DATA" ] || mkdir -p "$YADM_DATA"
# parse command line arguments # parse command line arguments
local retval=0 local retval=0
@ -1280,6 +1284,7 @@ function introspect_switches() {
--yadm-archive --yadm-archive
--yadm-bootstrap --yadm-bootstrap
--yadm-config --yadm-config
--yadm-data
--yadm-dir --yadm-dir
--yadm-encrypt --yadm-encrypt
--yadm-repo --yadm-repo
@ -1505,6 +1510,13 @@ function process_global_args() {
YADM_DIR="$2" YADM_DIR="$2"
shift 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 --yadm-repo) # override the standard YADM_REPO
if [[ ! "$2" =~ ^/ ]] ; then if [[ ! "$2" =~ ^/ ]] ; then
error_out "You must specify a fully qualified repo path" 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 # only resolve YADM_DIR if it hasn't been provided already
[ -n "$YADM_DIR" ] && return if [ -z "$YADM_DIR" ]; then
local base_yadm_dir
local base_yadm_dir base_yadm_dir="$XDG_CONFIG_HOME"
base_yadm_dir="$XDG_CONFIG_HOME" if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then
if [[ ! "$base_yadm_dir" =~ ^/ ]] ; then base_yadm_dir="${HOME}/.config"
base_yadm_dir="${HOME}/.config" fi
YADM_DIR="${base_yadm_dir}/yadm"
fi fi
YADM_DIR="${base_yadm_dir}/yadm"
issue_legacy_path_warning issue_legacy_path_warning
@ -1629,15 +1651,17 @@ LEGACY_WARNING_ISSUED=1
function configure_paths() { function configure_paths() {
# change all paths to be relative to YADM_DIR # change paths to be relative to YADM_DIR
YADM_REPO="$YADM_DIR/$YADM_REPO"
YADM_CONFIG="$YADM_DIR/$YADM_CONFIG" YADM_CONFIG="$YADM_DIR/$YADM_CONFIG"
YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT" YADM_ENCRYPT="$YADM_DIR/$YADM_ENCRYPT"
YADM_ARCHIVE="$YADM_DIR/$YADM_ARCHIVE"
YADM_BOOTSTRAP="$YADM_DIR/$YADM_BOOTSTRAP" YADM_BOOTSTRAP="$YADM_DIR/$YADM_BOOTSTRAP"
YADM_HOOKS="$YADM_DIR/$YADM_HOOKS" YADM_HOOKS="$YADM_DIR/$YADM_HOOKS"
YADM_ALT="$YADM_DIR/$YADM_ALT" 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 # independent overrides for paths
if [ -n "$YADM_OVERRIDE_REPO" ]; then if [ -n "$YADM_OVERRIDE_REPO" ]; then
YADM_REPO="$YADM_OVERRIDE_REPO" YADM_REPO="$YADM_OVERRIDE_REPO"
@ -1757,6 +1781,7 @@ function invoke_hook() {
# expose some internal data to all hooks # expose some internal data to all hooks
YADM_HOOK_COMMAND=$HOOK_COMMAND YADM_HOOK_COMMAND=$HOOK_COMMAND
YADM_HOOK_DIR=$YADM_DIR YADM_HOOK_DIR=$YADM_DIR
YADM_HOOK_DATA=$YADM_DATA
YADM_HOOK_EXIT=$exit_status YADM_HOOK_EXIT=$exit_status
YADM_HOOK_FULL_COMMAND=$FULL_COMMAND YADM_HOOK_FULL_COMMAND=$FULL_COMMAND
YADM_HOOK_REPO=$YADM_REPO YADM_HOOK_REPO=$YADM_REPO
@ -1768,6 +1793,7 @@ function invoke_hook() {
export YADM_HOOK_COMMAND export YADM_HOOK_COMMAND
export YADM_HOOK_DIR export YADM_HOOK_DIR
export YADM_HOOK_DATA
export YADM_HOOK_EXIT export YADM_HOOK_EXIT
export YADM_HOOK_FULL_COMMAND export YADM_HOOK_FULL_COMMAND
export YADM_HOOK_REPO export YADM_HOOK_REPO
@ -2182,7 +2208,7 @@ if [ "$YADM_TEST" != 1 ] ; then
process_global_args "$@" process_global_args "$@"
set_operating_system set_operating_system
set_awk set_awk
set_yadm_dir set_yadm_dirs
configure_paths configure_paths
main "${MAIN_ARGS[@]}" main "${MAIN_ARGS[@]}"
fi fi