From 19c465d6bed2bbb34a9b59575b3cb2ce0014b168 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Fri, 19 Jul 2024 03:26:14 -0300 Subject: [PATCH] Add functions.sh Replace repetitive code --- bin/setup-sourcezap | 10 +--------- libexec/sourcezap/commands/sourcezap-checkout | 17 +---------------- libexec/sourcezap/commands/sourcezap-clone | 15 +-------------- libexec/sourcezap/commands/sourcezap-install | 17 ++--------------- libexec/sourcezap/commands/sourcezap-pull | 15 +-------------- libexec/sourcezap/commands/sourcezap-rm | 7 ++----- libexec/sourcezap/functions.sh | 16 ++++++++++++++++ libexec/sourcezap/utils/git/get-changed-files | 5 +---- libexec/sourcezap/utils/git/get-removed-files | 5 +---- 9 files changed, 26 insertions(+), 81 deletions(-) create mode 100644 libexec/sourcezap/functions.sh diff --git a/bin/setup-sourcezap b/bin/setup-sourcezap index b39b3d0..d435ad8 100755 --- a/bin/setup-sourcezap +++ b/bin/setup-sourcezap @@ -10,15 +10,7 @@ doas="${localbase}"/etc/doas.conf ## # functions -printok() -{ - "${libexec}"/utils/printok "${1}" -} - -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} +. "${libexec}"/functions.sh ## # main diff --git a/libexec/sourcezap/commands/sourcezap-checkout b/libexec/sourcezap/commands/sourcezap-checkout index dc6ca76..91bd050 100755 --- a/libexec/sourcezap/commands/sourcezap-checkout +++ b/libexec/sourcezap/commands/sourcezap-checkout @@ -10,20 +10,7 @@ branch=$2 ## # functions -git() -{ - "${libexec}"/utils/git/run "${@}" -} - -printok() -{ - "${libexec}"/utils/printok "${1}" -} - -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} +. "${libexec}"/functions.sh ## # main @@ -42,9 +29,7 @@ if [ ! -e "${gitdir}/.git" ]; then exit 1 fi -set -x cd "${gitdir}" git fetch origin git checkout "${branch}" || git checkout -t origin/"${branch}" -set +x printok "${branch} checked out" diff --git a/libexec/sourcezap/commands/sourcezap-clone b/libexec/sourcezap/commands/sourcezap-clone index 6d78986..4bb5a51 100755 --- a/libexec/sourcezap/commands/sourcezap-clone +++ b/libexec/sourcezap/commands/sourcezap-clone @@ -11,20 +11,7 @@ branch=$3 ## # functions -git() -{ - "${libexec}"/utils/git/run "${@}" -} - -printok() -{ - "${libexec}"/utils/printok "${1}" -} - -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} +. "${libexec}"/functions.sh ## # main diff --git a/libexec/sourcezap/commands/sourcezap-install b/libexec/sourcezap/commands/sourcezap-install index 735032c..e82de5e 100755 --- a/libexec/sourcezap/commands/sourcezap-install +++ b/libexec/sourcezap/commands/sourcezap-install @@ -12,6 +12,8 @@ revfile=$3 ## # functions +. "${libexec}"/functions.sh + perform_update() { rev=$(cat "${revfile}") @@ -63,21 +65,6 @@ run_install() install -o root -g _sourcezap -m "${mode}" -v "$@" } -git() -{ - "${libexec}"/utils/git/run "${@}" -} - -printok() -{ - "${libexec}"/utils/printok "${1}" -} - -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} - ## # main if [ "$(id -u)" != "0" ]; then diff --git a/libexec/sourcezap/commands/sourcezap-pull b/libexec/sourcezap/commands/sourcezap-pull index 7171caf..c389e09 100755 --- a/libexec/sourcezap/commands/sourcezap-pull +++ b/libexec/sourcezap/commands/sourcezap-pull @@ -9,20 +9,7 @@ gitdir=$1 ## # functions -git() -{ - "${libexec}"/utils/git/run "${@}" -} - -printok() -{ - "${libexec}"/utils/printok "${1}" -} - -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} +. "${libexec}"/functions.sh ## # main diff --git a/libexec/sourcezap/commands/sourcezap-rm b/libexec/sourcezap/commands/sourcezap-rm index 963b28f..532a654 100755 --- a/libexec/sourcezap/commands/sourcezap-rm +++ b/libexec/sourcezap/commands/sourcezap-rm @@ -10,6 +10,8 @@ installdir=$2 ## # functions +. "${libexec}"/functions.sh + erase() { dir="${1}" @@ -24,11 +26,6 @@ erase() echo } -printerr() -{ - "${libexec}"/utils/printerr "${1}" -} - ## # main if [ "$(id -u)" != "0" ]; then diff --git a/libexec/sourcezap/functions.sh b/libexec/sourcezap/functions.sh new file mode 100644 index 0000000..105480d --- /dev/null +++ b/libexec/sourcezap/functions.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +git() +{ + "${libexec}"/utils/git/run "${@}" +} + +printok() +{ + "${libexec}"/utils/printok "${@}" +} + +printerr() +{ + "${libexec}"/utils/printerr "${@}" +} diff --git a/libexec/sourcezap/utils/git/get-changed-files b/libexec/sourcezap/utils/git/get-changed-files index ae70856..ebbcc13 100755 --- a/libexec/sourcezap/utils/git/get-changed-files +++ b/libexec/sourcezap/utils/git/get-changed-files @@ -10,10 +10,7 @@ commit=$2 ## # functions -git() -{ - "${libexec}"/utils/git/run "${@}" -} +. "${libexec}"/functions.sh ## # main diff --git a/libexec/sourcezap/utils/git/get-removed-files b/libexec/sourcezap/utils/git/get-removed-files index 4ce4492..ff5695e 100755 --- a/libexec/sourcezap/utils/git/get-removed-files +++ b/libexec/sourcezap/utils/git/get-removed-files @@ -10,10 +10,7 @@ commit=$2 ## # functions -git() -{ - "${libexec}"/utils/git/run "${@}" -} +. "${libexec}"/functions.sh ## # main