Dev: Refactor and big code cleaning
This commit is contained in:
parent
7ee51ed92b
commit
e2c5939b12
20
bin/idmgr
20
bin/idmgr
@ -35,6 +35,8 @@ IDM_DIR_ID=${IDM_DIR_ID:-$IDM_CONFIG_DIR/id}
|
||||
IDM_DIR_LIB=${IDM_DIR_LIB:-$IDM_DIR_ROOT/lib}
|
||||
IDM_DIR_CACHE=${IDM_DIR_CACHE:-${XDG_CACHE_HOME:-~/.cache}/idmgr}
|
||||
|
||||
mkdir -p $IDM_CONFIG_DIR $IDM_DIR_ID $IDM_DIR_CACHE
|
||||
|
||||
# Mod vars ...
|
||||
IDM_DISABLE_AUTO=
|
||||
|
||||
@ -373,8 +375,11 @@ idm_exit ()
|
||||
|
||||
|
||||
idm_exit_trap () {
|
||||
set +x
|
||||
rc=$?
|
||||
set +x
|
||||
|
||||
echo "EXIT TRAP" >/dev/null
|
||||
|
||||
if [[ $rc -ne 0 ]]; then
|
||||
lib_log ERR "The script exited with exit code: $rc"
|
||||
lib_trace || true
|
||||
@ -388,10 +393,12 @@ idm_exit_trap () {
|
||||
|
||||
## Data, tests
|
||||
|
||||
# DEPRECATRED, replaced by lib_id
|
||||
# Should be replaced by idm_validate ?
|
||||
# Is a wrapper for enduser !!!
|
||||
idm_is_enabled ()
|
||||
{
|
||||
lib_log DEPRECATED "call: idm_is enabled $@, use lib_id_is_enabled ${1-} instead"
|
||||
local id=${1}
|
||||
idm_validate is_enabled $id
|
||||
{
|
||||
@ -400,8 +407,11 @@ idm_is_enabled ()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# DEPRECATRED, replaced by lib_id
|
||||
idm_get ()
|
||||
{
|
||||
lib_log DEPRECATED "all: idm_get $@, use lib_id_has_config instead"
|
||||
local item=$1
|
||||
local value=${2-}
|
||||
|
||||
@ -432,8 +442,10 @@ idm_get ()
|
||||
esac
|
||||
}
|
||||
|
||||
# DEPRECATRED, replaced by lib_id
|
||||
idm_validate ()
|
||||
{
|
||||
lib_log DEPRECATED "call: idm_validate $@"
|
||||
local type=$1
|
||||
local value=${2-}
|
||||
|
||||
@ -572,16 +584,16 @@ idm_menu_main ()
|
||||
else
|
||||
|
||||
# Check id constraint
|
||||
if idm_validate id_config $1 ; then
|
||||
if lib_id_has_config ${1} &>/dev/null ; then
|
||||
menu=core
|
||||
action=enable
|
||||
id=$1
|
||||
elif idm_validate id_config ${2-} ; then
|
||||
elif lib_id_has_config ${2-_} &>/dev/null ; then
|
||||
menu=core
|
||||
action=$1
|
||||
id=$2
|
||||
shift 2 && opt=${@} || true
|
||||
elif idm_validate id_config ${3-} ; then
|
||||
elif lib_id_has_config ${3-_} &>/dev/null ; then
|
||||
menu=$1
|
||||
action=$2
|
||||
id=$3
|
||||
|
||||
@ -30,27 +30,27 @@ lib_require_bin () {
|
||||
# Nifty trick to set var from pipes
|
||||
lib_set_var () { read "$@" <&0; }
|
||||
|
||||
# Take an environment var name, an a list of vars to inject
|
||||
lib_vars_inject ()
|
||||
{
|
||||
local env_name=$1
|
||||
shift 1
|
||||
|
||||
# Check if not already loaded
|
||||
if [ "${last_env_name}" == "$env_name" ]; then
|
||||
return 0
|
||||
fi
|
||||
last_env_name=$env_name
|
||||
|
||||
# check if valid environment
|
||||
[ "$( type -t idm_vars_${env_name} )" = function ] || return 1
|
||||
|
||||
# Inject var list
|
||||
for var in ${@-}; do
|
||||
name=${env}_${var}
|
||||
$i=${!name}
|
||||
done
|
||||
}
|
||||
# # Take an environment var name, an a list of vars to inject
|
||||
# lib_vars_inject ()
|
||||
# {
|
||||
# local env_name=$1
|
||||
# shift 1
|
||||
#
|
||||
# # Check if not already loaded
|
||||
# if [ "${last_env_name}" == "$env_name" ]; then
|
||||
# return 0
|
||||
# fi
|
||||
# last_env_name=$env_name
|
||||
#
|
||||
# # check if valid environment
|
||||
# [ "$( type -t idm_vars_${env_name} )" = function ] || return 1
|
||||
#
|
||||
# # Inject var list
|
||||
# for var in ${@-}; do
|
||||
# name=${env}_${var}
|
||||
# $i=${!name}
|
||||
# done
|
||||
# }
|
||||
|
||||
|
||||
lib_trace ()
|
||||
@ -146,7 +146,13 @@ lib_parse_filerules ()
|
||||
|
||||
lib_log ()
|
||||
{
|
||||
|
||||
set +x
|
||||
[[ "${1-}" =~ ERR|WARN|TIP|NOTICE|INFO|DEBUG|RUN|CODE|DUMP ]] ||
|
||||
{
|
||||
lib_log ERR "Wrong message level while calling '${1-}'"
|
||||
return 1
|
||||
}
|
||||
|
||||
local level=$1
|
||||
shift || true
|
||||
@ -270,3 +276,213 @@ lib_date_diff_human ()
|
||||
# echo "export GOPATH=${XDG_OPT_HOME}/go"
|
||||
|
||||
|
||||
## Var lib
|
||||
#############################
|
||||
|
||||
lib_vars_load ()
|
||||
{
|
||||
local var_env=$1
|
||||
|
||||
# Check current var_env
|
||||
if [ "${IDM_VAR_ENV-}" == "$var_env" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Check if var_env is a function
|
||||
[ "$( type -t idm_vars_${var_env} )" == 'function' ] ||
|
||||
return 1
|
||||
|
||||
# Load the var_env
|
||||
idm_vars_${var_env} $SHELL_ID
|
||||
|
||||
# Set IDM_VAR_ENV
|
||||
IDM_VAR_ENV=$var_env
|
||||
|
||||
}
|
||||
|
||||
|
||||
## UI lib
|
||||
#############################
|
||||
|
||||
|
||||
## Id lib
|
||||
#############################
|
||||
|
||||
lib_id_is_valid_syntax ()
|
||||
{
|
||||
local id=$1
|
||||
[[ "$id" =~ ^[a-zA-Z0-9_-]+$ ]] || {
|
||||
lib_log WARN "Id $id is not a valid syntax"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
lib_id_has_config ()
|
||||
{
|
||||
local id=$1
|
||||
[[ -f "$IDM_DIR_ID/$id.env" ]] || {
|
||||
lib_log WARN "There is no config for $id"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
lib_id_is_enabled ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
[ "$id" != '_' ] || {
|
||||
lib_log WARN "There is no id enabled"
|
||||
return 1
|
||||
}
|
||||
|
||||
[ "$id" == "${SHELL_ID-}" ] || {
|
||||
lib_log WARN "The id $id is different from the enabled id ($id)"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
lib_id_get_file ()
|
||||
{
|
||||
local id=$id
|
||||
|
||||
[ -f "$IDM_DIR_ID/$id.env" ] ||
|
||||
return 1
|
||||
echo "$IDM_DIR_ID/$id.env"
|
||||
}
|
||||
|
||||
lib_id_get_config ()
|
||||
{
|
||||
local id=$id
|
||||
|
||||
cat "$( lib_id_get_file $id)" ||
|
||||
return 1
|
||||
|
||||
# [ -f "$IDM_DIR_ID/$id.env" ] ||
|
||||
# return 1
|
||||
# cat "$IDM_DIR_ID/$id.env"
|
||||
|
||||
}
|
||||
|
||||
lib_id_get_all_file ()
|
||||
{
|
||||
ls $IDM_DIR_ID/*.env || true
|
||||
}
|
||||
|
||||
lib_id_get_all_config ()
|
||||
{
|
||||
cat $IDM_DIR_ID/*.env || true
|
||||
}
|
||||
|
||||
lib_id_get_all_id ()
|
||||
{
|
||||
for id in $( find $IDM_DIR_ID -type f -name '*.env' 2>/dev/null ); do
|
||||
id=${id%%\.env}
|
||||
echo "${id##*/}"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
## Git lib
|
||||
#############################
|
||||
|
||||
lib_git_vars_load ()
|
||||
{
|
||||
local var_env=$1
|
||||
|
||||
lib_vars_load git_${var_env} ||
|
||||
return $?
|
||||
|
||||
[ ! -z "${git_dir-}" ] ||
|
||||
return 1
|
||||
[ ! -z "${git_work_tree-}" ] ||
|
||||
return 1
|
||||
}
|
||||
|
||||
lib_git_bin_is_present ()
|
||||
{
|
||||
lib_require_bin git ||
|
||||
{
|
||||
lib_log WARN "Missing git bin"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
lib_git ()
|
||||
{
|
||||
local var_env=$1
|
||||
lib_git_vars_load $var_env
|
||||
shift
|
||||
local opts=${@-}
|
||||
local rc=0
|
||||
local git_opts=""
|
||||
|
||||
# Check binary presence
|
||||
lib_git_bin_is_present ||
|
||||
return 1
|
||||
|
||||
# REALLY FUN BREAKER :(
|
||||
#lib_log RUN "git --git-dir "$git_dir" --work-tree "$git_work_tree" $opts"
|
||||
|
||||
git_opts+="--git-dir $git_dir "
|
||||
git_opts+="--work-tree $git_work_tree "
|
||||
|
||||
# Ignore CWD change if dir does not
|
||||
if [ -d "$git_work_tree" ]; then
|
||||
git_opts+="-C $git_work_tree "
|
||||
fi
|
||||
|
||||
|
||||
#set +e
|
||||
git $git_opts $opts || rc=$?
|
||||
#set -e
|
||||
|
||||
#echo "You should be able to see $rc"
|
||||
return ${rc:-0}
|
||||
}
|
||||
|
||||
lib_git_is_repo ()
|
||||
{
|
||||
local var_env=$1
|
||||
lib_git_vars_load $var_env
|
||||
|
||||
[ -d "$git_dir" ] &&
|
||||
#lib_git $var_env rev-parse > /dev/null 2>&1 ||
|
||||
lib_git $var_env rev-parse ||
|
||||
{
|
||||
lib_log WARN "Directory $git_dir is not a git repo"
|
||||
return 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
lib_git_is_repo_with_commits ()
|
||||
{
|
||||
local var_env=$1
|
||||
lib_git_vars_load $var_env
|
||||
|
||||
lib_git_is_repo $var_env ||
|
||||
return $?
|
||||
|
||||
find "$git_dir" -type f &>/dev/null || {
|
||||
lib_log "Repository have no commits"
|
||||
return $?
|
||||
}
|
||||
}
|
||||
|
||||
lib_git_is_all_commited ()
|
||||
{
|
||||
local var_env=$1
|
||||
lib_git_vars_load $var_env
|
||||
|
||||
lib_git_is_repo $var_env ||
|
||||
return $?
|
||||
|
||||
[ "$( lib_git $var_env status -s | wc -l)" -eq 0 ] ||
|
||||
{
|
||||
lib_log WARN "Some changes has not been commited"
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@ -4,15 +4,35 @@ IDM_MOD_DEPS="id"
|
||||
IDM_DISABLE_AUTO+=" git__enable git__disable git__kill "
|
||||
|
||||
|
||||
## Environments
|
||||
##############################
|
||||
|
||||
idm_git_header ()
|
||||
{
|
||||
local id=$1
|
||||
idm_vars_git_id $id
|
||||
|
||||
git_id_config=${IDM_CONFIG_DIR}/git/$id/local_gitconfig
|
||||
git_id_dir=$git_dir
|
||||
git_id_work_tree=$git_work_tree
|
||||
|
||||
mkdir -p $git_id_dir $git_id_work_tree ||
|
||||
idm_exit 1 ERR "Could not create dir: $git_id_dir $git_id_work_tree"
|
||||
}
|
||||
|
||||
|
||||
## User functions
|
||||
idm_vars_git_id () {
|
||||
local id=$1
|
||||
git_dir=$IDM_DIR_CACHE/git/$id/local.git
|
||||
git_work_tree=$HOME
|
||||
}
|
||||
|
||||
|
||||
## Front functions
|
||||
##############################
|
||||
|
||||
idm_git__help ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
echo "Git"
|
||||
printf " %-20s: %s\n" "git init" "Start a local repo"
|
||||
printf " %-20s: %s\n" "git scan" "Search and add interesting files"
|
||||
@ -24,19 +44,32 @@ idm_git__help ()
|
||||
printf " %-20s: %s\n" "git --help" "Git wrapper"
|
||||
printf " %-20s: %s\n" "git [cmd]" "Git wrapper"
|
||||
|
||||
if idm_validate id_config $id; then
|
||||
idm_git_init $id
|
||||
if lib_git_is_repo $git_local_dir $git_local_work_tree ; then
|
||||
echo
|
||||
idm_git_init $id
|
||||
echo " Config:"
|
||||
$GIT_LOCAL config -l | sort \
|
||||
| grep -E '(core|remote|include|remote|user|status)\.' #| sed 's/^/ /'
|
||||
fi
|
||||
fi
|
||||
#if lib_id_is_enabled $id; then
|
||||
# idm_git_header $id
|
||||
# echo
|
||||
# echo " Config:"
|
||||
# lib_git id config -l | sort \
|
||||
# | grep -E '(core|remote|include|remote|user|status)\.' | uniq | sed 's/^/ /'
|
||||
# # TOFIX: We have duplicate config entry here ... the fuckin fuck :(
|
||||
#fi
|
||||
|
||||
}
|
||||
|
||||
idm_git ()
|
||||
{
|
||||
local action=$1
|
||||
local id=$2
|
||||
shift 2
|
||||
opts=${*-}
|
||||
|
||||
# Loading
|
||||
lib_id_is_enabled $id ||
|
||||
return 1
|
||||
idm_git_header $id
|
||||
|
||||
# Forward to git
|
||||
lib_git id $action $opts
|
||||
}
|
||||
|
||||
idm_git__init ()
|
||||
{
|
||||
@ -44,56 +77,70 @@ idm_git__init ()
|
||||
shift 1
|
||||
opts=${*-}
|
||||
|
||||
# Sanity check
|
||||
idm_validate id_config $id
|
||||
idm_git_init $id
|
||||
# Loading
|
||||
lib_id_is_enabled $id ||
|
||||
return 1
|
||||
idm_git_header $id
|
||||
|
||||
# Check local repo
|
||||
if lib_git_is_repo $git_local_dir $git_local_work_tree ; then
|
||||
lib_log WARN "Do you want to override the esixting repo?"
|
||||
idm_cli_timeout 1 || idm_exit 1 "User cancelled"
|
||||
# Check if repo exists
|
||||
if lib_git_is_repo_with_commits id &>/dev/null ; then
|
||||
lib_log WARN "Do you want to override the existing repo?"
|
||||
idm_cli_timeout 1 ||
|
||||
idm_exit 1 "User cancelled"
|
||||
elif lib_git_is_repo id &>/dev/null; then
|
||||
lib_log INFO "Git repo is already there"
|
||||
return 0
|
||||
fi
|
||||
|
||||
$GIT_LOCAL init $opts
|
||||
lib_log NOTICE "Repository has been created into '$git_local_dir'"
|
||||
# Initialise repo
|
||||
lib_git id init $opts ||
|
||||
idm_exit ERR "Could not create reporitory"
|
||||
|
||||
# Generate
|
||||
$GIT_LOCAL config --add include.path "$git_local_config"
|
||||
idm_git__gen_git_config > $git_local_config
|
||||
# Generate config
|
||||
lib_git id config --add include.path "$git_id_config"
|
||||
idm_git__gen_git_config > $git_id_config
|
||||
|
||||
# Notify user
|
||||
lib_log NOTICE "Repository has been created into '$git_dir'"
|
||||
}
|
||||
|
||||
idm_git__scan ()
|
||||
{
|
||||
local id=$1
|
||||
idm_validate id_config $id
|
||||
idm_git_init $id
|
||||
shift 1
|
||||
opts=${*-}
|
||||
|
||||
# Ensure we have a valid repository
|
||||
if ! lib_git_is_repo $git_local_dir $git_local_work_tree ; then
|
||||
lib_log WARN "Do you want to create a local repository of your secrets?"
|
||||
idm_cli_timeout 1 || idm_exit 1 "User cancelled"
|
||||
$GIT_LOCAL init
|
||||
fi
|
||||
# Loading
|
||||
lib_id_is_enabled $id ||
|
||||
return 1
|
||||
idm_git_header $id
|
||||
|
||||
# Check if it is a valid repo
|
||||
lib_git_is_repo id ||
|
||||
idm_git__init $id
|
||||
|
||||
# Add all files
|
||||
$GIT_LOCAL add -f $( xargs <<<"$( idm_git__get_files_of_interest $id )" )
|
||||
lib_git id add -f $( xargs <<<"$( idm_git_get_files_of_interest $id )" )
|
||||
|
||||
# Check uncommited changes
|
||||
if ! lib_git_is_all_commited $git_local_dir $git_local_work_tree ; then
|
||||
if ! lib_git_is_all_commited id &>/dev/null ; then
|
||||
|
||||
lib_log INFO "There are the files we could add:"
|
||||
$GIT_LOCAL status -s
|
||||
lib_git id status -s
|
||||
|
||||
lib_log PROMPT "Do you want to add these files to your repo?"
|
||||
if idm_cli_timeout 1; then
|
||||
tty=$(tty)
|
||||
#$GIT_LOCAL commit -e
|
||||
echo "Add: Import $(hostname) data" | $GIT_LOCAL commit --file=-
|
||||
|
||||
lib_git id commit --file=- <<< "Add: Import $(hostname) data" ||
|
||||
idm_exit 1 "Could not commit files"
|
||||
|
||||
lib_log NOTICE "New files has been added to local repo"
|
||||
|
||||
else
|
||||
lib_log TIP "Commit your files with 'i git commit '"
|
||||
lib_log NOTICE "Scan returned some new files, please commit them"
|
||||
fi
|
||||
else
|
||||
lib_log INFO "Nothing to add ..."
|
||||
lib_log NOTICE "Scan didn't find other files"
|
||||
fi
|
||||
|
||||
}
|
||||
@ -103,32 +150,23 @@ idm_git__ls ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Loading
|
||||
lib_id_is_enabled $id ||
|
||||
return 1
|
||||
idm_git_header $id
|
||||
|
||||
idm_git_init $id
|
||||
# Check if it is a valid repo
|
||||
lib_git_is_repo id ||
|
||||
return 1
|
||||
|
||||
$GIT_LOCAL ls-files | sort
|
||||
#$GIT_LOCAL ls-files | sort | sed 's@/[^\/]*@@'
|
||||
|
||||
return
|
||||
|
||||
if idm_validate id_config $id; then
|
||||
idm_git_init $id
|
||||
if lib_git_is_repo $git_local_dir $git_local_work_tree ; then
|
||||
$GIT_LOCAL ls-files | sort | sed 's/^/ ~\//'
|
||||
else
|
||||
echo "Repository is not created"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
|
||||
#tree $
|
||||
# Show files
|
||||
lib_git id ls-files | sort | sed 's/^/ /'
|
||||
}
|
||||
|
||||
idm_git__enable ()
|
||||
{
|
||||
local id=$1
|
||||
idm_git_init $id
|
||||
idm_git_header $id
|
||||
|
||||
cat <<EOF -
|
||||
export GIT_DIR="$git_local_dir"
|
||||
@ -144,50 +182,12 @@ idm_git__disable ()
|
||||
|
||||
idm_git__kill () { idm_git__disable ${@-}; }
|
||||
|
||||
idm_git ()
|
||||
{
|
||||
local action=$1
|
||||
local id=$2
|
||||
shift 2
|
||||
local opts=${*-}
|
||||
idm_git_init $id
|
||||
|
||||
$GIT_LOCAL $action $opts
|
||||
}
|
||||
|
||||
|
||||
## External deps
|
||||
## Internal lib
|
||||
##############################
|
||||
|
||||
_git_local2 ()
|
||||
{
|
||||
lib_git_bin $git_local_dir $git_local_work_tree ${*-}
|
||||
}
|
||||
|
||||
idm_vars_git_local () {
|
||||
git_local_work_tree=$HOME
|
||||
git_local_dir=$IDM_DIR_CACHE/git/$id/local.git
|
||||
git_local_config=${IDM_CONFIG_DIR}/git/$id/local_gitconfig
|
||||
|
||||
git_local="lib_git_bin $git_local_dir $git_local_work_tree"
|
||||
GIT_LOCAL=$git_local
|
||||
}
|
||||
|
||||
idm_git_init ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Sanity check
|
||||
idm_validate id_config $id
|
||||
|
||||
# Load local repo vars
|
||||
idm_vars_git_local
|
||||
|
||||
}
|
||||
|
||||
####
|
||||
|
||||
idm_git__get_files_of_interest ()
|
||||
idm_git_get_files_of_interest ()
|
||||
{
|
||||
local id=${1}
|
||||
|
||||
@ -203,7 +203,6 @@ idm_git__get_files_of_interest ()
|
||||
|
||||
}
|
||||
|
||||
|
||||
idm_git__gen_git_config ()
|
||||
{
|
||||
(
|
||||
@ -215,103 +214,105 @@ EOF
|
||||
) | sed "s@$HOME/@~/@g"
|
||||
}
|
||||
|
||||
|
||||
# Debug libs
|
||||
########################
|
||||
# # Debug libs
|
||||
# ########################
|
||||
#
|
||||
# # Debug and shortcuts
|
||||
# idm_git_f () {
|
||||
# local id=$1
|
||||
# local cmd=$2
|
||||
# shift 2
|
||||
# local opts=${*-}
|
||||
# local rc=0
|
||||
#
|
||||
# trap '' INT TERM EXIT
|
||||
# lib_id_is_enabled $id
|
||||
# idm_git_init $id
|
||||
#
|
||||
# set -e
|
||||
# idm_git_${cmd#idm_git_} $opts
|
||||
# rc=$?
|
||||
# set -e
|
||||
#
|
||||
# if [ "$rc" -eq 0 ]; then
|
||||
# idm_exit 0 "Returns $rc"
|
||||
# else
|
||||
# idm_exit $rc WARN "Called: 'idm_git_${cmd#idm_git_} ${opts:+$opts }'"
|
||||
# fi
|
||||
#
|
||||
# }
|
||||
#
|
||||
# idm_git__d ()
|
||||
# {
|
||||
# $IDM_BIN git l
|
||||
# $IDM_BIN git status -s
|
||||
# $IDM_BIN git remote -v
|
||||
# {
|
||||
# $IDM_BIN git config -l \
|
||||
# | sort \
|
||||
# | grep -E '(core|remote|include|remote|user|status)\.'
|
||||
# }
|
||||
# }
|
||||
|
||||
|
||||
# Debug and shortcuts
|
||||
idm_git_f () {
|
||||
local id=$1
|
||||
local cmd=$2
|
||||
shift 2
|
||||
local opts=${*-}
|
||||
local rc=0
|
||||
|
||||
trap '' INT TERM EXIT
|
||||
idm_validate id_config $id
|
||||
idm_git_init $id
|
||||
|
||||
set -e
|
||||
idm_git_${cmd#idm_git_} $opts
|
||||
rc=$?
|
||||
set -e
|
||||
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
idm_exit 0 "Returns $rc"
|
||||
else
|
||||
idm_exit $rc WARN "Called: 'idm_git_${cmd#idm_git_} ${opts:+$opts }'"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
idm_git__d ()
|
||||
{
|
||||
$IDM_BIN git l
|
||||
$IDM_BIN git status -s
|
||||
$IDM_BIN git remote -v
|
||||
{
|
||||
$IDM_BIN git config -l \
|
||||
| sort \
|
||||
| grep -E '(core|remote|include|remote|user|status)\.'
|
||||
}
|
||||
}
|
||||
|
||||
## Future lib
|
||||
##############################
|
||||
|
||||
|
||||
lib_git_bin ()
|
||||
{
|
||||
local git_dir=$1
|
||||
local git_work_tree=$2
|
||||
shift 2
|
||||
local opts=${@-}
|
||||
local rc=0
|
||||
|
||||
# Check binary presence
|
||||
lib_require_bin git || \
|
||||
idm_exit 1 "Please install git first."
|
||||
|
||||
# REALLY FUN BREAKER :(
|
||||
#lib_log RUN "git --git-dir "$git_dir" --work-tree "$git_work_tree" $opts"
|
||||
|
||||
set +e
|
||||
git \
|
||||
--git-dir "$git_dir" \
|
||||
--work-tree "$git_work_tree" \
|
||||
-C "$git_work_tree" \
|
||||
$opts || rc=$?
|
||||
set -e
|
||||
|
||||
#echo "You should be able to see $rc"
|
||||
return ${rc:-0}
|
||||
}
|
||||
|
||||
|
||||
lib_git_is_repo ()
|
||||
{
|
||||
local git_dir=$1
|
||||
local git_work_tree=$2
|
||||
|
||||
[ -d "$git_dir" ] && lib_git_bin $git_dir $git_work_tree rev-parse > /dev/null 2>&1 ; return $?
|
||||
}
|
||||
|
||||
lib_git_has_commits ()
|
||||
{
|
||||
local git_dir=$1
|
||||
local git_work_tree=$2
|
||||
|
||||
lib_git_is_repo $git_dir $git_work_tree || return $?
|
||||
|
||||
find "$git_dir" -type f &>/dev/null || return 1
|
||||
}
|
||||
|
||||
lib_git_is_all_commited ()
|
||||
{
|
||||
local git_dir=$1
|
||||
local git_work_tree=$2
|
||||
|
||||
[ "$( lib_git_bin $git_dir $git_work_tree status -s | wc -l)" -eq 0 ]
|
||||
}
|
||||
|
||||
# ## Future lib
|
||||
# ##############################
|
||||
#
|
||||
#
|
||||
# lib_git_bin ()
|
||||
# {
|
||||
# local git_dir=$1
|
||||
# local git_work_tree=$2
|
||||
# shift 2
|
||||
# local opts=${@-}
|
||||
# local rc=0
|
||||
#
|
||||
# # Check binary presence
|
||||
# lib_require_bin git || \
|
||||
# idm_exit 1 "Please install git first."
|
||||
#
|
||||
# # REALLY FUN BREAKER :(
|
||||
# #lib_log RUN "git --git-dir "$git_dir" --work-tree "$git_work_tree" $opts"
|
||||
#
|
||||
# set +e
|
||||
# git \
|
||||
# --git-dir "$git_dir" \
|
||||
# --work-tree "$git_work_tree" \
|
||||
# -C "$git_work_tree" \
|
||||
# $opts || rc=$?
|
||||
# set -e
|
||||
#
|
||||
# #echo "You should be able to see $rc"
|
||||
# return ${rc:-0}
|
||||
# }
|
||||
#
|
||||
#
|
||||
# lib_git_is_repo ()
|
||||
# {
|
||||
# local git_dir=$1
|
||||
# local git_work_tree=$2
|
||||
#
|
||||
# [ -d "$git_dir" ] && lib_git_bin $git_dir $git_work_tree rev-parse > /dev/null 2>&1 ; return $?
|
||||
# }
|
||||
#
|
||||
# lib_git_has_commits ()
|
||||
# {
|
||||
# local git_dir=$1
|
||||
# local git_work_tree=$2
|
||||
#
|
||||
# lib_git_is_repo $git_dir $git_work_tree || return $?
|
||||
#
|
||||
# find "$git_dir" -type f &>/dev/null || return 1
|
||||
# }
|
||||
#
|
||||
# lib_git_is_all_commited ()
|
||||
# {
|
||||
# local git_dir=$1
|
||||
# local git_work_tree=$2
|
||||
#
|
||||
# [ "$( lib_git_bin $git_dir $git_work_tree status -s | wc -l)" -eq 0 ]
|
||||
# }
|
||||
#
|
||||
|
||||
@ -9,30 +9,42 @@ IDM_DISABLE_AUTO+=" tomb__enable tomb__disable tomb__kill "
|
||||
|
||||
|
||||
|
||||
## Tomb functions
|
||||
## Environments
|
||||
##########################################
|
||||
|
||||
# Install yadm
|
||||
# git clone https://github.com/TheLocehiliosan/yadm.git ~/.usr/opt/yadm
|
||||
#
|
||||
# This allow to secure your things ....
|
||||
idm_tomb_header ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Check if id is valid
|
||||
lib_id_has_config $id
|
||||
|
||||
# Load local repo vars
|
||||
idm_git_header $id
|
||||
git_id_enc=$IDM_DIR_CACHE/git/$id/local.git.tar.gz.asc
|
||||
|
||||
# Load tomb vars
|
||||
idm_vars_git_tomb $id
|
||||
git_tomb_config=${IDM_CONFIG_DIR}/git/$id/local_gitconfig
|
||||
git_tomb_dir=$git_dir
|
||||
git_tomb_work_tree=$git_work_tree
|
||||
git_tomb_enc=$IDM_CONFIG_DIR/enc/$id.tomb
|
||||
git_id_tomb_repo_name=tomb
|
||||
}
|
||||
|
||||
idm_vars_git_tomb () {
|
||||
git_tomb_work_tree=$HOME
|
||||
git_tomb_dir=$IDM_DIR_CACHE/git/$id/tomb.git
|
||||
git_tomb_config=${IDM_CONFIG_DIR}/git/$id/tomb_gitconfig
|
||||
git_tomb_enc=$IDM_CONFIG_DIR/enc/$id.tomb
|
||||
local id=$1
|
||||
git_dir=$IDM_DIR_CACHE/git/$id/tomb.git
|
||||
git_work_tree=$git_dir/.git
|
||||
}
|
||||
|
||||
|
||||
## Front functions
|
||||
##############################
|
||||
|
||||
|
||||
idm_tomb__help ()
|
||||
{
|
||||
local id=$1
|
||||
idm_vars_git_tomb
|
||||
|
||||
echo "tomb"
|
||||
echo " workflow:"
|
||||
@ -43,116 +55,278 @@ idm_tomb__help ()
|
||||
printf " %-20s: %s\n" "tomb encrypt" "Save the current configuration into the tomb"
|
||||
printf " %-20s: %s\n" "tomb push <remote>|all" "Save the current configuration into the tomb"
|
||||
printf " %-20s: %s\n" "tomb leave" "Remove all traces of your passage"
|
||||
|
||||
if lib_id_is_enabled $id; then
|
||||
idm_tomb_header $id
|
||||
echo " config:"
|
||||
printf " %-20s: %s\n" "git_tomb_enc" "$git_tomb_enc"
|
||||
printf " %-20s: %s\n" "git_tomb_dir" "$git_tomb_dir"
|
||||
printf " %-20s: %s\n" "git_tomb_config" "$git_tomb_config"
|
||||
return 0
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
idm_tomb__ls ()
|
||||
{
|
||||
local id=$1
|
||||
idm_vars_git_tomb
|
||||
local g_st=
|
||||
local t_st=
|
||||
local d_c=
|
||||
local d_m=
|
||||
local date_today=$(date '+%s')
|
||||
|
||||
|
||||
echo " Tombs:"
|
||||
find $IDM_CONFIG_DIR/enc/ -type f -name '*.tomb' -printf "%f (%Tc)\n" |
|
||||
sed -e 's/^/ /'
|
||||
find $IDM_CONFIG_DIR/enc/ -type f -name "*.tomb" | sed "s@$HOME@ ~@"
|
||||
|
||||
idm_tomb_require_enabled $id || return 0
|
||||
|
||||
# Calculate data
|
||||
if [ -d "$git_tomb_dir" ]; then
|
||||
g_st=open
|
||||
g_m=$( lib_date_diff_human $(find $git_tomb_dir -maxdepth 0 -printf "%Ts") )
|
||||
g_m=" $d_m"
|
||||
else
|
||||
g_st=closed
|
||||
g_m=
|
||||
fi
|
||||
if lib_id_is_enabled $id; then
|
||||
local tomb_status=
|
||||
local tomb_date=
|
||||
local git_status=
|
||||
local git_date=
|
||||
|
||||
# Load local vars
|
||||
idm_tomb_header $id
|
||||
|
||||
# Get status of tomb file
|
||||
if [ -f "$git_tomb_enc" ]; then
|
||||
t_st=present
|
||||
t_m=$( lib_date_diff_human $(find $git_tomb_enc -printf "%Ts") )
|
||||
t_m=", $t_m old"
|
||||
|
||||
tomb_status=open
|
||||
tomb_date=$( lib_date_diff_human $(find $git_tomb_enc -printf "%Ts") )
|
||||
tomb_date=", $tomb_date old"
|
||||
else
|
||||
t_st=absent
|
||||
t_m=
|
||||
tomb_status=closed
|
||||
fi
|
||||
|
||||
echo " Info:"
|
||||
printf " %-20s: %s\n" "encrypted tomb" "$t_st${t_m}"
|
||||
# Get status of git repo
|
||||
if [ -d "$git_tomb_dir" ]; then
|
||||
git_status=present
|
||||
#git_date=$( lib_date_diff_human $(find $git_tomb_dir -maxdepth 0 -printf "%Ts") )
|
||||
#git_date=" $git_date"
|
||||
else
|
||||
git_status=absent
|
||||
fi
|
||||
|
||||
# Display
|
||||
echo " Status:"
|
||||
printf " %-20s: %s\n" "encrypted tomb" "$tomb_status${tomb_date}"
|
||||
printf " %-20s: %s\n" "encrypted file" "$git_tomb_enc"
|
||||
printf " %-20s: %s\n" "tomb git status" "$g_st$g_m"
|
||||
printf " %-20s: %s\n" "tomb git status" "$git_status${git_date}"
|
||||
printf " %-20s: %s\n" "tomb git dir" "$git_tomb_dir"
|
||||
|
||||
if lib_git_is_repo $git_tomb_dir $git_tomb_work_tree; then
|
||||
# Show git remotes
|
||||
if lib_git_is_repo id; then
|
||||
echo " Git remotes:"
|
||||
_git_tomb remote -v | sed 's/^/ /'
|
||||
lib_git id remote -v | sed 's/^/ /'
|
||||
echo " Last commits:"
|
||||
lib_git id l --color=always | sed 's/^/ /'
|
||||
echo
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
}
|
||||
|
||||
# This leave everything open at this stage !!!
|
||||
|
||||
idm_tomb__rm ()
|
||||
{
|
||||
local id=$1
|
||||
local report=
|
||||
|
||||
# Load tomb variables
|
||||
idm_tomb_header $id
|
||||
|
||||
# Delete local remote branch
|
||||
if lib_git id remote show $git_id_tomb_repo_name &>/dev/null ; then
|
||||
lib_git id remote rm $git_id_tomb_repo_name ||
|
||||
{
|
||||
lib_log INFO "Could not remote tomb remote"
|
||||
return 1
|
||||
}
|
||||
else
|
||||
lib_log INFO "Tomb remote is already absent"
|
||||
fi
|
||||
|
||||
# Delete git repo
|
||||
if [ -d "$git_tomb_dir" ] ; then
|
||||
rm -rf "$git_tomb_dir"
|
||||
else
|
||||
lib_log INFO "Tomb repository is already absent"
|
||||
fi
|
||||
|
||||
# Notify
|
||||
lib_log NOTICE "Tomb repository has been deleted"
|
||||
}
|
||||
|
||||
idm_tomb__init ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Check if local repo is not empty
|
||||
lib_git_is_repo_with_commits id ||
|
||||
{
|
||||
lib_log INFO "Local repository must be present first"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Load tomb variables
|
||||
idm_tomb_header $id
|
||||
|
||||
# Check if local repo already exists # TOFIX !!! use lib_git_is_repo instead
|
||||
if [ -d "$git_tomb_dir" ] ; then
|
||||
lib_log INFO "Tomb repository alreay exists"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Create tomb: from local files
|
||||
if [ -f "$git_tomb_enc" ]; then
|
||||
|
||||
lib_log WARN "An encrypted tomb has been found. Do you want to decrypt it? ($git_tomb_enc)"
|
||||
if idm_cli_timeout 1 || false ; then
|
||||
lib_log "Extracting existing tomb ..."
|
||||
idm_tomb__decrypt $id ||
|
||||
idm_exit 1 ERR "Failed to create tomb repo"
|
||||
else
|
||||
lib_log INFO "Skipping existing tomb, creating a fresh one ..."
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# Create tomb: from other file #TODO
|
||||
# Create tomb: from other host #TODO
|
||||
|
||||
# Create tomb: from scratch
|
||||
if [ -f "$git_tomb_enc" ]; then
|
||||
mkdir -p "$git_tomb_dir"
|
||||
_git_tomb clone --bare $git_id_dir $git_tomb_dir || \
|
||||
idm_exit 1 ERR "Could not create tomb repo"
|
||||
lib_log NOTICE "Tomb repository has been created"
|
||||
fi
|
||||
|
||||
# Add tomb remote to local repo
|
||||
lib_git id remote | grep -q $git_id_tomb_repo_name ||
|
||||
lib_git id remote add $git_id_tomb_repo_name $git_tomb_dir ||
|
||||
idm_exit 1 ERR "Failed to add tomb remote to local git"
|
||||
|
||||
# Syncrhonise with tomb
|
||||
#if lib_git_is_repo_with_commits id ; then
|
||||
# idm_tomb__sync $id
|
||||
#fi
|
||||
|
||||
}
|
||||
|
||||
|
||||
idm_tomb__sync ()
|
||||
{
|
||||
local id=$1
|
||||
local repo_name=${2:-tomb}
|
||||
|
||||
|
||||
# Sanity check: id and local repo
|
||||
idm_tomb_require_enabled $id
|
||||
idm_tomb_require_valid_local_repo
|
||||
idm_tomb_header $id
|
||||
lib_git_is_repo_with_commits id
|
||||
|
||||
# Tomb repo check
|
||||
#set -x
|
||||
|
||||
if ! lib_git_is_repo $git_tomb_dir $git_tomb_work_tree; then
|
||||
if [ -f "$git_tomb_enc" ]; then
|
||||
|
||||
lib_log WARN "An encrypted tomb has been found. Do you want to decrypt it?"
|
||||
idm_cli_timeout 1 || idm_exit 1 ERR "Refuse to create a tomb duplicate"
|
||||
idm_tomb__decrypt $id || idm_exit 1 ERR "Failed to create tomb repo"
|
||||
|
||||
elif [ ! -d "$git_tomb_dir" ]; then
|
||||
idm_tomb__init $id || idm_exit 1 ERR "Tomb cannot be used without git"
|
||||
lib_log NOTICE "A tomb has been created"
|
||||
return 0
|
||||
else
|
||||
idm_exit 1 ERR "Unknow error"
|
||||
fi
|
||||
fi
|
||||
lib_git_is_repo tomb ||
|
||||
idm_tomb__init $id ||
|
||||
{
|
||||
lib_log ERR "Failed to create tomb repo"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Work on local
|
||||
_git_tomb remote show $repo_name &>/dev/null ||
|
||||
_git_tomb remote add $repo_name $git_tomb_dir ||
|
||||
idm_exit 1 ERR "Failed to add tomb remote to local git"
|
||||
|
||||
{
|
||||
_git_tomb fetch --all --tags &&
|
||||
_git_tomb push -u $repo_name --all &&
|
||||
_git_tomb push -u $repo_name --tags
|
||||
lib_git id fetch --all --tags &&
|
||||
lib_git id push -u $git_id_tomb_repo_name --all &&
|
||||
lib_git id push -u $git_id_tomb_repo_name --tags
|
||||
} >/dev/null || idm_exit 1 ERR "Something where wrong while syncinc"
|
||||
|
||||
# Notify user
|
||||
lib_log NOTICE "Tomb and local repository are now synced"
|
||||
|
||||
# Restore ctx
|
||||
}
|
||||
|
||||
|
||||
#### THIS PART BELOW NEED REFACTOOOORRRR
|
||||
|
||||
idm_tomb__encrypt ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
#set -x
|
||||
idm_tomb_header $id
|
||||
lib_git_is_all_commited id
|
||||
|
||||
# We check tomb repo here
|
||||
lib_git_is_repo tomb ||
|
||||
idm_tomb__init $id ||
|
||||
{
|
||||
lib_log ERR "Failed to create tomb repo"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Full sync both repo
|
||||
idm_tomb__sync $id ||
|
||||
idm_exit 1 ERR "Failed to push commits to tomb repo"
|
||||
|
||||
# Encrypt tomb data
|
||||
lib_gpg_encrypt_dir $git_tomb_dir $git_tomb_enc _PASS || \
|
||||
idm_exit 1 ERR "Failed to create tomb"
|
||||
|
||||
## Encrypt local data
|
||||
lib_gpg_encrypt_dir $git_id_dir $git_id_enc $GIT_AUTHOR_EMAIL || \
|
||||
idm_exit 1 ERR "Could not create local repo data"
|
||||
|
||||
# Clean tomb
|
||||
rm -rf $git_tomb_dir
|
||||
|
||||
lib_log NOTICE "Tomb has been closed into: $git_tomb_enc"
|
||||
}
|
||||
|
||||
idm_tomb__decrypt ()
|
||||
{
|
||||
local id=$1
|
||||
shift || true
|
||||
local opt=${@-}
|
||||
|
||||
# Sanity check
|
||||
idm_tomb_require_enabled $id
|
||||
|
||||
# Check if tomb repo is absent
|
||||
if lib_git_is_repo $git_tomb_dir $git_id_work_tree ; then
|
||||
lib_log WARN "A local repo is already present, we will overwrite it. Do you want to continue?"
|
||||
idm_cli_timeout 0 || idm_exit 1 ERR "Refuse to override existing repo"
|
||||
|
||||
# Let's not delete existing repo, just for fun and wee how git react :p
|
||||
fi
|
||||
|
||||
# Extract tomb
|
||||
lib_gpg_decrypt_dir $git_tomb_enc $git_tomb_dir || \
|
||||
idm_exit 1 ERR "Could not extract tomb"
|
||||
|
||||
# Extract local repo
|
||||
if lib_git_is_repo id ; then
|
||||
# Local repo always win !, so we just sync
|
||||
lib_log INFO "Local repo already present, we just start sync"
|
||||
idm_tomb__sync $id
|
||||
elif [ -f "$git_id_enc" ]; then
|
||||
lib_gpg_decrypt_dir $git_id_enc $git_id_dir || \
|
||||
idm_exit 1 ERR "Could not extract local repo"
|
||||
else
|
||||
idm_git__init $id &&
|
||||
idm_tomb__sync $id ||
|
||||
idm_exit 1 "Something wrong happened while working on local repo"
|
||||
|
||||
fi
|
||||
|
||||
# Sync :D
|
||||
#idm_tomb__sync $id
|
||||
|
||||
lib_log NOTICE "Your tomb has been decrypted"
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
# We manage distribution of our repo
|
||||
# but maybe it should be the lib_git_local roles ...
|
||||
# but maybe it should be the liblib_git id roles ...
|
||||
idm_tomb__push ()
|
||||
{
|
||||
local id=$1
|
||||
local arg=${2-}
|
||||
idm_tomb_require_enabled $id
|
||||
idm_tomb_header $id
|
||||
|
||||
# Manage argument
|
||||
if grep -sq "$arg" $IDM_CONFIG_DIR/git/$id/known_hosts ; then
|
||||
@ -169,9 +343,9 @@ idm_tomb__push ()
|
||||
for repo_name in $remotes; do
|
||||
lib_log INFO "Synchronising remote $repo_name ..."
|
||||
|
||||
_git_tomb fetch --all --tags &&
|
||||
_git_tomb push -u $repo_name --all &&
|
||||
_git_tomb push -u $repo_name --tags ||
|
||||
lib_git id fetch --all --tags &&
|
||||
lib_git id push -u $repo_name --all &&
|
||||
lib_git id push -u $repo_name --tags ||
|
||||
lib_log WARN "Could not sync with $reponame"
|
||||
done
|
||||
|
||||
@ -206,6 +380,58 @@ idm_tomb__push ()
|
||||
|
||||
}
|
||||
|
||||
|
||||
# COMPLETELY DEPRECATED, see with __rm
|
||||
idm_tomb__shred ()
|
||||
{
|
||||
local id=$1
|
||||
local arg=${2-}
|
||||
local files=
|
||||
|
||||
idm_tomb_require_enabled $id
|
||||
|
||||
case $arg in
|
||||
local) files="$git_id_dir" ;;
|
||||
tomb) files="$git_tomb_dir" ;;
|
||||
all) files="$git_tomb_dir $git_id_dir" ;;
|
||||
full) files="$git_tomb_dir $git_id_dir $git_id_enc" ;;
|
||||
disapear) files="$git_tomb_dir $git_id_dir $git_id_enc $( idm_git__get_files_of_interest $id | sed 's@^@~/@' | xargs )" ;;
|
||||
*)
|
||||
idm_exit 1 "You need to say: local|tomb|all|full"
|
||||
;;
|
||||
esac
|
||||
|
||||
lib_log WARN "All these files will be IRREVOCABLY DELETED."
|
||||
xargs -n 1 <<< "$files" | lib_log DUMP -
|
||||
|
||||
lib_log WARN "Do you want to continue ?"
|
||||
idm_cli_timeout 1 || idm_exit 1 ERR "No data deleted"
|
||||
|
||||
lib_log WARN "Run it yourself: rm -rf $files"
|
||||
|
||||
}
|
||||
|
||||
idm_tomb__enable () { return 0; }
|
||||
idm_tomb__disable () { return 0; }
|
||||
idm_tomb__kill () { return 0; }
|
||||
|
||||
|
||||
## Internal functions
|
||||
##############################
|
||||
|
||||
|
||||
_git_tomb ()
|
||||
{
|
||||
lib_git tomb $@ || return
|
||||
rc=$?
|
||||
#echo "RETURN2: $rc"
|
||||
return $rc
|
||||
}
|
||||
|
||||
|
||||
## Module functions
|
||||
##############################
|
||||
|
||||
idm_tomb_ssh_sync ()
|
||||
{
|
||||
local host=$1
|
||||
@ -240,188 +466,28 @@ EOF
|
||||
}
|
||||
|
||||
|
||||
idm_tomb__encrypt ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Sanity check: id and local repo
|
||||
idm_tomb_require_enabled $id
|
||||
idm_tomb_require_valid_local_repo || idm_exit 1 ERR "Cound not continue"
|
||||
# DEPRECATED, replaced by idm_tomb_header $id
|
||||
# idm_tomb_require_enabled ()
|
||||
# {
|
||||
# local id=$1
|
||||
#
|
||||
# # Sanity check
|
||||
# lib_id_has_config $id
|
||||
#
|
||||
# # Load local vars
|
||||
# idm_tomb_header $id
|
||||
# }
|
||||
|
||||
# We check tomb repo here
|
||||
lib_git_is_repo $git_tomb_dir $git_tomb_work_tree || \
|
||||
idm_tomb__init $id || \
|
||||
idm_exit 1 ERR "Tomb cannot be used without git"
|
||||
|
||||
# Full sync both repo
|
||||
idm_tomb__sync $id ||
|
||||
idm_exit 1 ERR "Failed to push commits to tomb repo"
|
||||
|
||||
# Encrypt tomb data
|
||||
lib_gpg_encrypt_dir $git_tomb_dir $git_tomb_enc _PASS || \
|
||||
idm_exit 1 ERR "Failed to create tomb"
|
||||
|
||||
## Encrypt local data
|
||||
lib_gpg_encrypt_dir $git_local_dir $git_local_enc $GIT_AUTHOR_EMAIL || \
|
||||
idm_exit 1 ERR "Could not create local repo data"
|
||||
|
||||
# Clean tomb
|
||||
rm -rf $git_tomb_dir
|
||||
|
||||
lib_log NOTICE "Tomb has been closed into: $git_tomb_enc"
|
||||
}
|
||||
|
||||
idm_tomb__decrypt ()
|
||||
{
|
||||
local id=$1
|
||||
shift || true
|
||||
local opt=${@-}
|
||||
|
||||
# Sanity check
|
||||
idm_tomb_require_enabled $id
|
||||
|
||||
# Check if tomb repo is absent
|
||||
if lib_git_is_repo $git_tomb_dir $git_local_work_tree ; then
|
||||
lib_log WARN "A local repo is already present, we will overwrite it. Do you want to continue?"
|
||||
idm_cli_timeout 0 || idm_exit 1 ERR "Refuse to override existing repo"
|
||||
|
||||
# Let's not delete existing repo, just for fun and wee how git react :p
|
||||
fi
|
||||
|
||||
# Extract tomb
|
||||
lib_gpg_decrypt_dir $git_tomb_enc $git_tomb_dir || \
|
||||
idm_exit 1 ERR "Could not extract tomb"
|
||||
|
||||
# Extract local repo
|
||||
if idm_tomb_require_valid_local_repo; then
|
||||
# Local repo always win !, so we just sync
|
||||
lib_log INFO "Local repo already present, we just start sync"
|
||||
idm_tomb__sync $id
|
||||
else
|
||||
lib_gpg_decrypt_dir $git_tomb_enc $git_tomb_dir || \
|
||||
idm_exit 1 ERR "Could not extract tomb"
|
||||
fi
|
||||
|
||||
# Sync :D
|
||||
#idm_tomb__sync $id
|
||||
|
||||
lib_log NOTICE "Your tomb has been decrypted"
|
||||
|
||||
|
||||
}
|
||||
|
||||
idm_tomb__init()
|
||||
{
|
||||
local id=$1
|
||||
shift
|
||||
|
||||
# Sanity check: id and local repo
|
||||
idm_tomb_require_enabled $id
|
||||
idm_tomb_require_valid_local_repo || idm_exit 1 ERR "Cound not continue"
|
||||
|
||||
# Load tomb environment from local
|
||||
if [ ! -d "$git_tomb_dir" ] ; then
|
||||
mkdir -p "$git_tomb_dir"
|
||||
_git_tomb clone --bare $git_local_dir $git_tomb_dir || \
|
||||
idm_exit 1 ERR "Could not create tomb repo"
|
||||
lib_log NOTICE "Tomb repository has been created"
|
||||
else
|
||||
lib_log INFO "Tomb repository alreay exists"
|
||||
fi
|
||||
|
||||
# Load tomb environment from encrypted_tomb
|
||||
# Load tomb environment from user@server/encrypted.tomb
|
||||
|
||||
# Syncrhonise with tomb
|
||||
if lib_git_has_commits $git_local_dir $git_local_work_tree ; then
|
||||
idm_tomb__sync $id
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
idm_tomb__shred ()
|
||||
{
|
||||
local id=$1
|
||||
local arg=${2-}
|
||||
local files=
|
||||
|
||||
idm_tomb_require_enabled $id
|
||||
|
||||
case $arg in
|
||||
local) files="$git_local_dir" ;;
|
||||
tomb) files="$git_tomb_dir" ;;
|
||||
all) files="$git_tomb_dir $git_local_dir" ;;
|
||||
full) files="$git_tomb_dir $git_local_dir $git_local_enc" ;;
|
||||
disapear) files="$git_tomb_dir $git_local_dir $git_local_enc $( idm_git__get_files_of_interest $id | sed 's@^@~/@' | xargs )" ;;
|
||||
*)
|
||||
idm_exit 1 "You need to say: local|tomb|all|full"
|
||||
;;
|
||||
esac
|
||||
|
||||
lib_log WARN "All these files will be IRREVOCABLY DELETED."
|
||||
xargs -n 1 <<< "$files" | lib_log DUMP -
|
||||
|
||||
lib_log WARN "Do you want to continue ?"
|
||||
idm_cli_timeout 1 || idm_exit 1 ERR "No data deleted"
|
||||
|
||||
lib_log WARN "Run it yourself: rm -rf $files"
|
||||
|
||||
}
|
||||
|
||||
idm_tomb__enable () { return 0; }
|
||||
idm_tomb__disable () { return 0; }
|
||||
idm_tomb__kill () { return 0; }
|
||||
|
||||
|
||||
## IDM API functions
|
||||
##############################
|
||||
|
||||
|
||||
|
||||
## Internal functions
|
||||
##############################
|
||||
|
||||
idm_tomb_require_enabled ()
|
||||
{
|
||||
local id=$1
|
||||
|
||||
# Sanity check
|
||||
idm_validate id_config $id
|
||||
|
||||
# Load local repo vars
|
||||
idm_vars_git_local
|
||||
git_local_enc=$IDM_DIR_CACHE/git/$id/local.git.tar.gz.asc
|
||||
|
||||
# Load tomb vars
|
||||
idm_vars_git_tomb
|
||||
}
|
||||
|
||||
|
||||
_git_tomb ()
|
||||
{
|
||||
lib_git_bin $git_tomb_dir $git_tomb_work_tree $@ || return
|
||||
rc=$?
|
||||
#echo "RETURN2: $rc"
|
||||
return $rc
|
||||
}
|
||||
|
||||
_git_local ()
|
||||
{
|
||||
local rc=0
|
||||
lib_git_bin $git_tomb_dir $git_tomb_work_tree $@ || rc=$?
|
||||
return $rc
|
||||
}
|
||||
|
||||
## Module functions
|
||||
##############################
|
||||
|
||||
|
||||
idm_tomb_require_valid_local_repo ()
|
||||
{
|
||||
|
||||
if ! lib_git_is_repo $git_local_dir $git_local_work_tree ; then
|
||||
idm_exit 1 NOTICE "You need to have a local repo first"
|
||||
elif ! lib_git_has_commits $git_local_dir $git_local_work_tree ; then
|
||||
idm_exit 1 NOTICE "You need to commit all your changes"
|
||||
fi
|
||||
}
|
||||
# DEPRECATED, use: lib_git_is_repo_with_commits id instead
|
||||
# idm_tomb_require_valid_local_repo ()
|
||||
# {
|
||||
#
|
||||
# if ! lib_git_is_repo id ; then
|
||||
# lib_log NOTICE "You need to have a local repo first"
|
||||
# return 1
|
||||
# elif ! lib_git_is_repo_with_commits id ; then
|
||||
# lib_log NOTICE "You need to commit all your changes"
|
||||
# return 1
|
||||
# fi
|
||||
# }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user