Refactor 'sourcezap-install' command (#7)

Co-authored-by: 0x1eef <0x1eef@protonmail.com>
Reviewed-on: http://git.bastion.home.network/0x1eef/sourcezap/pulls/7
This commit is contained in:
0x1eef 2024-08-18 22:31:31 -03:00
parent bc0553aabd
commit 3c994da3cb
5 changed files with 95 additions and 62 deletions

View file

@ -5,7 +5,6 @@ set -e
# variables
localbase=${LOCALBASE:-$(realpath "$(dirname "$0")"/../../..)}
libexec="${localbase}"/libexec/sourcezap
mode=u=rwX,g=rX,o=
git="${libexec}"/utils/git/run
gitdir=$1
installdir=$2
@ -16,60 +15,6 @@ revfile=$3
# shellcheck source=/dev/null
. "${libexec}"/functions/print.sh
perform_update()
{
# shellcheck disable=SC3043
local rev add del \
target parent parents
rev=$(cat "${revfile}")
add=$("${libexec}"/utils/git/get-changed-files "${gitdir}" "${rev}")
del=$("${libexec}"/utils/git/get-removed-files "${gitdir}" "${rev}")
for file in ${del}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
echo "rm ${target}"
rm -f "${target}"
find "${parent}" -type d -maxdepth 0 -empty -delete
done
for file in ${add}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
parents=""
while [ ! -e "${parent}" ]; do
parents="${parent} ${parents}"
parent=$(dirname "${parent}")
done
for dir in ${parents}; do
run_install "-d" "${dir}"
done
run_install "${file}" "${target}"
done
}
perform_install()
{
find -s . \
-maxdepth 1 \
! -name "." \
! -name ".git" \
! -name ".github" \
! -name ".gitignore" \
! -name ".gitattributes" \
! -name ".git-blame-ignore-revs" \
! -name ".cirrus-ci" \
! -name ".cirrus.yml" \
! -name ".gitignore" \
! -name ".arclint" \
! -name ".arcconfig" \
-exec cp -Rpv {} "${installdir}" \;
chown -Rv root "${installdir}"
}
run_install()
{
install -o root -g _sourcezap -m "${mode}" -v "$@"
}
##
# main
if [ "$(id -u)" != "0" ]; then
@ -77,14 +22,34 @@ if [ "$(id -u)" != "0" ]; then
exit 1
fi
umask "${mode}"
cd "${gitdir}"
run_install "-d" "${installdir}"
chmod ${mode} "${installdir}"
if [ -e "${revfile}" ]; then
perform_update
##
# install update
rev=$(cat "${revfile}")
"${libexec}"/utils/install/run -d "${installdir}"
"${libexec}"/utils/install/update-deleted-files "${gitdir}" "${installdir}" "${rev}"
"${libexec}"/utils/install/update-changed-files "${gitdir}" "${installdir}" "${rev}"
else
perform_install
##
# install from scratch
umask u=rwx,g=rx,o=
"${libexec}"/utils/install/run -d "${installdir}"
find -s . \
-maxdepth 1 \
! -name "." \
! -name ".git" \
! -name ".github" \
! -name ".gitignore" \
! -name ".gitattributes" \
! -name ".git-blame-ignore-revs" \
! -name ".cirrus-ci" \
! -name ".cirrus.yml" \
! -name ".gitignore" \
! -name ".arclint" \
! -name ".arcconfig" \
-exec cp -Rpv {} "${installdir}" \;
chown -Rv root "${installdir}"
fi
"${git}" rev-parse HEAD > "${revfile}"
printok "install complete"

View file

@ -0,0 +1,14 @@
#!/bin/sh
set -e
##
# variables
mode=u=rwX,g=rX,o=
##
# main
install -o root \
-g _sourcezap \
-m "${mode}" \
-v \
"${@}"

View file

@ -0,0 +1,28 @@
#!/bin/sh
set -e
##
# variables
localbase=${LOCALBASE:-$(realpath "$(dirname "$0")"/../../../..)}
libexec="${localbase}"/libexec/sourcezap
gitdir=$1
installdir=$2
rev=$3
##
# main
cd "${gitdir}"
files=$("${libexec}"/utils/git/get-changed-files "${gitdir}" "${rev}")
for file in ${files}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
parents=""
while [ ! -e "${parent}" ]; do
parents="${parent} ${parents}"
parent=$(dirname "${parent}")
done
for dir in ${parents}; do
"${libexec}"/utils/install/run -d "${dir}"
done
"${libexec}"/utils/install/run "${file}" "${target}"
done

View file

@ -0,0 +1,22 @@
#!/bin/sh
set -e
##
# variables
localbase=${LOCALBASE:-$(realpath "$(dirname "$0")"/../../../..)}
libexec="${localbase}"/libexec/sourcezap
gitdir=$1
installdir=$2
rev=$3
##
# main
cd "${gitdir}"
files=$("${libexec}"/utils/git/get-removed-files "${gitdir}" "${rev}")
for file in ${files}; do
target="${installdir}/${file}"
parent=$(dirname "${target}")
echo "rm ${target}"
rm -f "${target}"
find "${parent}" -type d -maxdepth 0 -empty -delete
done

View file

@ -1,13 +1,17 @@
* vNEXT
** Break up 'sourcezap-install' into multiple files
'libexec/sourcezap/utils/install/' contains files that cover both
a fresh install and applying an update
** Add 'setup/setup-doas' improvements
More likely to do what's expected, but blind spots still exist
** Add 'sourcezap setup', 'sourcezap teardown'
Replaces and enhances 'setup-sourcezap'
** Add libexec/sourcezap/commands/sourcezap-sh
Runs /bin/sh within /home/_sourcezap/src/ as the '_sourcezap' user
** Add 'libexec/sourcezap/commands/sourcezap-sh'
Runs '/bin/sh' within '/home/_sourcezap/src/' as the '_sourcezap' user
* v1.0.0